VB.net 连接 MySql,Use MySql.Data.dll

前端之家收集整理的这篇文章主要介绍了VB.net 连接 MySql,Use MySql.Data.dll前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下载一个叫 mysql-connector-net 的库文件
安装后,会得到一下东西
MysqL.Data.dll ,.net 与MysqL 连接的桥梁,很好用
MysqL.Data.dll ,源码,C# 的,你可以试着完善它
C#和VB.net 的示例程序。
在项目中引用
MysqL.Data.dll
再来个 Imports MysqL.Data.MysqLClient 就可以使用了

里面有一些对MysqL 数据库的一般操作,很容易上手。

下面是我总结出的MysqL命令语句,留个档~~

连接:
server={0};user id={1}; password={2}; database=MysqL; pooling=false;port={3}

获取
SELECT * FROM login where UserName='{0}' and Password='{1}'
这里值得注意,文本型的在 sql 2000 中要使用 like 比较
select ID from login order by ID desc limit 1
可以获得最后一个ID

更新:
UPDATE login SET logintimes='{0}',LastDateTime='{1}' where UserName='{2}'

插入:
INSERT INTO login (id,username,password) VALUES ('{0}','{1}','{2}')

删除
delete FROM med_entry where ID='{0}'


下面几个自己写的函数,很有用处

'执行 MysqL 语句,支持多行。没有返回值
Function ExecuteMysqLCMD(ByRef iconn As MysqLConnection,ByRef MysqLCMD As String,Optional ByRef Err As String = "") As Boolean
MysqLCMD = Strings.Replace(MysqLCMD,Chr(13),"")
MysqLCMD = Strings.Replace(MysqLCMD,Chr(10),"")
Err = ""
Dim reader As MysqLDataReader = Nothing
Dim cmdList() As String
Dim cmd As MysqLCommand = Nothing
Dim i As Integer
cmdList = Strings.Split(MysqLCMD,";")

If cmdList IsNot Nothing Then
Try
For i = 0 To cmdList.Length - 1
If cmdList(i) = "" Then Continue For
cmd = New MysqLCommand(cmdList(i),iconn)
reader = cmd.ExecuteReader()
reader.Close()
Next
Return True
Catch ex As MysqLException
Err = ex.Message
End Try
End If
Return False
End Function

'执行 MysqL 语句,只能一行。返回 DataTable,MysqLDataAdapter,MysqLCommandBuilder
Function ExecuteMysqLCMD(ByRef iconn As MysqLConnection,ByRef iDataTable As DataTable,Optional ByRef iMysqLDataAdapter As MysqLDataAdapter = Nothing,Optional ByRef iMysqLCommandBuilder As MysqLCommandBuilder = Nothing,"")
Err = ""

Dim cmdList() As String
cmdList = Strings.Split(MysqLCMD,";")
If cmdList IsNot Nothing Then
Try
If cmdList(0) = "" Then Exit Try
iDataTable = New DataTable
iMysqLDataAdapter = New MysqLDataAdapter(cmdList(0),iconn)
iMysqLCommandBuilder = New MysqLCommandBuilder(iMysqLDataAdapter)
iMysqLDataAdapter.Fill(iDataTable)
Return True
Catch ex As MysqLException
Err = ex.Message
End Try
End If
Return False
End Function

'检查数据库是否存在
Function CheckDataBase(ByRef iconn As MysqLConnection,ByRef DataBaseName As String,Optional ByRef Err As String = "") As Boolean
Dim reader As MysqLDataReader = Nothing
DataBaseName = DataBaseName.ToLower
Err = ""
Dim cmd As New MysqLCommand("SHOW DATABASES",iconn)
Try
reader = cmd.ExecuteReader()

While (reader.Read())
If reader.GetString(0).ToLower = DataBaseName Then
Return True
End If
End While

Catch ex As MysqLException
Err = ex.Message
Finally
If Not reader Is Nothing Then reader.Close()
End Try
Return False
End Function

'检查数据表是否存在 Function CheckTable(ByRef iconn As MysqLConnection,ByVal TableName As String,Optional ByRef Err As String = "") As Boolean Dim reader As MysqLDataReader = Nothing DataBaseName = DataBaseName.ToLower TableName = TableName.ToLower Err = "" iconn.ChangeDatabase(DataBaseName) Dim cmd As New MysqLCommand("SHOW TABLES",iconn) Try reader = cmd.ExecuteReader() While (reader.Read()) If reader.GetString(0).ToLower = TableName Then Return True End If End While Catch ex As MysqLException Err = ex.Message Finally If Not reader Is Nothing Then reader.Close() End Try Return False End Function
原文链接:https://www.f2er.com/vb/260624.html

猜你在找的VB相关文章