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

时间:2013-07-25 03:45:09

标签: combobox

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

Public Class 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

结束班

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

1 个答案:

答案 0 :(得分:0)

你应该这样试试

示例代码:

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