Visual Basic连接 mysql,如何从Visual Basic 6连接到MySQL数据库

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

code:

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?

解决方案

Can you try it using ADO instead of RDO?

Add a reference to the Microsoft ActiveX Data Objects 2.8 Library

Set up an ODBC DSN to connect to the database

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值