我想找一个使用远程MySQL库的简单例子.我知道,互联网上有一些教程,解释了如何设置ADODB.Connection和连接字符串,但我无法使它工作.谢谢你的帮助!
下载ODBC connector从MySQL的下载页面.
connectionstring在这里找对了.
在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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54780 次 |
| 最近记录: |