如何使用连接字符串(SQL Server,VB.NET)基于另一个ComboBox填充ComboBox

时间:2015-06-16 16:32:50

标签: sql-server vb.net combobox

当用户从组合框1(我们与之合作并且是合作伙伴的公司)中选择客户端时,它应该仅为该客户端填充组合框2中的用户,这需要来自SQL Server中的特定数据库。

示例:

当用户选择客户端时,第一个Combobox包含所有客户端,而第二个ComboBox只包含该数据库中的所有用户,我希望第二个ComboBox的用户列表根据从列表中选择的客户端进行更改第一个ComboBox,但包括一个连接字符串,通过SQL Server。

因此,如果我在Combobox 1和Combobox 2中选择说.... Google,我希望有来自Google的10位用户。但是,如果我改变主意并选择在Combobox 1和Combobox 2中选择Yahoo,我希望.....这次来自雅虎的12位用户。

我正在使用的数据库名称: MyDatabase

根据我所做的选择,从这些数据库中获取用户,其中包含那些特定用户,而不是具有用户的MyDatabase:

  • GoogleQA(如果用户选择Google,仅从此数据库获取用户并在运行时填充该用户)
  • YahooQA(如果用户选择了Yahoo,只从该数据库获取用户并在运行时填充它)

我的VB.Net代码:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("conStr").ConnectionString)
        Dim cmd As New SqlCommand("Select * from CLIENTS", con)
        con.Open()
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader())
        con.Close()
        cboClient.DataSource = dt
        cboClient.DisplayMember = "CLIENT_NAME"
        cboClient.ValueMember = "ID"

        rtfMessage.Tag = "Enter your message here"
        rtfMessage.Text = CStr(rtfMessage.Tag)

我可以在组合框代码中添加什么来实现我想要实现的最终目标:

  Private Sub cboUser_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboUser.SelectedValueChanged
--What do I put here?
    End Sub

Private Sub cboClient_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboClient.SelectedIndexChanged
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("conStr2").ConnectionString)
        Dim cmd As New SqlCommand("Select * from USERS", con)
        con.Open()
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader())
        con.Close()
        cboClient.DataSource = dt
        cboClient.DisplayMember = "NetworkID"
        cboClient.ValueMember = "ID"
        cboClient.Text = ""
        If cboClient.SelectedItem.Text = "Sunoco" Then
            cboUser.Items.Add("NetworkID")
        End If
    End Sub

可能性:在客户端更改组合框功能(cboClient_SelectedValueChanged)

1.) Change the database connection
2.) Query the proper user data from the correct database
3.) populate the user combo box with that user data

1 个答案:

答案 0 :(得分:0)

您只需在用户部分内的if语句中执行您在加载部分中所做的操作。

有点像:

    `If cboClient.SelectedItem.Text = "Google" then
     (code to populate the other combobox with google info)
     End if`


     `If cboClient.SelectedItem.Text = "Yahoo" then 
        dim sql as string = "Select firstName+ " " +lastname as empName from 
        Yahoo table"
        dim cmd as sqladapter(sql, con)
        dim dt as datatable
        cmd.fill(dt)
        cboUsers.datasource = dt
        cboUsers.Datatextfield = "empName"
        cboUsers.Datavaluefield = "empName"
        cboUsers.DataBind()
     End if


        Dim sql2 As String = "Select EQPnumber, EQPdesc from CREW_LINEUP_ACTIVE_EQUIPMENT"
        Dim activeEQPadt As New SqlDataAdapter(sql2, IPMS.Settings.conn)
        activeEQPadt.Fill(activeDT)
        ActiveEQPLstBx.DataSource = activeDT
        ActiveEQPLstBx.DataTextField = "EQPdesc"
        ActiveEQPLstBx.DataValueField = "EQPnumber"
        ActiveEQPLstBx.DataBind()