添加访问combobox vb.net的项目

时间:2015-03-08 13:58:39

标签: vb.net combobox

我想用ms访问数据库中的项目填充组合框。 这是我的代码..

conn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ainunandile\Desktop\ElectionDB.mdb;Persist Security Info=False;")
conn.Open()
sqlQry = "SELECT * FROM Candidates where Position='President'"
cmd = New OleDbCommand(sqlQry, conn)
rdr = cmd.ExecuteReader
While rdr.Read()
    ComboBox1.Items.Add(rdr("CandidateName").ToString())
End While

然而组合框不会填满,但如果我删除“where Position ='President'”代码可以工作,有人可以帮助我,我已经被困在这里好几个小时了。

1 个答案:

答案 0 :(得分:1)

你可以尝试这个我希望这项工作。

Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ainunandile\Desktop\ElectionDB.mdb;Persist Security Info=False;")
       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          cn.Open()
          Dim cm As New OleDbCommand("select * from Candidates where Position='President'", cn)
          Dim dr As OleDbDataReader = cm.ExecuteReader
          While dr.Read
             ComboBox1.Items.Add(dr(1).ToString)
          End While
          dr.Close()
          cn.Close()
       End Sub