0

如果我在组合框中选择一个来自 sql 的项目,我如何将多个数据从 sql 绑定到标签这是我的代码:

    Private Sub cmboCourse_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmboCourse.SelectedIndexChanged

    If cmboCourse.Text = "ADVANCED COMPUTER TECHNICIAN" Then
        callMe()
    ElseIf cmboCourse.Text = "AUTOELECTRICITY" Then
        callMe()
    ElseIf cmboCourse.Text = "AUTOMOTIVE" Then
        callMe()
    End If

End Sub


Private Sub callMe()

Dim str As String = ("Data Source=PC1; User ID=sa; Password=pwd;Databasfriend")
Dim con As New SqlConnection(str)
Dim str1 As String = "SELECT * FROM tbl_course"
Dim da As New SqlDataAdapter(str1, con)
Dim dataset1 As New DataSet()
da.Fill(dataset1, "course")
lbl.DataBindings.Add("text", dataset1, "course.Course_Code")

end sub

这是我的桌子

 Course_Code      Course
 ACT              ADVANCED COMPUTER TECHNICIAN
 AE               AUTOELECTRICITY
 AM               AUTOMOTIVE

它只绑定一个数据,我想在一个特定列中绑定许多数据示例我在组合框中选择一个课程 AUTOMOTIVE AUTOMOTIVE 的 course_code 如何绑定到标签,如果我选择 AUTOELECTRICITY AUTOELECTRICITY 的 course_code 如何绑定到相同的标签

4

2 回答 2

0

sc.Open() Dim da As New SqlDataAdapter()

    Dim dataset1 As New DataSet()
    Dim sql As New SqlCommand("Select * from book where Title='" + cmbtit.Text + "'", sc)

    da.SelectCommand = sql
    da.SelectCommand.ExecuteNonQuery()
    da.Fill(dataset1, "book")

    If dataset1.Tables("book").Rows.Count > 0 Then
        txtauthor.Text = dataset1.Tables("book").Rows(0)("Author")
    Else
        MsgBox("Author [" & cmbtit.Text & "] not found")
    End If
    sc.Close()

End Sub
于 2014-11-10T10:47:38.987 回答
0

这未经测试,如果它不起作用,请告诉我。

Private Sub cmboCourse_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmboCourse.SelectedIndexChanged
    callMe(cmboCourse.Text)
End Sub


Private Sub callMe(ByVal course as String)

    Dim str As String = ("Data Source=PC1; User ID=sa; Password=pwd;Databasfriend")
    Dim con As New SqlConnection(str)
    Dim str1 As String = "SELECT * FROM tbl_course WHERE [Course]='" & course & "'"
    Dim da As New SqlDataAdapter(str1, con)
    Dim dataset1 As New DataSet()
    da.Fill(dataset1, "tbl_course")
    'lbl.DataBindings.Add("text", dataset1, "course.Course_Code")
    If dataset1.Tables("tbl_course").Rows.Count > 0 Then
        lbl.Text = dataset1.Tables("tbl_course").Rows(0)("Course_Code")
    Else
        MsgBox "Course [" & course & "] not found"
    End If

End Sub

请注意,在您的代码中,da.Fill(dataset1, "course")您将表指定为“课程”,而 select 语句从“tbl_course”表中选择。我假设后者是正确的名称。

编辑1:

错误修复

编辑2:

调试

于 2013-03-26T19:47:09.270 回答