DotNet如何访问mySQL数据库

发表于:2007-4-04 14:48

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:shanhe    来源:51testing博客

.net的数据库天然支持MSSQLServer,但是并非其他数据库不支持,而是微软基于自身利益需要,在支持、营销上推自己的数据库产品;但是作为平台战略,他并非排斥其他数据库,而是参考java体系提出了一套数据库访问规范,让各个第三方进行开发,提供特定的驱动。
MySQL是免费的数据库,在成本上具有无可替代的优势,但是目前来讲,并没有提供。微软把MySQL当作ODBC数据库,可以按照ODBC.Net规范进行访问,具体参考
而实际上,针对ODBC。Net的需要配置DSN的麻烦,而是出现了一个开源的系统MySQLDriverCS,对MySQL的开发进行了封装,实现了.net环境下对于MySQL数据库系统的访问。
 
通过阅读源代码,我们看到MySQLDriverCS的思路是利用C函数的底层库来操纵数据库的,通常提供对MySQL数据库的访问的数据库的C DLL是名为libmySQL.dll的驱动文件,MySQLDriverCS作为一个.net库进行封装C风格的驱动。
具体如何进行呢?
打开工程后,我们看到其中有一个比较特殊的.cs文件CPrototypes.cs:
#regionLICENSE
/*
     MySQLDriverCS: An C# driver for MySQL.
     Copyright (c) 2002 Manuel Lucas Vi馻s Livschitz.
 
     This file is part of MySQLDriverCS.
 
    MySQLDriverCS is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    MySQLDriverCS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with MySQLDriverCS; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#endregion
usingSystem;
usingSystem.Data;
usingSystem.Runtime.InteropServices;
namespaceMySQLDriverCS
{
    //[StructLayout(LayoutKind.Sequential)]
    publicclassMYSQL_FIELD_FACTORY
     {
        staticstringversion;
        publicstaticIMYSQL_FIELD GetInstance()
         {
 
             if(version==null)
              {
                   version = CPrototypes.GetClientInfo();
              }
             if(version.CompareTo("4.1.2-alpha")>=0)
              {
                  returnnewMYSQL_FIELD_VERSION_5();
              }
             else
                  returnnewMYSQL_FIELD_VERSION_3();
         }
     }
    publicinterfaceIMYSQL_FIELD
     {
        stringName{get;}
        uintType{get;}
        longMax_Length     {get;}
     }
    ///<summary>
    ///Field descriptor
    ///</summary>
     [StructLayout(LayoutKind.Sequential)]//"3.23.32", 4.0.1-alpha
    internalclassMYSQL_FIELD_VERSION_3: IMYSQL_FIELD
     {
        ///<summary>
        ///Name of column
        ///</summary>
        publicstringname;         
        ///<summary>
        ///Table of column if column was a field
        ///</summary>
        publicstringtable;         
         //public string org_table;              /* Org table name if table was an alias */
         //public string db;             /* Database for table */
        ///<summary>
        ///def
        ///</summary>
        publicstringdef;           
        ///<summary>
        ///length
        ///</summary>
        publiclonglength;     
        ///<summary>
        ///max_length
        ///</summary>
        publiclongmax_length;    
        ///<summary>
        ///Div flags
        ///</summary>
        publicuintflags;        
        ///<summary>
        ///Number of decimals in field
        ///</summary>
        publicuintdecimals;     
        ///<summary>
        ///Type of field. Se mysql_com.h for types
        ///</summary>
        publicuinttype;
 
        ///<summary>
        ///Name
        ///</summary>
        publicstringName
         {
             get{returnname;}
         }
        ///<summary>
        ///Type
        ///</summary>
        publicuintType
         {
             get{returntype;}
         }
        ///<summary>
        ///Max_Length
        ///</summary>
        publiclongMax_Length
         {
             get     {returnmax_length;}
         }
     }
 
    ///<summary>
    ///Field descriptor
    ///</summary>
     [StructLayout(LayoutKind.Sequential)]
    internalclassMYSQL_FIELD_VERSION_5: IMYSQL_FIELD
     {
        ///<summary>
        ///Name of column
        ///</summary>
        publicstringname;     
        ///<summary>
        ///Original column name, if an alias
        ///</summary>
        publicstringorg_name;    
        ///<summary>
        ///Table of column if column was a field
        ///</summary>
        publicstringtable;     
        ///<summary>
        ///Org table name if table was an alias
        ///</summary>
        publicstringorg_table;    
        ///<summary>
        ///Database for table
        ///</summary>
        publicstringdb;            
        ///<summary>
        ///Catalog for table
        ///</summary>
         //public string catalog;          
        ///<summary>
        /// def
        ///</summary>
        publicstringdef;           
        ///<summary>
        ///length
        ///</summary>
        publiclonglength;     
        ///<summary>
         ///max_length
        ///</summary>
        publiclongmax_length;    
        ///<summary>
        ///name_length
        ///</summary>
         //public uint name_length;
        ///<summary>
        ///org_name_length
        ///</summary>
        publicuintorg_name_length;
        ///<summary>
        ///table_length
        ///</summary>
        publicuinttable_length;
        ///<summary>
        ///org_table_length
        ///</summary>
        publicuintorg_table_length;
        ///<summary>
        ///db_length
        ///</summary>
        publicuintdb_length;
        ///<summary>
        ///catalog_length
        ///</summary>
        publicuintcatalog_length;
        ///<summary>
        ///def_length
        ///</summary>
        publicuintdef_length;
        ///<summary>
        ///Div flags
        ///</summary>
        publicuintflags;        
        ///<summary>
        ///Number of decimals in field
        ///</summary>
        publicuintdecimals;     
        ///<summary>
        ///Character set
        ///</summary>
        publicuintcharsetnr;    
        ///<summary>
        ///Type of field. Se mysql_com.h for types
        ///</summary>
        publicuinttype;
 
        ///<summary>
        ///Name
        ///</summary>
        publicstringName
         {
             get     {returnname;}
         }
        ///<summary>
        ///Type
        ///</summary>
        publicuintType
         {
             get     {returntype;}
         }
        ///<summary>
        ///Max_Length
        ///</summary>
        publiclongMax_Length
         {
             get     {returnmax_length;}
         }
     } 
 
    //[StructLayout(LayoutKind.Explicit)]
    publicenumenum_field_types
     {
          FIELD_TYPE_DECIMAL, FIELD_TYPE_TINY,
                              FIELD_TYPE_SHORT, FIELD_TYPE_LONG,
                              FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE,
                              FIELD_TYPE_NULL,   FIELD_TYPE_TIMESTAMP,
                              FIELD_TYPE_LONGLONG,FIELD_TYPE_INT24,
                              FIELD_TYPE_DATE,   FIELD_TYPE_TIME,
                              FIELD_TYPE_DATETIME, FIELD_TYPE_YEAR,
                              FIELD_TYPE_NEWDATE,
                              FIELD_TYPE_ENUM=247,
                              FIELD_TYPE_SET=248,
                              FIELD_TYPE_TINY_BLOB=249,
                              FIELD_TYPE_MEDIUM_BLOB=250,
                              FIELD_TYPE_LONG_BLOB=251,
                              FIELD_TYPE_BLOB=252,
                              FIELD_TYPE_VAR_STRING=253,
                              FIELD_TYPE_STRING=254,
                              FIELD_TYPE_GEOMETRY=255
 
     };
 
    ///<summary>
    ///C prototypes warpper for mysqllib.
    ///</summary>
    internalclassCPrototypes
     {
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_init" )]
        unsafepublicstaticexternvoid* mysql_init(void* must_be_null);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_close" )]
        unsafepublicstaticexternvoidmysql_close(void* handle);
        
        // BEGIN ADDITION 2004-07-01 BY Alex Seewald
        // Enables us to call mysql_option to activate compression and timeout
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_options" )]     
        unsafepublicstaticexternvoidmysql_options(void* mysql,uintoption,uint*value);
        // END ADDITION 2004-07-01 By Alex Seewald
 
 
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_real_connect" )]
        unsafepublicstaticexternvoid* mysql_real_connect(void* mysql,stringhost,stringuser,stringpasswd,stringdb,uintport,stringunix_socket,intclient_flag);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_query" )]
        unsafepublicstaticexternintmysql_query(void*mysql,stringquery);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_store_result" )]
        unsafepublicstaticexternvoid*mysql_store_result(void*mysql);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_free_result" )]
        unsafepublicstaticexternvoidmysql_free_result(void*result);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_errno" )]
        unsafepublicstaticexternuintmysql_errno(void*mysql);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_error" )]
        unsafepublicstaticexternstringmysql_error(void*mysql);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_field_count" )]
        unsafepublicstaticexternuintmysql_field_count(void*mysql);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_affected_rows" )]
        unsafepublicstaticexternulongmysql_affected_rows(void*mysql);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_num_fields" )]
        unsafepublicstaticexternuintmysql_num_fields(void*result);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_num_rows" )]
        unsafepublicstaticexternulongmysql_num_rows(void*result);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_fetch_field_direct" )]
        unsafepublicstaticexternIntPtr mysql_fetch_field_direct(void*result,uintfieldnr);
 
         ///<returns>Returns a string that represents the client library version</returns>
          [DllImport("libmySQL.dll",CharSet=System.Runtime.InteropServices.CharSet.Ansi,
          EntryPoint="mysql_get_client_info", ExactSpelling=true)]
        publicstaticexternstringGetClientInfo();
 
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_fetch_row" )]
        unsafepublicstaticexternIntPtr mysql_fetch_row(void*result);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_select_db" )]
        unsafepublicstaticexternintmysql_select_db(void*mysql,stringdbname);
          [ DllImport( "libmySQL.dll", EntryPoint="mysql_fetch_lengths" )]
        unsafepublicstaticexternUInt32 *mysql_fetch_lengths(void*result);
 
     }
}
 
基本上是将C风格的基础数据结构进行.net的重新定义,然后通过InteropServices进行访问。
 
具体如何利用这个库进行操作,可以参考其中的例子。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号