2012-04-05 12 views
5

リモートMySQLベースで作業する簡単な例を探したいと思います。私は知っている、ADODB.Connectionとconnectionstringsを設定する方法を説明するインターネット上のチュートリアルがありますが、私はそれを動作させることができませんでした。助けてくれてありがとう!Visual Basic 6.0のMySQLサンプル - 読み書き可能

答えて

6

ODBC connectorMySQL download pageからダウンロードしてください。

connectionstringhere以上に探してください。

VB6プロジェクトでは、Microsoft ActiveX Data Objects 2.8 Libraryへの参照を選択します。 Windows VistaまたはWindows 7を使用している場合は、6.0ライブラリもある可能性があります。あなたのプログラムをWindows XPクライアントで実行するには、2.8ライブラリを使用する方がよいでしょう。 SP 1のWindows 7をお使いの場合、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

+0

ですbtw、確かに私はリモートアクセス用の接続文字列を使用しました – f1nn

+0

完全なエラーメッセージは何ですか? – Martin

関連する問題