我正在尝试将组合框链接到我的数据库列" name",为此我正在观看来自youtube的教程。一切都很顺利,但现在我遇到连接组合框的问题。我是VB.Net的新手,所以请指导我。 这是我的代码:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con = New SqlConnection
con.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\zeeshan\documents\visual studio 2013\Projects\Tutorials\Tutorials\Register.mdf"
Dim READER As SqlDataReader
Try
con.Open()
Dim Query As String
Query = "select * from dbo.edata"
cmd = New SqlCommand(Query, con)
READER = cmd.ExecuteReader
While READER.Read
Dim sName = READER.GetString("name")
ComboBox1.Items.Add(sName)
End While
con.Close()
Catch ex As SqlException
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Sub
答案 0 :(得分:2)
datatable dt= cmd.ExecuteReader
if(dt.rows.count>0)
{
foreach(Datarow dr in dt.rows)
{
ComboBox1.Items.Add(dt.Rows[0]["Name"].ToString());
}
}
答案 1 :(得分:1)
使用索引列
While READER.Read
Dim sName = READER.GetString(1)
ComboBox1.Items.Add(sName)
End While