Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
'数据库连接对象
'错误的写法
'Dim objConnection As SqlConnection = New SqlConnection("server=(local);database=pubs;user id=sa;password=")
'正确写法
Dim objConnection As New SqlConnection("Data Source=127.0.0.1; Initial Catalog=pubs; user id=sa; password=;")
'数据适配器
Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter()
'DataSet
Dim objDataSet As DataSet = New DataSet()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'设置查询命令属性
objDataAdapter.SelectCommand = New SqlCommand
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = "select au_lname,au_fname,title,price from authors join titleauthor on authors.au_id=titleauthor.au_id join titles on titleauthor.title_id=titles.title_id order by au_lname,au_fname"
objDataAdapter.SelectCommand.CommandType = CommandType.Text
'打开数据库连接
objConnection.Open()
'填充DataSet对象
objDataAdapter.Fill(objDataSet, "authors")
'关闭数据库连接
objConnection.Close()
'给DataGrid绑定数据
grdAuthorTitles.DataSource = objDataSet
grdAuthorTitles.DataMember = "authors"
'清除
objDataAdapter = Nothing
objConnection = Nothing
End Sub
End Class
如上图所示,最后选上联机就可以了
希望对你能有所帮助。
微软 sqlserver?百度下就是了,这都是普遍的
QQ:470692236
请注明百度提问。