2009-07-22 38 views
2

使用VB 6和SQL Server 2000如何編寫適當的SQL連接字符串?

在我的代碼我使用打開的對話框控制選擇數據庫文件,一旦我選擇的數據庫(Dual_ACS.mdf)文件,它會出現在文本框中(路徑和文件名)

Textbox name = 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碼幫助

回答

0

數據源在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 
' ----------------------------------------------------------------------------- 
7

如果你確定它的連接字符串,檢查connectionstrings.com - 它具有適當的格式爲數以百計的連接字符串。這是一個很好的參考。

你確定它是連接字符串嗎?你爲什麼不告訴我們錯誤信息是什麼?你使用的是什麼版本的SQL Server?

+0

+1我需要學會更快地鍵入... – 2009-07-22 06:26:41

11

有一個完整的網站,致力於這個問題:http://www.connectionstrings.com/

+0

+1:不知道這是否會與原來的人的問題恰恰幫助,但它是人們搜索和發現這個問題的正確答案一般來說。 – Beska 2009-07-22 14:31:24