从访问组合框 vb.net 添加项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28927257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add items from access to combobox vb.net
提问by Beginner
I want to fill a combobox with items from ms access database. here's my code..
我想用 ms access 数据库中的项目填充组合框。这是我的代码..
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
however the combobox won't fill up, but if i remove the "where Position ='President'" the code works, can someone help me i've been stuck here for hours.
但是组合框不会填满,但是如果我删除“where Position ='President'”代码有效,有人可以帮助我,我已经被困在这里好几个小时了。
回答by Pradnya Bolli
you can try this i hope this work.
你可以试试这个我希望这个工作。
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

