简体   繁体   English

将 SQL 服务器凭证添加到 ADO 连接

[英]Adding SQL Server Credentials to ADO Connection

I have an Excel workbook that builds a bunch of SQL Update scripts, and then executes them in SQL Server.我有一个 Excel 工作簿,它构建了一堆 SQL 更新脚本,然后在 SQL 服务器中执行它们。

I got assistance with the below VBA script.我得到了以下 VBA 脚本的帮助。 The below works fine if I am running it while logged in as the Admin user Windows.如果我在以管理员用户 Windows 身份登录时运行它,则以下工作正常。 However, when running from a users workstation I run into issues.但是,当从用户工作站运行时,我遇到了问题。

The main issue seems to be the user id and password are incorrect.主要问题似乎是用户名和密码不正确。 I am not sure where on the below I can add the system administrator (sa) user name and password for SQL Server.我不确定在下面哪里可以添加 SQL 服务器的系统管理员 (sa) 用户名和密码。 Please may I get some assistance.请问我可以得到一些帮助。

My code:我的代码:

Sub test()

Const SERVER = "SRV\ServerName"
Const DATABASE = "Test Database"

Dim fso As Object, ts As Object, ar
Dim ws As Worksheet
Dim iLastRow As Long, i As Long
Dim sql As String, timestamp As String
Dim Folder As String, SQLfile As String, LOGfile As String
Dim t0 As String: t0 = Timer

' query file and log filenames
timestamp = Format(Now, "YYYYMMDD_HHMMSS")
Folder = "\\SRV\Test Folder\"
SQLfile = Folder & timestamp & ".sql"
LOGfile = Folder & timestamp & ".log"

Set fso = CreateObject("Scripting.FileSystemObject")

' read data from sheet into array to build sql file
Set ws = ThisWorkbook.Sheets("UDF Update")
iLastRow = ws.Cells(Rows.Count, "N").End(xlUp).Row
If iLastRow = 1 Then
    MsgBox "No data in Column N", vbCritical
    Exit Sub
End If
ar = ws.Range("N2").Resize(iLastRow - 1).Value2

' connect to server and run query


    Dim sConn As String, conn, cmd, n As Long
    sConn = "Provider=SQLOLEDB;Server=" & SERVER & _
            ";Initial Catalog=" & DATABASE & _
            ";Trusted_Connection=yes;"

    ' open log file
    Set ts = fso.CreateTextFile(LOGfile)

    ' make connection
    Set conn = CreateObject("ADODB.Connection")
    conn.Open sConn

    ' execute sql statements
    Set cmd = CreateObject("ADODB.Command")
    With cmd
        .ActiveConnection = conn
        For i = 1 To UBound(ar)
            ts.writeLine ar(i, 1)
            .CommandText = ar(i, 1)
            .Execute
            
    On Error Resume Next
    Next
    End With
    ts.Close
    conn.Close
    MsgBox UBound(ar) & " SQL queries completed (ADODB)", vbInformation, Format(Timer - t0, "0.0 secs")


End Sub

If you use Trusted_Connection=yes , the SQL server accepts/rejects you via Windows authentication.如果您使用Trusted_Connection=yes ,则 SQL 服务器通过 Windows 身份验证接受/拒绝您。 It seems that your admin account is accepted by the server while other accounts are not.服务器似乎接受了您的管理员帐户,而其他帐户则不接受。
Either the other accounts are added to the database server by the database admin or you need to provide credentials and set Trusted_Connection=no (or omit it as that is the defaults)数据库管理员将其他帐户添加到数据库服务器,或者您需要提供凭据并设置Trusted_Connection=no (或省略它,因为这是默认设置)

sConn = "Provider=SQLOLEDB;Server=" & SERVER & _
        ";Initial Catalog=" & DATABASE & _
        ";Trusted_Connection=no" & _
        ";User ID=MyUserID;Password=MyPassword;"

See https://docs.microsoft.com/en-us/sql/ado/guide/appendixes/microsoft-ole-db-provider-for-sql-server?view=sql-server-ver15请参阅https://docs.microsoft.com/en-us/sql/ado/guide/appendixes/microsoft-ole-db-provider-for-sql-server?view=sql-server-ver15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM