資料庫連線

摘要:資料庫連線

離開學校進入職場之後,連接資料庫有現成的元件可以使用。有時候必須自已寫時便要找很久,現在紀錄起來方便以後找。

 

01   Dim SqlConnectingString As New SqlClient.SqlConnectionStringBuilder
02         SqlConnectingString.DataSource = "ServerName"
03         SqlConnectingString.InitialCatalog = "DataBaseName"
04         SqlConnectingString.UserID = "UserName"
05         SqlConnectingString.Password = "PassWord"
06         SqlConnectingString.ConnectTimeout = 30
07
08         Dim oSqlClient As New SqlClient.SqlConnection
09         oSqlClient.ConnectionString = SqlConnectingString.ConnectionString
10
11         Dim SqlCommand As New SqlClient.SqlCommand()
12         SqlCommand.CommandText = "select * from Table1"
13         SqlCommand.Connection = oSqlClient
14
15         Dim oDap As New SqlClient.SqlDataAdapter
16         Dim ds As New DataSet
17         oDap.SelectCommand = SqlCommand
18         oDap.Fill(ds)
19

 不止Know How 還要Know Why