如何使用数据库(访问数据库)中的数据填充vb 2010中的Combobox

时间:2015-05-27 09:34:39

标签: vb.net visual-studio-2010

抱歉,我是Vb的初学者,因此我正在寻找一种简单的方法来使用来自访问数据库的数据填充combox。

PHP WAY

#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;

如何将上述代码转换为vb vb form with combobox

1 个答案:

答案 0 :(得分:1)

        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

cb有组合框名称。不要忘记在代码的最顶层导入OleDb,如下所示:

imports System.Data.OleDb