日期:2014-05-16  浏览次数:20714 次

用VB程序导入与导出MySQL数据的问题(急)
怎样在VB中编写程序,实现MySQL数据的导入,导出?

谢谢!

------解决方案--------------------
我也正在做这样的一个Project, 用VB.Net和MySQL,暂时使用的是MySQL的存储过程实现数据输入输出,遇到了很大的问题,总是提示subquery 返回值为多行,搞的好多function 都不工作,正痛苦的研究中......
------解决方案--------------------
下载myodbc后安装
示例程序
Option Explicit

Public Function openMYSqlDB(c As Connection, ByVal userName As String, Optional password As String) As Boolean
On Error GoTo errorHandler
Dim p As String
c.Open "DRIVER={MyODBC Driver};SERVER=localhost;PORT=;DATABASE=test;USER= " & userName & ";PASSWORD= " & password

openMYSqlDB = True
Exit Function
errorHandler:
MsgBox Err.Description, vbCritical, "打开数据库错误 "
End
End Function

Private Sub Command1_Click()
Dim Conn As New ADODB.Connection
Dim Rs As Recordset
Dim Sql As String
Dim Rc As String
If openMYSqlDB(Conn, "root ") = True Then
Sql = "select length(name) as l,name,address from mytable where name= ' " & "张三 ' "
Set Rs = Conn.Execute(Sql)
Do While Not Rs.EOF And Not Rs.BOF
Rc = Rc & "l= " & Rs.Fields( "l ").Value & "______ "
Rc = Rc & "Name= " & Rs.Fields( "name ").Value & "______ "
Rc = Rc & " Address= " & Rs.Fields( "address ").Value & vbCrLf
Rs.MoveNext
Loop
Text1.Text = Rc
End If
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub

Private Sub Command2_Click()
Dim Conn As New ADODB.Connection
'Dim Rs As Recordset
Dim Sql As String
Dim Rc As String
If openMYSqlDB(Conn, "root ") = True Then
Sql = "insert into mytable(name,address) values( '张三 ', 'address here ') "
Conn.Execute Sql
MsgBox "ok "
End If
'Rs.Close
'Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub

Private Sub Command3_Click()
Dim Conn As New ADODB.Connection
'Dim Rs As Recordset
Dim Sql As String
Dim Rc As String
If openMYSqlDB(Conn, "root ") = True Then
Sql = "create table MyTable(id int(3) auto_increment not null primary key, "
Sql = Sql & "name varchar(50), "
Sql = Sql & "address varchar(100) "
Sql = Sql & ") "
Conn.Execute Sql
MsgBox "Create Done "
End If
'Rs.Close
'Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub

Private Sub Command4_Click()
Dim Conn As New ADODB.Connection
'Dim Rs As Recordset
Dim Sql As String
Dim Rc As String
If openMYSqlDB(Conn, "root ") = True Then
Sql = "drop table mytable "
Conn.Execute Sql
MsgBox "Delete done "
End If
'Rs.Close
'Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub

------解决方案--------------------
你为什么不把路径D:\Program Files\MySQL\MySQL Server 5.0\bin\
写道环境变量PATH里去呢?
我的BAT文件 :

csdn.bat