如何在vb.net中获取标签上的数据库值?

时间:2017-12-06 07:25:50

标签: vb.net

我是初级程序员。实际上我使用了两个名为“Category”和“Location”的组合框。当我点击提交时,它应该分别从标签本身的数据库中显示化学品类别和位置。

1 个答案:

答案 0 :(得分:0)

我想你可能正在寻找这样的东西?

 Dim connetionString As String
    Dim connection As SqlConnection
    Dim adapter As SqlDataAdapter
    Dim ds As New DataSet

    connetionString = "ConnetionString;"
    connection = New SqlConnection(connetionString)
    Try
        connection.Open()
        adapter = New SqlDataAdapter("Select column1, column2, column3, column4 from Table1 where Id='" & TextBox9.Text & "'", connection)
        adapter.Fill(ds)
        connection.Close()

        If ds.Tables(0).Rows.Count > 0 Then
            label1.Text = "blablabla" & ds.Tables(0).Rows(0)("column1").ToString()
            label2.Text = "blablabla" & ds.Tables(0).Rows(0)("column2").ToString()
            label3.Text = "blablabla" & ds.Tables(0).Rows(0)("column3").ToString()
            label4.Text = "blablabla" & ds.Tables(0).Rows(0)("column4").ToString()

        End If
    Catch ex As Exception
        Form4.TextBox1.Text = "Failed to connect to database"
        Form4.Show()

    End Try
    Return False