vb连接mysql

前端之家收集整理的这篇文章主要介绍了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

原文链接:https://www.f2er.com/vb/258144.html

猜你在找的VB相关文章