如何编写正确的SQL连接字符串?

3

使用VB 6和Sql Server 2000。

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

文本框名称 = databasetext

我的代码。

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代码帮助

3个回答

11

+1:不知道它是否能够精确地帮助原问题的提出者,但对于一般搜索和找到这个问题的人来说,这是正确的答案。 - Beska

7
如果您确定是连接字符串的问题,请查看connectionstrings.com,它具有数百种连接字符串的正确格式。 这是一个很好的参考资料。
但是您确定是连接字符串的问题吗? 为什么不告诉我们错误消息是什么? 您正在使用哪个版本的SQL Server?

0

在 SQLOLEDB 连接中,数据源需要指向 SQL Server 服务器和实例名称(或 IP,但是您需要指定端口)。请参见此处以获取详细信息。 您不能直接指向 mdf 文件。这不是访问数据库。

如果您希望用户能够选择数据源,则应使用内置于 ADO 的通用数据链接对话框。以下是我使用的较大类中提取的示例。您需要添加对“Microsoft OLE DB Service Component 1.0 Type Library”或 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
' -----------------------------------------------------------------------------

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接