#1. Download MySQL Connector
-Go to address:Http://www.mysql.com/products/connector/
-Download ODBC ctor Based on the MySQL platform version (My MySQL is 32-bit, although my operating system is 64-bit, so I downloaded it:Mysql-connector-odbc-5.2.5-win32.msi)
#2. Install MySQL connector and find its driver string for future use
-My connection driver is:MySQL ODBC 5.2 Unicode driver
#3. Data Table Structure in the database (my example)
Create Table if not exists 'mydb' ('username' varchar (30) not null, 'Password' varchar (30) not null, 'age' int (11) not null, primary Key ('username') engine = MyISAM default charset = Latin1;
Insert two data records:
Insert into 'mydb' ('username', 'Password', 'age') values ('xiwang', '000000', 27), ('rizhou', '000000 ', 26 );
#4. Use the following test Code (Save Testdb. vbs )
Sub maindim dbset DB = new mysqldbdb. connect "test", "root", "xiwang", "localhost", 3306if err thenmsgbox err. descriptiondb. unconnectexit subend ifdb. q ("select * From 'mydb'") if err thenmsgbox err. descriptiondb. unconnectexit subend ifdb. unconnectif dB. data (0)> 0 thenmsgbox "Row count:" & dB. data (0) msgbox "1st row: username =" & dB. data (1) ("username") & ", password =" & dB. data (1) ("password") msgbox "2nd row: username =" & dB. data (2) ("username") & ", password =" & dB. data (2) ("password") end ifset DB = nothingend subcall main ''c: \ windows \ syswow64 \ odbcad32.exe ''c: \ windows \ system32 \ odbcad32.execlass mysqldb ''' current error code: '''-1'-2'-2'-3 private oconnprivate orecset ''' data (Row-index) ("column-name ") = 'cell value' public data () private sub class_initializeend subprivate sub class_terminateunconnecterase dataset dicterrdef = nothingmsgbox "clean works complete. "End sub ''' public methods ''' public sub connect (dB, uid, PWD, host, Port) on error resume nexterr. cleardim sconnstr '''create connection stringsconnstr = "driver = {MySQL ODBC 5.2 Unicode driver};" 'sconnstr = "driver = {MySQL ODBC 5.2 ANSI driver }; "sconnstr = sconnstr &" database = "& dB &"; "sconnstr = sconnstr &" user = "& uid &"; "sconnstr = sconnstr &" Password = "& PWD &"; "sconnstr = sconnstr &" Server = "& host &"; "sconnstr = sconnstr &" Port = "& Port &"; "sconnstr = sconnstr &" option = 3; "'''create connectionset oconn = Createobject (" ADODB. connection ") oconn. open sconnstrif err or oconn. state = 0 thenerr. raise vbobjecterror + 1 exit subend ifmsgbox "connection created. "End sub '''query'return: '-data (Row-index) (" column name ") public function q (SQL) on error resume nexterr. cleardim dictrowdatadim irowindexdim I, j '''clean dataerase dat''' create recordsetset orecset = Createobject ("ADODB. recordset ") orecset. cursorlocation = 3orecset. open SQL, oconnif err thenredim preserve data (0) data (0) = 0 exit functionelseif orecset. recordcount = 0 or orecset. recordcount =-1 thenredim preserve data (0) data (0) = 0 exit functionend ifredim preserve data (orecset. recordcount) data (0) = orecset. recordcount ''' cursor toporecset. movefirstirowindex = 1''' cursor loop (as row) do while not orecset. EOF ''will free dictrowdata when erase data: arrayset dictrowdata = Createobject (" scripting. dictionary ") for I = 0 to orecset. fields. count-1 dictrowdata (orecset. fields (I ). name) = orecset. fields (I ). valuenextredim preserve data (irowindex) Set Data (irowindex) = dictrowdatairowindex = irowindex + 1orecset. movenextlooporecset. closeset orecset = nothingend functionpublic sub unconnect () If oconn is nothing thenexit subend ifoconn. closeset oconn = nothingend subend class
#5. Running results
The following dialog box appears:
-Connection created.
-Row count: 2
-1st row: username = xiwang, password = 123456
-2nd row: username = rizhou, password = 789
-Clean works complete.
----
Have fun ^_^
----