以程式設計方式建立應用程式和 SQL Server 資料庫間的連接
以下程式碼建立 SqlConnection 物件、設定 SqlConnection.ConnectionString 屬性並開啟連接。
' Visual BasicPublic Sub ConnectToSql()
Dim conn
As New SqlClient.SqlConnection
' TODO: Modify the connection string and include any ' additional required properties for your database. conn.ConnectionString = & _
"integrated security=SSPI;data source=SQL Server Name;" & _
"persist security info=False;initial catalog=northwind"
Try conn.
Open()
' Insert code to process data. Catch ex
As Exception MessageBox.Show("Failed to connect to data source")
Finally conn.
Close()
End TryEnd Sub你有裝VS2003的MSDN嗎?如果有的話在VS裡搜尋SQL可以找到上面這段~