在WinForms中显示数据表

时间:2014-01-10 18:59:38

标签: vb.net winforms

我正在创建一个显示数据表的Windows表单。我还希望能够从显示的表中选择一行,并在调用它的表单上显示信息。我已连接到服务器但似乎无法填充数据表。

Dim Students As String = "select * from dbo.[students]"
Dim Studentscmd As New SqlCommand(Students, cnn)
Dim Studentstable As New DataTable
Dim Studentadapter As New SqlDataAdapter

Dim NStudents As DataSet = New DataSet()
Studentadapter.Fill(NStudents)

Datagridview.DataGridView1.DataSource = NStudents
Datagridview.DataGridView1.Show()

1 个答案:

答案 0 :(得分:2)

试试这个:
(正如@DarrenMB提到你缺少SqlAdapterSqlCommand之间的联系)

Dim Students As String = "select * from dbo.[students]"
Dim Studentstable As New DataTable()
Using Studentscmd As New SqlCommand(Students, cnn)
    Using Studentadapter As New SqlDataAdapter(Studentscmd) 'This link Adapter with command
        Studentadapter.Fill(Studentstable) 'Can fill straight to DataTable
    End Using
End Using
Datagridview.DataGridView1.DataSource = Studentstable
Datagridview.DataGridView1.Show()

“更好地练习”在处理非托管资源(如数据库
)时使用Using statementEnd Using上,使用Using创建的所有对象和资源都将正确处理/释放