2012-04-05 12 views
5

मैं दूरस्थ MySQL बेस के साथ काम करने का एक सरल उदाहरण ढूंढना चाहता हूं। मुझे पता है, इंटरनेट पर कुछ ट्यूटोरियल हैं, यह बताते हुए कि ADODB.Connection और कनेक्शनस्ट्रिंग कैसे सेट अप करें, लेकिन मैं इसे काम नहीं कर सका। किसी भी मदद के लिए धन्यवाद!विजुअल बेसिक 6.0 के लिए MySQL नमूना - पढ़ें/लिखें

उत्तर

6

MySQL download page से ODBC connector डाउनलोड करें।

connectionstringhere से दाएं connectionstring देखें।

अपनी वीबी 6 परियोजना में Microsoft ActiveX Data Objects 2.8 Library का संदर्भ चुनें। यह संभव है कि आपके पास 6.0 लाइब्रेरी भी हो, यदि आपके पास Windows Vista या Windows 7 है। यदि आप चाहते हैं कि आपका प्रोग्राम 2.8 लाइब्रेरी के साथ आपके बेहतर बंद होने से भी Windows XP क्लाइंट पर चलाना चाहे। यदि आपके पास प्रोग्राम के मुकाबले एसपी 1 के साथ विंडोज 7 है तो एसपी 1 में संगतता बग के कारण कम चश्मा वाले किसी अन्य सिस्टम पर कभी भी नहीं चलेंगे। आप KB2517589 में इस बग के बारे में अधिक पढ़ सकते हैं।

यह कोड आपको ओडीबीसी कनेक्टर के साथ शुरू करने के लिए पर्याप्त जानकारी देनी चाहिए।

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

बीटीडब्ल्यू, निश्चित रूप से मैंने रिमोट एक्सेस – f1nn

+0

के लिए कनेक्शन स्ट्रिंग का उपयोग किया है पूर्ण त्रुटि संदेश क्या है? सर्वर = mysql.mtgrand; ड्राइवर = {MySQL ODBC 5.1 ड्राइवर}: http://tinyurl.com/mysqlerror-12 आप अपने आप को कोशिश कर सकते हैं, यहाँ coneection स्ट्रिंग मैं का उपयोग करें: – Martin

संबंधित मुद्दे