Visual Basic 6.0 的 MySQL 示例 - 读/写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10024474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL Sample for Visual Basic 6.0 - read/write
提问by f1nn
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!
我想找到一个使用远程 MySQL 库的简单示例。我知道,互联网上有一些教程,解释了如何设置 ADODB.Connection 和 connectionstrings,但我无法使其工作。谢谢你的帮助!
回答by Martin
Download the ODBC connector
from the MySQL download page.
下载ODBC connector
从MySQL的下载页面。
Look for the right connectionstring
over here.
connectionstring
在这里找合适的。
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.
在您的 VB6 项目中,选择对Microsoft ActiveX Data Objects 2.8 Library
. 如果您使用的是 Windows Vista 或 Windows 7,则您也可能拥有 6.0 库。如果您希望您的程序也能在 Windows XP 客户端上运行,那么最好使用 2.8 库。如果您使用的是带有 SP 1 的 Windows 7,那么由于 SP1 中的兼容性错误,您的程序将永远不会在任何其他规格较低的系统上运行。您可以在KB2517589 中阅读有关此错误的更多信息。
This code should give you enough information to get started with the ODBC connector.
此代码应该为您提供足够的信息来开始使用 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