2011-09-26 91 views
0

我正面临着在创建的Visual Basic窗体上显示我的表的记录的问题。无法在Visual Basic窗体上显示SQL数据库数据

这是我的代码:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
    myconnection = New SqlConnection("server=HOME-PC\SQLEXPRESS;uid=sa;pwd=123;database=college") 

    myconnection.Open() 
    mycommand = New SqlCommand("SELECT * from demo3)", myconnection) 
    Dim mySqlDataAdapter As New SqlDataAdapter(mycommand) 
    Dim mydsStudent As New DataSet() 

    mySqlDataAdapter.Fill(mydsStudent, "Student") 

    ra = mycommand.ExecuteNonQuery() 
    MessageBox.Show("Data Displayed" & ra) 

    myconnection.Close() 
End Sub 
End Class 

注:我的数据库名是 “大学”,表名是 “demo3”。表格包含2栏,即名称和卷号。如何在我创建的Visual Basic窗体上的这些列中显示数据?

回答

2

您不需要调用执行非查询。您可以将数据集绑定到DataGridView。像这样

Dim DataGridView1 as new DataGridView() 
DataGridView1.DataSource = mydsStudent 
'Your table goes here, not sure about the exact propety name, hope it works. 
DataGridView1.DisplayMember = "demo3" 
Me.Controls.Add(DataGridView1) 
+0

是DisplayMemeber还是DisplayMember? –

+0

Anuraj:我得到一个错误,说DataGridView1没有声明...如何解决这个错误? –

+0

@ Parth_90您需要在窗体中拖放DataGridview。或者你喜欢尝试我的答案,我修改了它。 – Anuraj