【问题标题】:How to connect to Sql Server 2008 database from Visual Basic 6?如何从 Visual Basic 6 连接到 Sql Server 2008 数据库?
【发布时间】:2011-10-08 10:03:18
【问题描述】:

什么是正确的连接字符串以及从 Visual Basic 6 连接到 Sql Server 2008 数据库的要求是什么?

【问题讨论】:

    标签: sql-server sql-server-2008 connection-string


    【解决方案1】:
    ' Initialize variables.
    Dim cn As New ADODB.Connection
    Dim provStr As String
    
    ' Specify the OLE DB provider.
    cn.Provider = "sqloledb"
    
    ' Specify connection string on Open method.
    ProvStr = "Server=MyServer;Database=northwind;Trusted_Connection=yes"
    cn.Open provStr
    

    仅用于说明 (Ref.):

    Set rsOrders = New Recordset
    rsOrders.Open "Select * from orders", cn
    Do While Not rsOrders.EOF
        '
        ' If the order matches some custom business logic then get the details for
        ' that order, without opening a new connection.
        '
        If SomeBusinessLogic(rsOrders("CustomerID")) Then
            Dim rsDetails As Recordset
            Set rsDetails = New Recordset
            '
            ' Open a new recordset using the same connection. Normally it's not
            ' possible to have two recordsets simultaniously using the same
            ' connection, but MARS makes this possible
            '
            rsDetails.Open "Select sum(quantity * unitprice) as total " & _
                "from [order details] " & _
                "where OrderID=" & rsOrders("OrderID"), _
                cn
            grandTotal = grandTotal + rsDetails("total")
        End If
        rsOrders.MoveNext
    Loop
    
    lblTotalOrders = grandTotal
    

    【讨论】:

      【解决方案2】:

      您将使用ADODB。这里有一些connection string samples

      【讨论】:

        【解决方案3】:
        Public Cnn As New ADODB.Connection
        Cnn.Open Provider=SQLNCLI10;Server=10.1.100.1;Database=DataJualLama;Uid=sa;Pwd=sa;
        

        需要从微软站点安装sql server 2008 native client runtime。

        【讨论】:

        • 如果您发布代码、XML 或数据示例,在文本编辑器中突出显示这些行并单击编辑器上的“代码示例”按钮 ({ })工具栏以很好地格式化和语法突出显示它!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多