65.9K
CodeProject 正在变化。 阅读更多。
Home

.NET MySQL 连接 V 1.2

2003年7月18日

viewsIcon

142459

downloadIcon

2328

用于 MySQL 请求的类库。

引言

这是一个用 VB.NET 编写的类库,用于访问 MySQL 数据库。您可以使用此库而无需创建用于操作数据库的函数。

要使用此库,您必须安装

在这个库中,您有 3 个类

  • ManipStringForMySQL:这个类修改字符串以适应数据库管理系统 (SGBD)
  • MySQL_Requettes:这个类向数据库管理系统 (SGBD) 发送请求
  • MySQL_Utils :这个类对数据库管理系统 (SGBD) 的数据进行测试

Visual Studio project screenshot

一个包含查询结果的数据集的函数示例

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

该函数的调用方式如下

Dim MonDatasetTemp As DataSet = MySQL_Requettes.MyODBCDataset(
               SQL,
               constantes.ODBCBase, 
               constantes.ODBCServer, 
               constantes.ODBCPwd, 
               constantes.ODBCUser, 
               "MonDataset")

结论

这个库是我在 ASP.NET 解决方案中使用 MySQL 的第一个示例。我希望它能易于在您的项目中应用。

您还可以查看这篇文章的法语版本,该版本更侧重于类库的创建和使用

© . All rights reserved.