2011-09-26 123 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