0

我从朋友那里修改了一些代码

但它只显示第一条记录我将如何操作以遍历所有记录

任何能在代码中给出正确答案的人都将获得赏金感谢

注意: DisplayOfficeEquipmentList() 是将数据库上的数据显示到文本框和组合框的子

Public Sub DisplayOfficeEquipmentList()
        Dim sqlconn As New SqlClient.SqlConnection
        sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
        "Database = EOEMS;integrated security=true"

        Dim dt As New DataTable

        sqlconn.Open()
        Dim da As New SqlDataAdapter("SELECT * FROM tblOfficeEquipmentProfile", sqlconn)
        da.Fill(dt)
        cmbCategory.Text = dt.Rows(0)("OE_Category").ToString()
        cmbSubCategory.Text = dt.Rows(0)("OE_SubCategory").ToString()
        txtOEID.Text = dt.Rows(0)("OE_ID").ToString()
        txtName.Text = dt.Rows(0)("OE_Name").ToString()
        txtUser.Text = dt.Rows(0)("OE_User").ToString()
        cmbBrand.Text = dt.Rows(0)("OE_Brand").ToString()
        cmbModel.Text = dt.Rows(0)("OE_Model").ToString()
        txtSpecs.Text = dt.Rows(0)("OE_Specs").ToString()
        txtSerialNo.Text = dt.Rows(0)("OE_SerialNo").ToString()
        txtPropertyNo.Text = dt.Rows(0)("OE_PropertyNo").ToString()
        txtMacAddress.Text = dt.Rows(0)("OE_MacAddress").ToString()
        txtStaticIP.Text = dt.Rows(0)("OE_Static_IP").ToString()
        txtVendor.Text = dt.Rows(0)("OE_Vendor").ToString()
        dtpPurchaseDate.Text = dt.Rows(0)("OE_PurchaseDate").ToString()
        txtWarrantyStatus.Text = dt.Rows(0)("OE_WarrantyStatus").ToString()
        txtWarrantyInclusiveYear.Text = dt.Rows(0)("OE_WarrantyInclusiveYear").ToString()
        txtStatus.Text = dt.Rows(0)("OE_Status").ToString()
        cmbDeptCode.Text = dt.Rows(0)("OE_Dept_Code").ToString()
        cmbLocationCode.Text = dt.Rows(0)("OE_Location_Code").ToString()
        txtRemarks.Text = dt.Rows(0)("OE_Remarks").ToString()
        sqlconn.Close()
    End Sub

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click



    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click

    End Sub
4

2 回答 2

2

您必须循环这些行,您将获得索引 0 处的行的值(只有一行)。

使用 foreach:

foreach (System.Data.DataRow row in dt.Rows)
{ 
 //Get values of row
}

编辑:在 vb.net 中会是这样的:

For Each filarow As DataRow In dt.Rows
            Dim OE_ID As String = filarow("OE_ID").ToString
            Dim txtName As String = filarow("OE_NAME").ToString
Next

顺便说一句,您似乎正在填充文本框,因此值将在下一个循环中更改。也许您应该使用另一个控件,例如 ListBox

于 2013-05-06T08:43:29.527 回答
1

您需要将检索数据的逻辑与显示该数据的逻辑分开。首先添加一个加载数据表的方法

Private Function LoadData() as DataTable

    Using sqlconn = New SqlClient.SqlConnection("server = SKPI-APPS1;" & _
                                 "Database = EOEMS;integrated security=true")
        Dim dt As New DataTable
        sqlconn.Open()
        Dim da As New SqlDataAdapter("SELECT * FROM tblOfficeEquipmentProfile", sqlconn)
        da.Fill(dt)
        return dt
    End Using
End Function

然后在按钮中单击将要显示的数据表和行号传递给 DisplayOfficeEquipmentList

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
     if currentRow + 1 >= dt.Rows.Count Then
        Return
     End if
     currentRow = currentRow + 1
     DisplayOfficeEquipmentList(dt, currentRow)
End Sub

Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
     if currentRow - 1 < 0 Then
        Return
     End if
     currentRow = currentRow - 1
     DisplayOfficeEquipmentList(dt, currentRow)
End Sub

并在 DisplayOfficeEquipmentList 中引用按钮单击传递的行

Public Sub DisplayOfficeEquipmentList(ByRef dt as DataTable, ByVal rowNum as INteger)
    Dim row as DataRow
    row = dt.Rows(rowNum)
    cmbCategory.Text = row("OE_Category").ToString()
    cmbSubCategory.Text = row("OE_SubCategory").ToString()
    ....
End Sub

为此,您需要在显示表单(Load 事件?)时在某处调用 LoadData,并且您应该将 currentRow 设置为表单全局级别变量

于 2013-05-06T08:42:09.633 回答