5
我想找一个使用远程MySQL库的简单示例。我知道,互联网上有一些教程,解释了如何设置ADODB.Connection和连接字符串,但我无法使其工作。谢谢你的帮助!用于Visual Basic 6.0的MySQL示例 - 读/写
我想找一个使用远程MySQL库的简单示例。我知道,互联网上有一些教程,解释了如何设置ADODB.Connection和连接字符串,但我无法使其工作。谢谢你的帮助!用于Visual Basic 6.0的MySQL示例 - 读/写
从MySQL download page下载ODBC connector
。
寻找合适的connectionstring
超过here。
在您的VB6项目中,选择对Microsoft ActiveX Data Objects 2.8 Library
的引用。如果您拥有Windows Vista或Windows 7,则可能还有一个6.0库。如果您希望程序在Windows XP客户端上运行,那么最好使用2.8库。如果您的Windows 7使用SP 1,则由于SP1中存在兼容性问题,您的程序将无法在规格较低的其他系统上运行。您可以在KB2517589中阅读有关此错误的更多信息。
此代码应该为您提供足够的信息以开始使用ODBC连接器。
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
谢谢你,但它返回我“无法连接到MySQL服务器上......”每次我尝试连接的时间...我已经检查了服务器,用户,并通过 - 一切是正确的 – f1nn 2012-04-05 10:23:56
顺便说一句,当然我使用连接字符串进行远程访问 – f1nn 2012-04-05 10:24:46
什么是完整的错误信息? – Martin 2012-04-05 11:14:37