DotNet 如何访问 mySQL 数据库 .net 的数据库天然支持 MSSQLServer,但是并非其他数据库不支持,而是微软基于自身利益 需要,在支持、营销上推自己的数据库产品;但是作为平台战略,他并非排斥其他数据库, 而是参考 java 体系提出了一套数据库访问规范,让各个第三方进行开发,提供特定的驱动。 MySQL 是
免费的数据库,在成本上具有无可替代的优势,但是目前来讲,并没有提供。微 软把 MySQL 当作 ODBC 数据库,可以按照 ODBC.Net 规范进行访问,具体参考 microsoft/china/community/Columns/Luyan/6.mspx 而实际上,针对 ODBC。Net 的需要配置 DSN 的麻烦,而是出现了一个开源的
系统 MySQLDriverCS,对 MySQL 的开发进行了封装,实现了.net 环境下对于 MySQL 数据库系 统的访问。 sourceforge.net/projects/mysqldrivercs/ 通过阅读
源代码, 我们看到 MySQLDriverCS 的思路是利用 C 函数的底层库来操纵数据库的, 通常提供对 MySQL 数据库的访问的数据库的 C DLL 是名为 libmySQL.dll 的驱动文件, MySQLDriverCS 作为一个.net 库进行封装 C 风格的驱动。 具体如何进行呢? 打开工程后,我们看到其中有一个比较特殊的.cs 文件 CPrototypes.cs:
#region LICENSE /* 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. 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 */ #endregion using System; using System.Data; using System.Runtime.InteropServices; namespace MySQLDriverCS 02111-1307 USA See the
{ //[StructLayout(LayoutKind.Sequential)] public class MYSQL_FIELD_FACTORY { static string version; public static IMYSQL_FIELD GetInstance() { if (version==null) { version = CPrototypes.GetClientInfo(); } if (version.CompareTo("4.1.2-alpha")>=0) { return new MYSQL_FIELD_VERSION_5(); } else return new MYSQL_FIELD_VERSION_3(); } } public interface IMYSQL_FIELD { string Name{get;} uint Type{get;} long Max_Length } ///
/// Field descriptor /// [StructLayout(LayoutKind.Sequential)]//"3.23.32", 4.0.1-alpha internal class MYSQL_FIELD_VERSION_3: IMYSQL_FIELD { ///
/// Name of column /// public string name; ///
/// Table of column if column was a field /// public string table;
//public string org_table; //public string db; ///
/// def /* Org table name if table was an alias */ /* Database for table */ {get;}
/// public string def; ///
/// length /// public long length; ///
/// max_length /// public long max_length; ///
/// Div flags /// public uint flags; ///
/// Number of decimals in field /// public uint decimals; ///
/// Type of field. Se mysql_com.h for types /// public uint type; ///
/// Name /// public string Name { get{return name;} } ///
/// Type /// public uint Type { get{return type;} } ///
/// Max_Length /// public long Max_Length { get {return max_length;} }
} ///
/// Field descriptor /// [StructLayout(LayoutKind.Sequential)] internal class MYSQL_FIELD_VERSIO