VB 读取和修改 外网MYSQL数据库的方法,望大侠们提供帮助。
小弟我新手,只写过单机版的VB程序。现想写一个多用户连接远程外网数据库的程序,数据库是放在西部数码的虚拟主机上的,且只支持MYSQL。毫无思路,不知道从何下手远程连接和修改MYSQL数据库,望大侠们不吝赐教,提供思路能提供参考代码。不胜感激! 数据库 mysql vb 远程连接 --------------------编程问答-------------------- 差不多吧,只是把数据库连接部分修改下,IP改到远程地址…… --------------------编程问答-------------------- 就是 ConnectionString 中的 Server=这里改为虚拟机的ip --------------------编程问答-------------------- 西部数码的虚拟主机ip地址是经常变动呢 --------------------编程问答-------------------- 那就填写动态域名呗…… --------------------编程问答-------------------- 在上面架个IIS好了。通过ASP页来操作。 --------------------编程问答-------------------- 给你贴完整代码,已经测试过,肯定行,一定要给分哦!自己好好研究一下,肯定行哦!三个窗体,MDIForm、frmConnect、frmSQLViewer和一个模块
你要根据代码中的涉及到的控件而添加哦!
frmConnect窗体:
窗口添加5个textbox控件,按照下面代码中的名称改为相应名称,用途是填写服务器IP、用户名、密码和数据库名称的。别忘了下载一个MySQL ODBC 3.51,安装上,否则无驱动不行。
Option Explicit
Private Sub cmdCancel_Click()
'取消
Unload Me
End Sub
Private Sub cmdPing_Click()
'测试
Dim msg As String
If IsValid = True Then
ConSTR = BuildSTR
DisableControls
If Connect = True Then
MsgBox "服务器和数据库完整,测试通过!", vbInformation, "状态"
Else
msg = "查询超时... 连接失败..." & vbCr & "可以有以下错误 :" & vbCr & vbCr & "1. 服务器或者数据库不存在..." & vbCr & "2. 用户名不存在" & vbCr & "3. 密码或者用户名错误"
MsgBox msg, vbCritical, "信息"
End If
End If
EnableControls
End Sub
Private Sub cmdView_Click()
'显示字符串
ConSTR = BuildSTR
MsgBox ConSTR, vbInformation, "字符串"
End Sub
Private Sub Form_Load()
Me.Move (Screen.Width - Me.Width) / 3, (Screen.Height - Me.Height) / 4
End Sub
Private Sub cmdOpen_Click()
'打开
Dim msg As String
If IsValid = True Then
ConSTR = BuildSTR
DisableControls
If Connect = True Then
frmSQLViewer.Show
Unload Me
Else
msg = "查询超时... 连接失败..." & vbCr & "可以有以下错误 :" & vbCr & vbCr & "1. 服务器或者数据库不存在..." & vbCr & "2. 用户名不存在" & vbCr & "3. 密码或者用户名错误"
MsgBox msg, vbCritical, "信息"
EnableControls
End If
End If
End Sub
Private Function IsValid() As Boolean
If txtDatabase = "" Then
MsgBox "未选择数据库", vbInformation + vbOKOnly, "参数错误"
IsValid = False
Exit Function
End If
If txtServer = "" Then
MsgBox "未选择服务器", vbInformation + vbOKOnly, "参数错误"
IsValid = False
Exit Function
End If
IsValid = True
End Function
Public Function BuildSTR() As String
' 建立连接字符串
'连接参数
'BuildSTR = BuildSTR & ";Persist Security Info=False;Initial Catalog=" & txtDatabase & ";Data Source=" & txtServer
BuildSTR = "DRIVER={MySQL ODBC 3.51 Driver}; " _
& " Password=" & txtPassword & "; " _
& " Persist Security Info=False; " _
& " User ID=" & txtUser & "; " _
& " Server=" & txtServer & ";" _
& " DataBase=" & txtDatabase & "; " _
& " OPTION=3;stmt=SET NAMES GB2312"
End Function
Private Sub EnableControls()
txtServer.Enabled = True
txtDatabase.Enabled = True
cmdView.Enabled = True
cmdPing.Enabled = True
cmdOpen.Enabled = True
cmdCancel.Enabled = True
End Sub
Private Sub DisableControls()
txtServer.Enabled = False
txtDatabase.Enabled = False
cmdView.Enabled = False
cmdPing.Enabled = False
cmdOpen.Enabled = False
cmdCancel.Enabled = False
End Sub
frmSQLViewer窗体:
Option Explicit
Dim CTN As ADODB.Connection
Dim RSTable As ADODB.Recordset
Dim RSRc As ADODB.Recordset
Private Sub cboTables_Click()
'选择表
On Error GoTo errorhandler
adoSQL.ConnectionString = ConSTR
adoSQL.RecordSource = "SELECT * FROM " & cboTables.Text
adoSQL.Refresh
FillcboFields
Exit Sub
errorhandler:
MsgBox Error$
End Sub
Private Sub cmdAdd_Click()
'增加
On Error GoTo cmdAdd_Click_ErrHandler
adoSQL.Recordset.AddNew
Exit Sub
cmdAdd_Click_ErrHandler:
MsgBox Err.Description & vbCr & vbCr & "(错误号 #" & Err.Number & ")", , "frmSQLViewer窗体错误: Sub cmdAdd_Click"
End Sub
Private Sub cmdDelete_Click()
'删除
On Error GoTo cmdDelete_Click_ErrHandler
adoSQL.Recordset.Delete
Exit Sub
cmdDelete_Click_ErrHandler:
MsgBox Err.Description & vbCr & vbCr & "(错误号 #" & Err.Number & ")", , "frmSQLViewer窗体错误: Sub cmdDelete_Click"
End Sub
Private Sub cmdUpdate_Click()
'更新
On Error GoTo cmdUpdate_Click_ErrHandler
adoSQL.Recordset.Update
Exit Sub
cmdUpdate_Click_ErrHandler:
MsgBox Err.Description & vbCr & vbCr & "(错误号 #" & Err.Number & ")", , "frmSQLViewer窗体错误: Sub cmdUpdate_Click"
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContextID
End Sub
Private Sub cmdRefresh_Click()
'刷新
On Error GoTo cmdRefresh_Click_ErrHandler
adoSQL.Recordset.Requery
Exit Sub
cmdRefresh_Click_ErrHandler:
MsgBox Err.Description & vbCr & vbCr & "(错误号 #" & Err.Number & ")", , "frmSQLViewer窗体错误: Sub cmdRefresh_Click"
End Sub
Private Sub Form_Load()
fraWorkSpace.Caption = "数据库名称 - " & frmConnect.txtDatabase.Text
cboTables.Clear
GetTable
End Sub
Private Sub GetTable()
Dim X As Integer
Dim CurTable As String
Set CTN = New ADODB.Connection
Set RSTable = New ADODB.Recordset
'打开连接
CTN.ConnectionString = ConSTR
CTN.Open
Set RSTable = CTN.OpenSchema(adSchemaTables)
RSTable.MoveFirst
Do Until RSTable.EOF
If RSTable.Fields("TABLE_TYPE") = "TABLE" Then
cboTables.AddItem RSTable.Fields("TABLE_NAME")
End If
RSTable.MoveNext
Loop
RSTable.Close
Set RSTable = Nothing
'关闭连接
CTN.Close
Set CTN = Nothing
If cboTables.ListCount <> 0 Then cboTables.ListIndex = 0
Exit Sub
errorhandler:
MsgBox Error$
End Sub
Private Sub FillcboFields()
Dim X As Integer
Dim CTable As String
cboFields.Clear
CTable = cboTables.Text
Set CTN = New ADODB.Connection
Set RSTable = New ADODB.Recordset
CTN.ConnectionString = ConSTR
CTN.Open
Set RSRc = New ADODB.Recordset
RSRc.ActiveConnection = CTN
RSRc.Open CTable, , , , adCmdTable
For X = 0 To RSRc.Fields.Count - 1
If RSRc.Fields(X).Name <> "ID" Then
cboFields.AddItem RSRc.Fields(X).Name
End If
Next X
RSRc.Close
Set RSRc = Nothing
CTN.Close
Set CTN = Nothing
If cboFields.ListCount <> 0 Then cboFields.ListIndex = 0
End Sub
模块内容:
Option Explicit
Global ConSTR As String '连接字符串
Sub Main()
frmMDISQL.Show
End Sub
Public Function Connect() As Boolean
On Error GoTo errorhandler
Dim SQLServer As ADODB.Connection
Set SQLServer = New ADODB.Connection
SQLServer.ConnectionString = ConSTR
SQLServer.Open
If SQLServer.State = adStateOpen Then
Connect = True
Else
Connect = False
End If
SQLServer.Close
Exit Function
errorhandler:
MsgBox Error$
Connect = False
End Function
补充:VB , 数据库(包含打印,安装,报表)