vb的ADO控件如何连接操作MYSQL数据库啊

2024-11-20 16:26:12
推荐回答(2个)
回答1:

这要看你是用控件Adodc 还是用面向对象的编程adodb,如果是你只是临时的做一点东西,那么建议你用adodc!这是可视化的直接从部件里面调出来就是了!在设置一下它的属性就可以链接SQL了。建议你用adodb,这样你才会真正的体会面向对象编程的精髓。可以参考上面那么仁兄的!

回答2:

看这么长时间没人回答,我就帮你写一个吧

'创建连接 ,DateBaseName是数据库路径,DateUserID, DatePassword是用户名和密码
Set objConn = DbConnection("MSACCESS", DateBaseName, DateUserID, DatePassword)

'放到模块里面,DbConnection连接数据库函数
Public Function DbConnection(strDatabaseType As String, strDBService As String, Optional strUserID As String, Optional strPassword As String) As adodb.Connection
Dim objDB As New adodb.Connection
Dim strConnectionString As String

'定义Access数据库连接字符串
strConnectionString = "DBQ=" & strDBService
strConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)}; " & strConnectionString

With objDB
.Mode = adModeReadWrite ' 连接模式
.ConnectionTimeout = 10
.CommandTimeout = 5
.CursorLocation = adUseClient
.Open strConnectionString, strUserID, strPassword
End With

Set DbConnection = objDB
End Function