vb.net 如何使用数据库(访问数据库)中的数据填充 vb 2010 中的 Combobox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30478516/
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
how to fill Combobox in vb 2010 with data from database (access database)
提问by SyntaxError
Sorry am a beginer to Vb, therefore am looking for a simple way to fill combox with data from access database.
对不起,我是 Vb 的初学者,因此我正在寻找一种简单的方法来用访问数据库中的数据填充组合框。
PHP WAY
PHP方式
#connection first.
$sql = "select * from projects";
$select_records = mysqli_query($con,$sql);
while($fetch = mysqli_fetch_array($select_records)):
echo "<form>";
echo "<select name='project">";
echo "<option name='$fetch[projectname]'>$fetch[projectname]</option>";
echo "</select>";
echo "</form>";
endwhile;
how can i transform the above code to vb
我如何将上面的代码转换为 vb
回答by MAC
Dim query as String
Dim con as OleDbConnection
Dim command as OleDbCommand
Dim reader as OleDbDataReader
con.Open()
query = "select * from projects"
command = New OleDbCommand(query, con)
reader = command.ExecuteReader()
If reader.HasRows Then
While reader.Read()
cb.Items.Add(reader("FieldName"))
End While
End If
cbthere is the combobox name. Don't forget to import OleDb at the topmost of your code like this:
cb有组合框名称。不要忘记在代码的最顶部导入 OleDb,如下所示:
imports System.Data.OleDb

