如何从Visual Basic 6连接到MySQL数据库 [英] How to connect to MySQL database from Visual Basic 6

查看:257
本文介绍了如何从Visual Basic 6连接到MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Basic6。我创建了一个按钮,当按下该按钮时应显示表的所有条目。我正在使用以下代码连接到MySQL数据库。我已使用Microsoft远程数据服务作为参考

I am using visual basic 6. I have a button created which when pressed should display all the entries of the table. I am using following code to connect to MySQL database. I have used the Microsoft Remote Data Services as my reference

代码:

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 

我无法连接到数据库。如何连接?

I am not able to connect to the database. How do I connect?

推荐答案

可以使用ADO而不是RDO尝试吗?

Can you try it using ADO instead of RDO?


  • 添加对Microsoft ActiveX数据对象2.8库的引用

  • 设置ODBC DSN以连接到数据库

然后使用类似这样的代码

Then use code something like this

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

这篇关于如何从Visual Basic 6连接到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆