【问题标题】:looping through all records on the database遍历数据库中的所有记录
【发布时间】:2013-05-10 14:48:03
【问题描述】:

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

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

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

注意: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

【问题讨论】:

  • 添加其余代码。所有的东西都在 DisplayOfficeEquipmentList() 中
  • @CarlosLanderas 这是先生

标签: sql vb.net


【解决方案1】:

你必须循环这些行,你得到索引 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

【讨论】:

  • 你不需要表格只是 dt.rows
  • 哦,看到你现在改了。 :)
  • 是的,我以为他在使用Dataset,当我看到它是Datatable时就改变了。现在在没有 IDE 的情况下进行编码 xD
  • 我用 vb.net 代码更新了我的答案。只需包含其余字段
  • 考虑使用另一个控件,如 ListBox。当您迭代行时,文本框将显示最后一行的内容。
【解决方案2】:

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

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 设置为表单全局级别变量

【讨论】:

  • 先生,我在将函数调用到主窗体时遇到问题,但我的全局变量现在正在工作
  • 哪个问题?哪个功能?任何错误信息?我可以帮忙,但我需要更多上下文
猜你喜欢
  • 1970-01-01
  • 2011-10-19
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多