我希望根据组合框的选择转到下一行sql数据

7kjnsjlb  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(275)

基本上,如果组合框选择(字段)是“engineering”,那么只会显示与该选择相对应的行。我想访问下一行数据,这些数据与此(字段)选择相似且仅对应,并适当地填充文本框控件。将显示该行中该组合框选择下保存的所有数据。这个很好用。按钮1下的代码模块是我试图访问下一行的代码,但显示的数据不正确。是否有修复和显示所需数据的方法-组合框选择符合数据库条件的下一整行sql数据。

Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
    Dim Command As New SqlCommand("Select * From Table_Upload where Field = @Field", Connection)
    Dim DA As New SqlDataAdapter(Command)
    Dim DT As New DataTable()
    Dim DR As SqlDataReader

    Command.Parameters.Add("@Field", SqlDbType.VarChar).Value = CmbField.SelectedItem
    DA.Fill(DT)
    CmbField.DataSource = DT
    CmbField.DisplayMember = "Field"
    Connection.Open()
    DR = Command.ExecuteReader()

    If DR.Read() Then
        TxtTitle.Text = DR(0)
        TxtComp.Text = DR(1)
        TxtDate.Text = DR(2)
        TxtSalary.Text = DR(3)
        CmbEmp.SelectedItem = DR(4)
        TxtDesc.Text = DR(5)
        TxtExp.Text = DR(6)
        TxtPhone.Text = DR(7)
        TxtEmail.Text = DR(8)
        TxtAddr.Text = DR(9)
    End If

    Connection.Close()
End Sub

下一步代码:private sub button1\u click(sender as object,e as eventargs)处理btnnext。click dim command as new sqlcommand(“select*from table\u upload where field=@field”,connection)dim da as new sqldataadapter(command)dim ds as new dataset()dim dr as sqldatareader

Command.Parameters.Add("@Field", SqlDbType.VarChar).Value = CmbField.SelectedItem

DA.Fill(DS, "Table_Upload")

CmbField.DisplayMember = "Field"

Connection.Open()
DR = Command.ExecuteReader()
If DR.Read() Then
    TxtTitle.Text = DR(+1).ToString()
    TxtComp.Text = DR(+1).ToString()
    TxtDate.Text = DR(+1).ToString()
    TxtSalary.Text = DR(+1).ToString()
    CmbEmp.SelectedItem = DR(+1).ToString()
    TxtDesc.Text = DR(+1).ToString()
    TxtExp.Text = DR(+1).ToString()
    TxtPhone.Text = DR(+1).ToString()
    TxtEmail.Text = DR(+1).ToString()
    TxtAddr.Text = DR(+1).ToString()
End If
Connection.Close()

末端接头

dnph8jn4

dnph8jn41#

一旦有了数据读取器,就创建一个 DataTable 并称之为 Load 方法加载所有数据。然后可以绑定 DataTableBindingSource 以及 BindingSource 给你的 TextBoxes . 你可以打电话给我 MoveNext 通过数据前进。

myDataTable.Load(myDataReader)
myBindingSource.DataSource = myDataTable
myTextBox.DataBindings.Add("Text", myBindingSource, "MyColumn")

编辑:
在根据joelcoehoorn下面的评论进行了一些阅读和测试之后,我了解到,虽然不能将数据读取器直接绑定到windows窗体中的控件,但可以将数据读取器绑定到 BindingSource 它会产生一个 IBindingList(Of DataRecordInternal) ,这是一个 IList 从而满足winforms绑定的要求。一 DataRecordInternal 可以按列名或序号索引,方法与 DataRow 或者 DataRowView 可以,因此它们可以以几乎相同的方式进行处理。

Using connection As New SqlConnection("connection string here"),
      command As New SqlCommand("SELECT * FROM MyTable", connection)
    connection.Open()

    Using reader = command.ExecuteReader()
        BindingSource1.DataSource = reader
        ComboBox1.DisplayMember = "Name"
        ComboBox1.ValueMember = "Id"
        ComboBox1.DataSource = BindingSource1
        TextBox1.DataBindings.Add("Text", BindingSource1, "Description")
    End Using
End Using

不管怎样

kuhbmx9i

kuhbmx9i2#

“我想访问下一行数据”
打个电话就可以了 DR.Read() 再一次。

相关问题