2019-11-26

〔VBA〕ADODB.Connection-建立資料庫連線--v039

【建立一個資料庫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/
※※ 本區做為經常使用之程式碼複製區,提問請至小講堂唷 ※※

5 章老師的電腦小講堂: 〔VBA〕ADODB.Connection-建立資料庫連線--v039 【建立一個資料庫MSSQL連線】 Public Function SCE_GetConn() As ADODB.Connection     Dim Conn As ADODB.Connection     Dim sConnect As String     Dim...
< >
小講堂經過多次搬移,舊文章連結及內容較難整理~
大家可以新文章為主~
每篇文章後有代碼,任何文章問題可至FB小講堂用代碼提問唷~