2013-07-25 55 views
0

我有多個組合框需要顯示候選人的名稱,從總統到繆斯,沒有按任何按鈕,但我不知道該怎麼做。如何使用vb.net顯示數據庫中的記錄

公共類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 

末級

我應該怎麼把它連接到數據庫?

+0

請搜索[谷歌](https://www.google.com/search?q=populate+combobox+vb。淨&RLZ = 1C1AFAB_enUS452US452&OQ =填充+組合框+ VB&AQS = chrome.2.69i57j0l3.4232j0&的SourceID =鉻&即= UTF-8#安全=關閉&RLZ = 1C1AFAB_enUS452US452&sclient = PSY-AB&q =填充+組合框+ vb.net +數據庫和OQ =填充+組合框+ vb.net + DA&gs_l = serp.1.0.0i22i30l4.5829.6496.0.7396.3.3.0.0.0.0.158.411.0j3.3.0 .... 0.0..1c.1.20.psy-ab.mN6fPsjuvDs&PBX = 1&BAV = on.2,or.r_qf &BVM = bv.49784469%2Cd.aWc%2Cpv.xjs.s.en_US.MpiVkF51mp A.O&fp = 1fb426de7cf3e564&biw = 1110&bih = 759),有***許多***結果 – Dom

回答

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 
相關問題