Combobox 如何使用vb.net显示数据库中的记录

Combobox 如何使用vb.net显示数据库中的记录,combobox,Combobox,我有多个组合框,可以显示候选人的名字,从总统到缪斯,不需要按任何按钮,但我不知道怎么做 公共级Frm_ElemBallot Private Sub ComboBox7_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox7.SelectedIndexChanged End Sub Private Sub Frm_ElemBallot_Load(ByVa

我有多个组合框,可以显示候选人的名字,从总统到缪斯,不需要按任何按钮,但我不知道怎么做

公共级Frm_ElemBallot

Private Sub ComboBox7_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox7.SelectedIndexChanged

End Sub

Private Sub Frm_ElemBallot_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
末级


我应该如何将其连接到数据库?

您应该这样尝试

示例代码:

Try
            Dim cn As SqlConnection
            Dim strCnn As String = "Your data base connection string"
            cn = New SqlConnection(strCnn)
            cn.Open()
            Dim comm As New SqlCommand("Your sp or Command text to get the data from DB", cn)
            '' If ur using Sp then Commandtype= CommandType.StoredProcedure if  'is  Text then comm.CommandType=CommandType.Text
            comm.CommandType = CommandType.StoredProcedure
            Dim da As New SqlDataAdapter(comm)
            Dim ds As New DataSet
            da.Fill(ds)
            ListBox1.Items.Clear()
            If ds.Tables.Count > 0 And ds.Tables(0).Rows.Count > 0 Then
                ListBox1.DataSource = ds
            End If
            ''Close your connections and commands.
        Catch ex As Exception
            ''Handle error if any
        End Try
请搜索,有很多结果