参考一下:
Dim conn As New SqlConnection("Data Source=localhost;Initial Catalog=MyDbase;Integrated Security=True")
'要养成Data Source的值为localhost的习惯,因为这就可以让软件可以在其他电脑上正常运行
Dim daAuthors As New SqlDataAdapter("Select * From student", conn)
Dim ds As New DataSet
Dim ds As New DataSet
Try
conn.Open() '在形成SqlDataAdapter前打开conn
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
daAuthors.Fill(ds, "student")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
' conn.Close() '在填充完ds后关闭连接,接着对ds进行操作
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
DataGrid1.DataSource = ds.Tables("student")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try