vb用mysql数据库
Ⅰ 【80分悬赏】怎么用VB连接MYsql数据库来编写登陆界面
/在模块里编写(新建模块)
Public comm As New ADODB.Command
Public conn As New ADODB.Connection
Public uname As String
Public pwd As String
Public ulevel As Integer
Sub main()
conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=wow;Data Source=."
conn.Open
Form1.Show
End Sub
'判断登陆是否成功(封装函数)
Public Function isLogin(uname, pwd, ulevel) As Boolean
Dim flag As Boolean
comm.ActiveConnection = conn
comm.CommandType = adCmdStoredProc
comm.CommandText = "isLogin"
comm.Parameters(1).Type = adVarChar
comm.Parameters(1).Size = 50
comm.Parameters(1).Direction = adParamInput
comm.Parameters(1).Value = uname
comm.Parameters(2).Type = adVarChar
comm.Parameters(2).Size = 50
comm.Parameters(2).Direction = adParamInput
comm.Parameters(2).Value = pwd
comm.Parameters(3).Type = adInteger
comm.Parameters(3).Size = 4
comm.Parameters(3).Direction = adParamInput
comm.Parameters(3).Value = ulevel
comm.Parameters(4).Type = adInteger
comm.Parameters(4).Size = 4
comm.Parameters(4).Direction = adParamOutput
comm.Execute
If comm.Parameters(4).Value > 0 Then
flag = True
Else
flag = False
End If
isLogin = flag
End Function
/在登陆窗体中调用封装函数实现登陆唯陆判断
Private Sub Command1_Click()
Dim uname As String
Dim pwd As String
Dim ulevel As Integer
uname = Text1.Text
pwd = Text2.Text
If Combo1.Text = "管理员" Then
ulevel = 1
Else
ulevel = 0
End If
If isLogin(uname, pwd, ulevel) Then
MsgBox "登陆成功", vbOKOnly, "提示"
Form2.Show
Unload Me
Else
MsgBox "登录失败", vbOKOnly, "提示"
End If
End Sub
Private Sub Command2_Click()
Text1.Text = Clear
Text2.Text = Clear
Text1.SetFocus
End Sub
Private Sub Command3_Click()
Unload Me
End Sub
Private Sub Command4_Click()
Form3.Show
Unload Me
End Sub
Private Sub Form_Load()
Combo1.AddItem "管理员"
Combo1.AddItem "用户"
End Sub
/在MYSQL企业管理器中编写存储过程:(选中以下编写氏悉闹的内容并执行歼罩即可生成存储过程)
create proc isLogin @uname varchar(50), @pwd varchar(50),@ulevel int, @flag int output
as
declare @count int
select @count=count(*) from [user] where
uname=@uname and pwd=@pwd and ulevel=@ulevel
if @count>0
set @flag=1
else
set @flag=0
create proc AddUser @uname
给分把
Ⅱ vb的ADO控件如何连接操作MYSQL数据库啊
由于ADO对象不直接支持MySQL,所以必须先安装MyODBC, 后者也是一个免费产品,在 www.mysql.org上有下载,安装好了MyODBC, 就可以在ODBC数据源管理中配置一个数据源名称,把它指向你想连接的MySQL数据库。代码如下:Sub connectMySQL() '通过MyODBC去连接MySQL数据库,并将Microsoft SQL Server 7 '的数据转进mysql中 Dim sConnect As String, sSql As String, i As Long Dim cnMSSQL As New ADODB.Connection Dim cnMySQL As New ADODB.Connection '声明并创建对象 连接 Dim rs As New ADODB.Recordset '声明并创建对象 记录集 Dim cm As New ADODB.Command '声明并创建对象 命令 sConnect = "dsn=mysql1" '指定MySQL的数据源名称 cnMySQL.Open sConnect '连接到 mysql sConnect="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=123456;Initial Catalog=softdown;Data Source=ntserver" '连接到 ms sql server 7 cnMSSQL.Open sConnect 'sSql = "create table softinfo (softNum smallint,softname varchar(70),softdesc blob," & _ "softpath varchar(30),softleng varchar(10),softclass varchar(10),softsugest tinyint(1)," & _ "softdown smallint(4))" '创建新的MySQL数据表语句 sSql = "select * from softinfo order by softnum" rs.Open sSql, cnMSSQL, 1, 1 While Not rs.EOF sSql = "insert into softinfo values (" & Trim(rs(0).Value) & ",'" & Trim(rs(1).Value) & _ "','" & Trim(rs(2).Value) & "','" & Trim(rs(3).Value) & "','" & Trim(rs(4).Value) & _ "','" & Trim(rs(5).Value) & "'," & Trim(rs(6).Value) & "," & Trim(rs(7).Value) & ")" cm.ActiveConnection = cnMySQL cm.CommandType = adCmdText cm.CommandText = sSql cm.Execute rs.MoveNext Wend rs.Close Set rs = Nothing cnMySQL.Close Set cnMySQL = Nothing cnMSSQL.Close Set cnMSSQL = Nothing End Sub
Ⅲ 如何用vb连接mysql数据库
VB连接MYSQL数据的方法
博客分类:
VB
VBMySQLSQLServerSQLSocket
第一步:上网http://dev.mysql.com/downloads/connector/odbc/下载mysql-connector-odbc-5.1.6-win32.msi
第二步:安装mysql-connector-odbc-5.1.6-win32.msi
第三步:运行VB并新建一标准EXE工程, 通过菜单工程->引用 打开”引用”对话框, 找到 MicrosoftActiveX Data Objects x.x Library , 其中 x.x 是版本号, 可能会有很多个, 这里我选择的是2.5
第四步:打开代码窗口, 在 Form_Load 过程中输入下面的代码(具体说明在代码的注释中):
Vb代码
' 定义并创建数据库连接和访问对象
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
' 定义数据库连接字符串变量
Dim strCn As String
' 定义数据库连接参数变量
Dim db_host As String
Dim db_user As String
Dim db_pass As String
Dim db_data As String
' 定义 SQL 语句变量
Dim sql As String
' 初始化数据库连接变量
db_host = "localhost"
db_user = "root"
db_pass = ""
db_data = "erqiuda"
' MySQL ODBC 连接参数
'+------------+---------------------+----------------------------------+
'| 参数名 | 默认值 | 说明 |
'+------------+------------------------------------------------------–+
'| user | ODBC (on Windows) | MySQL 用户名 |
'| server | localhost | MySQL 服务器地址 |
'| database | | 默认连接数据库 |
'| option | 0 | 参数用以指定连接的工作方式 |
'| port | 3306 | 连接端口 |
'| stmt | | 一段声明, 可以在连接数据库后运行 |
'| password | | MySQL 用户密码 |
'| socket | | (略) |
'+------------+---------------------+----------------------------------+
' 详细查看官方说明
' http://dev.mysql.com/doc/refman/5.0/en/myodbc-configuration-connection-parameters.html
strCn = "DRIVER={MySQL ODBC 5.1 Driver};" & _
"SERVER=" & db_host & ";" & _
"DATABASE=" & db_data & ";" & _
"UID=" & db_user & ";PWD=" & db_pass & ";" & _
"OPTION=3;stmt=SET NAMES GB2312"
' stmt=SET NAMES GB2312
' 这句是设置数据库编码方式
' 中文操作系统需要设置成 GB2312
' 这样中文才不会有问题
' 版本要求 mysql 4.1+
' 连接数据库
cn.Open strCn
' 设置该属性, 使 recordcount 和 absolutepage 属性可用
cn.CursorLocation = adUseClient
' 访问表users
sql = "select * from users"
rs.Open sql, cn
MsgBox rs.RecordCount
Ⅳ vb 杩炴帴mysql鏁版嵁搴撹兘涓嶈兘涓嶈呴┍锷
涓嶈兘镄勚傛病链夋墍璋撶豢镩茬増镄勚傚氨绠楁槸鐢 BAT锛屼篃鏄灏 DLL 缁勪欢娉ㄥ唽浜嗘墠鑳戒娇鐢ㄣ
鍙浠ュ皢椹卞姩鍦ㄤ綘镄勭▼搴忓畨瑁呮椂涓璧峰畨瑁呬篃鍙浠
鍦ㄧ▼搴忓惎锷ㄦ椂鍒ゆ柇链夋病链夎 MSQL 镄勭粍浠堕┍锷锛屽傛灉娌℃湁锛岃繖鎻愮ず鎴栫洿鎺ユ墽琛屽畨瑁呮搷浣溿伞伞