【问题标题】:Connect to a SQL Server database with Outlook使用 Outlook 连接到 SQL Server 数据库
【发布时间】:2015-11-04 23:27:20
【问题描述】:

我想使用 Outlook 宏连接到 MS SQL Server 数据库。但我不知道是代码错误还是我需要添加库/驱动程序或这里发生了什么但它不起作用。

Private Sub Application_Startup()
On Error GoTo ExitHere
    'adodb connection to other database
    stg_cn.Open "Provider = SQLOLEDB;" & _
                        "Data Source = 192.168.100.100;" & _
                        "Initial Catalog = hugeDB;" & _
                        "Integrated Security=SSPI;" & _
                        "User ID = oneuser;" & _
                        "Password = onepassword;"

    sQuery = "SELECT * FROM documents where location = 'IE'"

    'set reference to query
    Set cmd = New ADODB.Command
        cmd.ActiveConnection = stg_cn
        cmd.CommandType = adCmdText
        cmd.CommandText = sQuery
    Set rs = cmd.Execute
    Do While Not rs.EOF
        For i = 0 To rs.Fields.count - 1
            MsgBox (i + 1)
        Next
        rs.MoveNext
    Loop
ExitHere:
    If Not stg_cn Is Nothing Then stg_cn.Close
    Set rs = Nothing
    Set stg_cn = Nothing
    Exit Sub

End Sub

【问题讨论】:

  • 错误/问题是什么?你没有给我们太多的继续。
  • 您说的是 Outlook 宏,但代码以 Workbook_Open 开头,即 Excel。如果该过程在 Outlook 中,是否应将其称为 Application_Startup
  • 我改了,还是不行……难道是我连接数据库的方式应该不一样?

标签: sql sql-server vba outlook


【解决方案1】:

在视力测试中,我无法找出问题所在,我认为这与您执行 ADO 操作的方式有关。

但是我只是把我写的最后一个宏从宏连接到 SQL-Server。希望能帮助到你。

Private Sub Workbook_Open()
On Error GoTo ErrorHandler
    '**************************************Initialize Variables**************************************
    sServer = "<SQL SERVER Server>"
    sDBName = "<SQL SERVER DB>"

    '**************************************Open Connection**************************************
    'adodb connection to other database
    stg_cn.Open "Provider=SQLOLEDB;Data Source=" & sServer & _
                  ";Initial Catalog=" & sDBName & _
                  ";Integrated Security=SSPI;"

    sQuery = "SELECT * " & _
             "FROM Table "

    'set reference to query
    Set cmd = New ADODB.Command
        cmd.ActiveConnection = stg_cn
        cmd.CommandType = adCmdText
        cmd.CommandText = sQuery
    Set rs = cmd.Execute
    Do While Not rs.EOF
        For i = 0 To rs.Fields.Count - 1
            <PERFORM OPERATIONS>
        Next
        rs.MoveNext
    Loop

ExitHere:
    If Not stg_cn Is Nothing Then stg_cn.Close
    Set rs = Nothing
    Set stg_cn = Nothing
    Exit Sub

End Sub

【讨论】:

    【解决方案2】:

    @CodePhobia 提供的连接字符串应该适合你。

    下面仅包括用户 ID 和密码功能,因为您的原始问题显示尝试使用此连接。

    Dim rsConn as ADODB.Connection
    Set rsConn = New ADODB.Connection
    With rsConn
        .ConnectionString = "Provider = sqloledb;" & _
                            "Data Source = myServerName;" & _
                            "Initial Catalog = myCatalog;" & _
                            "Integrated Security=SSPI;" & _
                            "User ID = myUserID;" & _
                            "Password = myPassword;"
        .Open
    End With
    

    以后可以使用this website 查找连接字符串。它应该涵盖您希望建立的所有可能的连接。

    【讨论】:

    • ...无法访问数据库...我很确定 IP、表、用户名和密码是正确的。 Conn.connectionString = "提供者 = sqloledb;" &amp; _ "数据源 = hugeDB;" &amp; _ "集成安全=SSPI;" &amp; _ "用户 ID = hdbmread;" &amp; _ "密码 = hdbmreadonly;" Conn.Open Set Rst.ActiveConnection = Conn Rst.Open "select COUNT(*) from Nav n" &amp; _ "group by n.Nav_Date" rs.Close Set rs = Nothing CN.Close Set CN = Nothing End Sub
    • 您的连接字符串中是否包含"Initial Catalog = myCatalog;" &amp; _?这是您要连接到的数据库中的目录。
    • Conn.connectionString = "提供者 = sqloledb;" & _ "数据源 = 192.168.100.100;" & _ "初始目录=" & hugeDB & _ "集成安全=SSPI;" & _ "用户 ID = hdbmread;" & _ "密码 = hdbmreadread;"还是同样的问题。它无法从 Outlook 连接,但我可以通过 Microsoft SQL Server Management Studio 进行连接,因此不是权限问题……我在这里被阻止了,应该很容易。
    • Data Source 应该是服务器名称;您的服务器名称是 IP 地址吗?
    • 在我的宏中我有服务器名称。在这里我添加了 IP,但只是因为我不想使用我们在公司中使用的相同内部名称。但是,是的,我使用的是服务器名称。
    猜你喜欢
    • 2019-09-16
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    相关资源
    最近更新 更多