【建立一個資料庫MSSQL連線】
Public Function SCE_GetConn() As ADODB.Connection
Dim Conn As ADODB.Connection
Dim sConnect As String
Dim strSqlInstance As String
Dim strSqlDB As String
Dim strSqlUser As String
Dim strSqlPWD As String
' strSqlInstance = "SERVER_NAME\INSTANCE"
' 如果伺服器只有裝一次 SQL,就只要輸入伺服器名稱
strSqlInstance = "."
strSqlDB = "DBName"
strSqlUser = "sa"
strSqlPWD = "sqlpassword"
sConnect = "Provider=SQLOLEDB"
sConnect = sConnect & ";DATA SOURCE=" & strSqlInstance & ";Initial Catalog=" & strSqlDB
' 如果把 User ID=...;Password=... 改成 "Integrated Security=sspi;",認證方式就變成 AD 登入帳號
sConnect = sConnect & ";User ID=" & strSqlUser & ";Password=" & strSqlPWD & ";"
Set Conn = New ADODB.Connection
Conn.ConnectionString = sConnect
Set SCE_GetConn = Conn
End Function
相關課程:
章老師的電腦小講堂 https://www.facebook.com/ScenicSchool/
※※ 本區做為經常使用之程式碼複製區,提問請至小講堂唷 ※※