我正在使用visual basic 6.我创建了一个按钮,按下时应显示该表的所有条目.我使用以下代码连接到MySQL数据库.我使用Microsoft远程数据服务作为我的参考
码:
Private Sub cmdConnectMySQL_Click()
Dim cnMySql As New rdoConnection
Dim rdoQry As New rdoQuery
Dim rdoRS As rdoResultset
cnMySql.CursorDriver = rdUseOdbc
cnMySql.Connect = "uid=root;pwd=;
server=localhost; driver={MySQL ODBC 3.51 Driver};
database=demo;dsn=;"
cnMySql.EstablishConnection
With rdoQry
.Name = "selectUsers"
.SQL = "select * from user"
.RowsetSize = 1
Set .ActiveConnection = cnMySql
Set rdoRS = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
End With
Do Until rdoRS.EOF
With rdoRS
rdoRS.MoveNext
End With
Loop
rdoRS.Close
cnMySql.Close
End Sub
我无法连接到数据库.我该如何连接?
解决方法:
你能用ADO代替RDO吗?
>添加对Microsoft ActiveX Data Objects 2.8库的引用
>设置ODBC DSN以连接到数据库
然后使用这样的代码
Dim cnConnection As ADODB.Connection
Dim adorsRecordSet As ADODB.Recordset
Dim sDatabase As String
Dim sSQL As String
sDatabase = "NameOfTheMysqlDSN"
sSQL= "Select * From user"
Set cnConnection = New ADODB.Connection
cnConnection.Open sDatabase
Set adorsRecordSet = New ADODB.Recordset
adorsRecordSet.Open sSQL, cnConnection
Do Until (adorsRecordSet.EOF)
adorsRecordSet.MoveNext
Loop