【问题标题】:How to write a Proper SQL Connection String?如何编写正确的 SQL 连接字符串?
【发布时间】:2009-07-22 06:21:54
【问题描述】:

使用 VB 6 和 Sql Server 2000。

在我的代码中,我使用打开对话框控件来选择数据库文件,一旦我选择了数据库(Dual_ACS.mdf)文件,它将出现在文本框中(路径和文件名)

文本框名称 = 数据库文本

我的代码。

Cn.ConnectionString = "Provider=SQLOLEDB.1; Persist Security Info=False;User ID=" & UName & ";Password=" & PWord & ";InitialCatalog=DUAL_ ACS; Data Source=" & databasetext & ""
Cn.Open

但它显示错误。如何编写正确的 SQL 连接字符串?

需要 VB 6 代码帮助

【问题讨论】:

    标签: sql-server vb6 connection-string


    【解决方案1】:

    有一个专门讨论这个主题的完整网站:http://www.connectionstrings.com/

    【讨论】:

    • +1:不知道它是否会准确地帮助原始人的问题,但对于搜索和查找此问题的人来说,它是正确的答案。
    【解决方案2】:

    如果您确定它是连接字符串,请检查connectionstrings.com - 它具有数百个连接字符串的正确格式。这是一个很好的参考。

    你确定这是连接字符串吗?你为什么不告诉我们错误信息是什么?您使用的是什么版本的 SQL Server?

    【讨论】:

      【解决方案3】:

      SQLOLEDB 连接中的数据源需要指向 SQL Server 服务器和实例名称(或 IP,但您需要指定端口)。有关详细信息,请参阅here。 您不能直接指向 mdf 文件。这不是访问权限。

      如果您希望用户能够选择数据源,您应该使用 ADO 内置的通用数据链接对话框。这是从我使用的一个更大的类中提取的一个示例。您需要添加对“Microsoft OLE DB 服务组件 1.0 类型库”或 C:\Program Files\Common Files\system\ole db\oledb32.dll 的引用。 如果您只需要自己查看连接字符串的外观,只需创建一个名为 test.udl 的文件,然后双击它。此处使用的接口与此代码中调用的接口相同。

      ' -----------------------------------------------------------------------------
      ' Edit
      '
      ' Description:
      '    Edits the udl
      '
      ' Arguments:
      '
      ' Dependencies:
      '    MSDASC.DataLinks.PromptEdit
      '    MSDASC.DataLinks.PromptNew
      '
      ' History:
      ' 05/23/2003 - WSR : Created
      '
      Public Function Edit() As Long
      
      Dim dlgEdit       As MSDASC.DataLinks
      Dim strConnection As String
      
         Set dlgEdit = New MSDASC.DataLinks
      
         ' if there is a connection string
         If Len(m_conSource.ConnectionString) > 0 Then
      
            ' prompt user to edit the connection
            If Not dlgEdit.PromptEdit(m_conSource) Then
      
               ' if they didn't edit the connection string
               ' return error code
               Edit = -1
      
            End If
      
         ' if there is no connection string
         Else
      
            ' prompt user to create new connection
            On Error Resume Next
            strConnection = dlgEdit.PromptNew()
      
            ' if there was a connection string created
            If Len(strConnection) > 0 Then
      
               ' use it
               m_conSource.ConnectionString = strConnection
      
            ' if there was no connection string created
            Else
      
               ' return error code
               Edit = -1
      
            End If
      
         End If
      
         Set dlgEdit = Nothing
      
      End Function
      ' -----------------------------------------------------------------------------
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 2015-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多