VB.NET操作MySql數(shù)據(jù)庫代碼范例解讀
通過對(duì)VB.NET的學(xué)習(xí),大家可能會(huì)對(duì)這款功能強(qiáng)大的編程語言有些愛不釋手。尤其是在數(shù)據(jù)庫的操作上,體現(xiàn)了簡(jiǎn)便靈活的特性。下面我們就來看看有關(guān)VB.NET操作MySql數(shù)據(jù)庫的一些應(yīng)用技巧。#t#
這是在VB.NET中為連接MySql中編寫的一個(gè)類庫。你可以使用這個(gè)類庫直接操作MySql數(shù)據(jù)庫。
在使用這個(gè)類庫之前,你必須安裝
ODBC MYSQL 3.51下載地址:http://www.mysql.com/downloads/api-myodbc-3.51.html
Driver ODBC.NET下載地址:http://www.microsoft.com/downloads/details.aspx?FamilyId=6CCD8427-1017-4F33-A062-D165078E32B1&displaylang=en
在這個(gè)庫中有三個(gè)類:
ManipStringForMySQL: 此類修改SGBD的字符串
MySQL_Requettes: 發(fā)送請(qǐng)求
MySQL_Utils : 對(duì)SGBD數(shù)據(jù)進(jìn)行測(cè)試
下面是VB.NET操作MySql數(shù)據(jù)庫的一個(gè)例子
對(duì)DataSet進(jìn)行操作
- Public Shared Function MyODBCDataset
(ByVal ReqSQL As String, _- ByVal LaBase As String, ByVal
Server As String, _- ByVal Password As String, ByVal
User As String, _- ByVal NomDataSet As String) As DataSet
- '' Connexion à un server MySQL
- ''avec le Driver ODBC 3.51 avec
requette qui renvoie un dataset- Dim MyConString As String = _
- "DRIVER={MySQL ODBC 3.51 Driver};
SERVER=" & Server _- & ";DATABASE=" & LaBase & ";
UID=" & User _- & ";PASSWORD=" & Password & ";
OPTION=3;"- Dim MyODBCConnexion As New
OdbcConnection(MyConString)- Try
- Dim ds As New DataSet()
- Dim cmd As OdbcDataAdapter = New
- OdbcDataAdapter(ReqSQL, MyConString)
- Dim MyCommand As New OdbcCommand()
- Dim MyDataReader As OdbcDataReader
- cmd.Fill(ds, NomDataSet)
- MyODBCConnexion.Close()
- Return ds
- Catch MyOdbcException As OdbcException
- ''
- HttpContext.Current.Response.
Write(MyOdbcException.ToString)- Catch MyException As Exception
- ''
- HttpContext.Current.Response.
Write(MyException.ToString)- End Try
- End Function
對(duì)函數(shù)的調(diào)用
- Dim MonDatasetTemp As
DataSet = MySQL_Requettes
.MyODBCDataset(- SQL,
- constantes.ODBCBase,
- constantes.ODBCServer,
- constantes.ODBCPwd,
- constantes.ODBCUser,
- "MonDataset")
此VB.NET操作MySql數(shù)據(jù)庫實(shí)現(xiàn)程序用到的類庫可在如下地址下載:http://www.codeproject.com/dotnet/MySQLConnexion/MySQLConnexion.zip