如何从 Visual Basic 6 连接到 MySQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7227706/
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
How to connect to MySQL database from Visual Basic 6
提问by Parth Doshi
I am using visual basic 6. I have a button created which when pressed should display all the entries of the table. I am using following code to connect to MySQL database. I have used the Microsoft Remote Data Services as my reference
我使用的是visual basic 6。我创建了一个按钮,按下时应显示表的所有条目。我正在使用以下代码连接到 MySQL 数据库。我使用了 Microsoft 远程数据服务作为我的参考
code:
代码:
Private Sub cmdConnectMySQL_Click()
Dim cnMySql As New rdoConnection
Dim rdoQry As New rdoQuery
Dim rdoRS As rdoResultset
cnMySql.CursorDriver = rdUseOdbc
cnMySql.Connect = "uid=root;pwd=;
server=localhost; driver={MySQL ODBC 3.51 Driver};
database=demo;dsn=;"
cnMySql.EstablishConnection
With rdoQry
.Name = "selectUsers"
.SQL = "select * from user"
.RowsetSize = 1
Set .ActiveConnection = cnMySql
Set rdoRS = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
End With
Do Until rdoRS.EOF
With rdoRS
rdoRS.MoveNext
End With
Loop
rdoRS.Close
cnMySql.Close
End Sub
I am not able to connect to the database. How do I connect?
我无法连接到数据库。如何连接?
采纳答案by Eireash
Can you try it using ADO instead of RDO?
您可以尝试使用 ADO 而不是 RDO 吗?
- Add a reference to the Microsoft ActiveX Data Objects 2.8 Library
- Set up an ODBC DSN to connect to the database
- 添加对 Microsoft ActiveX 数据对象 2.8 库的引用
- 设置 ODBC DSN 以连接到数据库
Then use code something like this
然后使用这样的代码
Dim cnConnection As ADODB.Connection
Dim adorsRecordSet As ADODB.Recordset
Dim sDatabase As String
Dim sSQL As String
sDatabase = "NameOfTheMysqlDSN"
sSQL= "Select * From user"
Set cnConnection = New ADODB.Connection
cnConnection.Open sDatabase
Set adorsRecordSet = New ADODB.Recordset
adorsRecordSet.Open sSQL, cnConnection
Do Until (adorsRecordSet.EOF)
adorsRecordSet.MoveNext
Loop
回答by Raji Hamidu
' the follwoing code inside module and use adodc
Public Myconn As New ADODB.Connection
Public Recset As New ADODB.Recordset
Public SqlStr As String
Public Function Connectdb()
Set Myconn = New ADODB.Connection
Set Recset = New ADODB.Recordset
Myconn.Open "Provider=MSDASQL.1;Persist Security Info=False;Extended Properties='DRIVER=SQL Server Native Client 10.0;SERVER=.\sqlexpress;Trusted_Connection=Yes;APP=Visual Basic;WSID=YOUNGPROGRAMA;DATABASE=StdB;';Initial Catalog=StdB"
End Function
' the following code inside ur form
Private Sub Command1_Click()
Connectdb
SqlStr = "Insert into Logintb values('" + Text1.Text + "', '" + Text2.Text + "' )"
Recset.Open SqlStr, Myconn, adOpenKeyset, adLockOptimistic
MsgBox "New User Added"
Myconn.Close
End Sub
Private Sub Form_Load()
Connectdb
With Form1
.Top = (Screen.Height - .Height) / 2
.Left = (Screen.Width - .Width) / 2
End With
End Sub
'it works
'for verification call Mr. Raji on 08067455933