mysql vb_vb连接mysql
環境:xp+vb6+mysql-5.6.13-win32+mysql-connector-odbc-5.2.5-win32
一、首先要下載mysql,其次要下載mysql的odbc驅動。
odbc驅動的安裝很簡單,雙擊就可執行。
mysql不用安裝,解壓就行。但它的服務的啟動比較麻煩,需要用到cmd,下面是我電腦上的啟動過程:
1、
Microsoft Windows XP [版本 5.1.2600]
(C) 版權所有 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>cd c:\
C:\>cd mysql-5.6.13-win32
C:\mysql-5.6.13-win32>cd bin
C:\mysql-5.6.13-win32\bin>mysqld
2014-03-25 09:18:18 0 [Warning] TIMESTAMP with implicit DEFAULT value is depreca
ted. Please use --explicit_defaults_for_timestamp server option (see documentati
on for more details).
啟動之后,cmd窗口就卡在那里,如果需要關閉服務,就要新開一個cmd窗口,然后像上面一樣,cd進mysql目錄,執行:
C:\mysql-5.6.13-win32\bin>mysqladmin -u root -p shutdown
Enter password:
C:\mysql-5.6.13-win32\bin>
2、上面這種方法是臨時用的,一般來說還是注冊成服務,執行:
C:\mysql-5.6.13-win32\bin>mysqld --install mysqlabc
Service successfully installed.
C:\mysql-5.6.13-win32\bin>net start mysqlabc
mysqlabc 服務正在啟動 ..
mysqlabc 服務已經啟動成功。
C:\mysql-5.6.13-win32\bin>
另外,停止服務是net stop mysqlabc,卸載服務是mysqld --remove mysqlabc。mysqlabc是服務名,你可以起其他的名字。
在cmd窗口里面調試sql語句,執行:
C:\mysql-5.6.13-win32\bin>mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.13 MySQL Community Server (GPL)
Copyright (c) 2000,2013,Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database abc character set gb2312;
Query OK,1 row affected (0.03 sec)
mysql> \q
Bye
C:\mysql-5.6.13-win32\bin>
二、當安裝了驅動,啟動了服務之后,就可以開始代碼工作:
引用Microsoft ActiveX Data Objects 2.0 Library
引用Microsoft ADO Data Control 6.0 (OLEDB)控件。
添加兩個文本框,兩個命令按鈕,一個Adodc控件。
Option Explicit
Dim m_conPhone As ADODB.Connection
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 Form_Initialize()
If SqlConnect(m_conPhone) Then
m_conPhone.Execute "create database if not exists abc character set gb2312",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
m_conPhone.Close
Set m_conPhone = Nothing
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.CommandType = adCmdText Adodc1.RecordSource = "select * from test" Set Text1.DataSource = Adodc1 Text1.DataField = "name" Set Text2.DataSource = Adodc1 Text2.DataField = "time" Command1.Caption = "增加" Command2.Caption = "刪除" End Sub
總結
以上是生活随笔為你收集整理的mysql vb_vb连接mysql的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机组成原理知识点白中英,计算机组成原
- 下一篇: 一个Java程序猿眼中的前后端分离以及V