如何将2个ComboBox连接到不同的表?

时间:2019-03-03 15:30:29

标签: sql vb.net

我正在尝试设置一个窗体,该窗体上有2个ComboBox,每个ComboBox都附加到数据库中的不同表。
用户在每个组合框中进行选择后,我想查询另一个表以列出所有符合条件的项目。
到目前为止,我已经将其与1个ComboBox配合使用,但是第二个ComboBox无法连接到数据库。
我不确定如何将其他ComboBox添加到代码中。此按钮目前仅显示1个ComboBox的输出,但是我将对其进行更改以将值保存在内存中,并打开一个新窗口以显示查询结果。

Imports System.Data.SqlClient

Public Class frmSecond
    'Private MyDatAdp As New SqlDataAdapter '
    'Private MyDataTbl As New DataTable '
    'Private MyRowPosition As Integer = 0 '
    Private Sub frmSecond_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim connectionString As String = Nothing
        connectionString = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim sql As String = Nothing
        Dim adaptor As New SqlDataAdapter()
        Dim ds As New DataSet()
        Dim i As Integer = 0
        sql = "Select SID, EVENT from Event_Type"
        connection = New SqlConnection(connectionString)
        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            adaptor.SelectCommand = command
            adaptor.Fill(ds)
            adaptor.Dispose()
            command.Dispose()
            connection.Close()
            ComboBox1.DataSource = ds.Tables(0)
            ComboBox1.ValueMember = "SID"
            ComboBox1.DisplayMember = "Event"
        Catch ex As Exception
            MessageBox.Show("Can't open connection")
        End Try

        connectionString = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"
        Dim sql1 As String = Nothing
        Dim ds1 As New DataSet()
        sql1 = "Select BID, EVENT_Sub from Sub_Event"
        connection = New SqlConnection(connectionString)

        Try
            connection.Open()
            command = New SqlCommand(sql1, connection)
            adaptor.SelectCommand = command
            adaptor.Fill(ds1)
            adaptor.Dispose()
            command.Dispose()
            connection.Close()
            ComboBox2.DataSource = ds1.Tables(0)
            ComboBox2.ValueMember = "BID"
            ComboBox2.DisplayMember = "Event_Sub"
        Catch ex As Exception
            MessageBox.Show("Can't open 2nd connection")
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox(ComboBox1.Text + " --" + ComboBox1.DisplayMember)
    End Sub
End Class

这不起作用!
它仅连接到第一个组合,并且显示未连接到第二个组合,并且报告的异常错误找不到或丢失了PDB文件。
我没有经验,而且年纪较大,因此可以提供一些帮助。

0 个答案:

没有答案