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

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

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

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

连接:
server={0};user id={1}; password={2}; database=@R_403_198@; 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}'


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

'执行 @R_403_198@ 语句,支持多行。没有返回值
Function Execute@R_403_198@CMD(ByRef iconn As @R_403_198@Connection,ByRef @R_403_198@CMD As String,Optional ByRef Err As String = "") As Boolean
@R_403_198@CMD = Strings.Replace(@R_403_198@CMD,Chr(13),"")
@R_403_198@CMD = Strings.Replace(@R_403_198@CMD,Chr(10),"")
Err = ""
Dim reader As @R_403_198@DataReader = Nothing
Dim cmdList() As String
Dim cmd As @R_403_198@Command = Nothing
Dim i As Integer
cmdList = Strings.Split(@R_403_198@CMD,";")

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

'执行 @R_403_198@ 语句,只能一行。返回 DataTable,@R_403_198@DataAdapter,@R_403_198@CommandBuilder
Function Execute@R_403_198@CMD(ByRef iconn As @R_403_198@Connection,ByRef iDataTable As DataTable,Optional ByRef i@R_403_198@DataAdapter As @R_403_198@DataAdapter = Nothing,Optional ByRef i@R_403_198@CommandBuilder As @R_403_198@CommandBuilder = Nothing,"")
Err = ""

Dim cmdList() As String
cmdList = Strings.Split(@R_403_198@CMD,";")
If cmdList IsNot Nothing Then
Try
If cmdList(0) = "" Then Exit Try
iDataTable = New DataTable
i@R_403_198@DataAdapter = New @R_403_198@DataAdapter(cmdList(0),iconn)
i@R_403_198@CommandBuilder = New @R_403_198@CommandBuilder(i@R_403_198@DataAdapter)
i@R_403_198@DataAdapter.Fill(iDataTable)
Return True
Catch ex As @R_403_198@Exception
Err = ex.Message
End Try
End If
Return False
End Function

'检查数据库是否存在
Function CheckDataBase(ByRef iconn As @R_403_198@Connection,ByRef DataBaseName As String,Optional ByRef Err As String = "") As Boolean
Dim reader As @R_403_198@DataReader = Nothing
DataBaseName = DataBaseName.ToLower
Err = ""
Dim cmd As New @R_403_198@Command("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 @R_403_198@Exception
Err = ex.Message
Finally
If Not reader Is Nothing Then reader.Close()
End Try
Return False
End Function

'检查数据表是否存在 Function CheckTable(ByRef iconn As @R_403_198@Connection,ByVal TableName As String,Optional ByRef Err As String = "") As Boolean Dim reader As @R_403_198@DataReader = Nothing DataBaseName = DataBaseName.ToLower TableName = TableName.ToLower Err = "" iconn.ChangeDatabase(DataBaseName) Dim cmd As New @R_403_198@Command("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 @R_403_198@Exception Err = ex.Message Finally If Not reader Is Nothing Then reader.Close() End Try Return False End Function

猜你在找的VB相关文章