案例名称:使用Fill方法\r

工程名称:Proj4-10\r

Imports System.Data.SqlClient\r

\r

Public Class Form1\r

\r

'声明Connection,DataAdapter和DataSet对象。\r

Dim Conn As SqlConnection\r

Dim da As SqlDataAdapter\r

Dim ds As DataSet\r

\r

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\r

Try\r

'连接本地SQL Server数据库\r

Conn = New SqlConnection()\r

Conn.ConnectionString = \"Data Source=(local);Initial Catalog=Sales;Integrated Security=false;User ID=sa;Password=sa123\"\r

Conn.Open()\r

da = New SqlDataAdapter(TextBox1.Text, Conn)\r

ds = New DataSet()\r

da.Fill(ds, \"mytb1\")\r

DataGridView1.DataSource = ds.Tables(\"mytb1\")\r

Conn.Close()\r

Catch\r

MessageBox.Show(\"数据库连接有错误!\")\r

End Try\r

End Sub\r

End Class\r