引用Microsoft ActiveX Data Objects 2.8 Library
引用Microsoft ADO Data Control 6.0 (OLEDB)控件。
添加两个文本框,三个命令按钮,一个Adodc控件。
Option Explicit
Dim m_conPhone As ADODB.Connection
Dim m_rsPhone As ADODB.Recordset
Private Sub Adodc1_MoveComplete(ByVal adReason As ADODB.EventReasonEnum,ByVal pError As ADODB.Error,adStatus As ADODB.EventStatusEnum,ByVal pRecordset As ADODB.Recordset)
If (Not Adodc1.Recordset.BOF) And (Not Adodc1.Recordset.EOF) Then Adodc1.Caption = Adodc1.Recordset.Bookmark
End Sub
Private Sub Command1_Click()
Adodc1.Recordset.AddNew
End Sub
Private Sub Command2_Click()
Adodc1.Recordset.Delete
Adodc1.Refresh
End Sub
Private Sub Command3_Click()
Set m_rsPhone = New ADODB.Recordset
Text1.Text = "drop database if exists abc"
' m_rsPhone.Open Text1.Text,m_conPhone,adOpenDynamic,adLockBatchOptimistic
m_rsPhone.Open Text1,adLockBatchOptimistic
Set m_rsPhone = Nothing
Unload Me
End Sub
Private Sub Form_Initialize()
If sqlConnect(m_conPhone) Then
' m_conPhone.Execute "create database if not exists abc character set gb2312",adExecuteNoRecords
m_conPhone.Execute "create database if not exists abc",adExecuteNoRecords
m_conPhone.Execute "use abc",adExecuteNoRecords
m_conPhone.Execute "CREATE TABLE IF NOT EXISTS " & "test" & "( name varchar(20) primary key," _
& "time datetime)",adExecuteNoRecords
m_conPhone.Execute "insert ignore into test values ('名字','2014-3-1')",adExecuteNoRecords
Else
Set m_conPhone = Nothing
Set Form1 = Nothing
End If
End Sub
Private Function sqlConnect(Connect As ADODB.Connection) As Boolean
On Error GoTo errconnect
Set Connect = New ADODB.Connection
Connect.ConnectionString = "DRIVER={MysqL ODBC 5.2 Ansi Driver};" _
& "SERVER=localhost;" _
& "UID=root;PWD=; OPTION=3;"
Connect.Open
sqlConnect = True
Exit Function
errconnect:
Dim strErr As String
If Err.Number = -2147467259 Then
strErr = "可能MysqL服务没有开启。"
End If
Debug.Print Err.Number & "," & strErr & Err.Description
sqlConnect = False
End Function
Private Sub Form_Load()
Me.Show
' Adodc1.ConnectionString = "Provider=MSDAsql.1;Persist Security Info=False;User ID=root;Extended Properties=" & _
Chr(34) & "DRIVER={MysqL ODBC 5.2 Ansi Driver};SERVER=localhost; OPTION=3;DATABASE=abc" & Chr(34)
Adodc1.ConnectionString = "Provider=MSDAsql.1;Persist Security Info=False;User ID=root;Extended Properties=" & _
Chr(34) & "DRIVER={MysqL ODBC 5.2 Ansi Driver};SERVER=localhost; OPTION=3" & Chr(34)
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "select * from test"
' Set Text1.DataSource = Adodc1
Text1.DataSource = Adodc1
Text1.DataField = "name"
Set Text2.DataSource = Adodc1
Text2.DataField = "time"
Command1.Caption = "增加"
Command2.Caption = "删除"
Command3.Caption = "删除数据库"
End Sub
Private Sub Form_Unload(Cancel As Integer)
m_conPhone.Close
Set m_conPhone = Nothing
End Sub
运行的话,应该会出现下面5个错误,错误语句的修改可以对比它上面的注释语句来修改。
1、编译错误:过程声明与同名事件或过程的描述不匹配
原因:Microsoft ActiveX Data Objects 2.8 Library与Microsoft ADO Data Control 6.0 (OLEDB)控件冲突。
解决办法:取消引用Microsoft ActiveX Data Objects 2.8 Library,改为引用Microsoft ActiveX Data Objects 2.0 Library
2、编译错误:未找到方法或数据成员。
原因:
解决办法:在过程Form_Load里,把“Text1.DataSource = Adodc1”改为“Set Text1.DataSource = Adodc1”。
3、[MysqL][ODBC 5.2(a) Driver][MysqLd-5.6.13]No database selected
原因:Adodc1.ConnectionString没有选择数据库
解决办法:在过程Form_Load里,为Adodc1.ConnectionString添加“;DATABASE=abc”。
4、点击删除数据库,提示实时错误 '3001':参数类型不正确,或不在可以接受的范围之内,或与其他参数冲突。
原因:Text1不等于Text1.Text。
解决办法:在过程Command3_Click里,把“m_rsPhone.Open Text1,...”改成“m_rsPhone.Open Text1.Text,...”。
5、文本框显示乱码。 原因:创建数据库的时候,没有选择字符集。 解决办法:先执行删除数据库。然后修改代码,在过程Form_Initialize里,为m_conPhone.Execute "create database..."添加“ character set gb2312”。