Visual Basic连接 mysql,适用于Visual Basic 6.0的MySQL示例-读/写

I'd like to find a simple example of working with remote MySQL base. I know, there are some tutorial over the internet, explaining how to set up ADODB.Connection and connectionstrings, but I couldnt make it work. Thanks for any help!

解决方案

Download the ODBC connector from the MySQL download page.

Look for the right connectionstring over here.

In your VB6 project select the reference to Microsoft ActiveX Data Objects 2.8 Library. It's possible that you have a 6.0 library too if you have Windows Vista or Windows 7. If you want your program to run on Windows XP clients too than your better off with the 2.8 library. If you have Windows 7 with SP 1 than your program will never run on any other system with lower specs due to a compatibility bug in SP1. You can read more about this bug in KB2517589.

This code should give you enough information to get started with the ODBC connector.

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值