2012-04-05 43 views

回答

6

MySQL download page下載ODBC connector

尋找合適的connectionstring超過here

在您的VB6項目中,選擇對Microsoft ActiveX Data Objects 2.8 Library的引用。如果您擁有Windows Vista或Windows 7,則可能還有一個6.0庫。如果您希望程序在Windows XP客戶端上運行,那麼最好使用2.8庫。如果您的Windows 7使用SP 1,則由於SP1中存在兼容性問題,您的程序將無法在規格較低的其他系統上運行。您可以在KB2517589中閱讀有關此錯誤的更多信息。

此代碼應該爲您提供足夠的信息以開始使用ODBC連接器。

Private Sub RunQuery() 
    Dim DBCon As adodb.connection 
    Dim Cmd As adodb.Command 
    Dim Rs As adodb.recordset 
    Dim strName As String 

    'Create a connection to the database 
    Set DBCon = New adodb.connection 
    DBCon.CursorLocation = adUseClient 
    'This is a connectionstring to a local MySQL server 
    DBCon.Open "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase; User=myUsername;Password=myPassword;Option=3;" 

    'Create a new command that will execute the query 
    Set Cmd = New adodb.Command 
    Cmd.ActiveConnection = DBCon 
    Cmd.CommandType = adCmdText 
    'This is your actual MySQL query 
    Cmd.CommandText = "SELECT Name from Customer WHERE ID = 1" 

    'Executes the query-command and puts the result into Rs (recordset) 
    Set Rs = Cmd.Execute 

    'Loop through the results of your recordset until there are no more records 
    Do While Not Rs.eof 
     'Put the value of field 'Name' into string variable 'Name' 
     strName = Rs("Name") 

     'Move to the next record in your resultset 
     Rs.MoveNext 
    Loop 

    'Close your database connection 
    DBCon.Close 

    'Delete all references 
    Set Rs = Nothing 
    Set Cmd = Nothing 
    Set DBCon = Nothing 
End Sub 
+0

謝謝你,但它返回我「無法連接到MySQL服務器上......」每次我嘗試連接的時間...我已經檢查了服務器,用戶,並通過 - 一切是正確的 – f1nn 2012-04-05 10:23:56

+0

順便說一句,當然我使用連接字符串進行遠程訪問 – f1nn 2012-04-05 10:24:46

+0

什麼是完整的錯誤信息? – Martin 2012-04-05 11:14:37