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 鐨勭粍浠墮┍鍔錛屽傛灉娌℃湁錛岃繖鎻愮ず鎴栫洿鎺ユ墽琛屽畨瑁呮搷浣溿傘傘