中国最大的web开发资源网站及技术社区,
.net的数据库天然支持mssqlserver,但是并非其他数据库不支持,而是微软基于自身利益需要,在支持、营销上推自己的数据库产品;但是作为平台战略,他并非排斥其他数据库,而是参考java体系提出了一套数据库访问规范,让各个第三方进行开发,提供特定的驱动。
mysql是免费的数据库,在成本上具有无可替代的优势,但是目前来讲,并没有提供。微软把mysql当作odbc数据库,可以按照odbc.net规范进行访问,具体参考
http://www.microsoft.com/china/community/columns/luyan/6.mspx
而实际上,针对odbc。net的需要配置dsn的麻烦,而是出现了一个开源的系统mysqldrivercs,对mysql的开发进行了封装,实现了.net环境下对于mysql数据库系统的访问。
http://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. 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
using system;
using system.data;
using system.runtime.interopservices;
namespace mysqldrivercs
{
//[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 {get;}
}
///<summary>
/// field descriptor
///</summary>
[structlayout(layoutkind.sequential)]//"3.23.32", 4.0.1-alpha
internal class mysql_field_version_3: imysql_field
{
///<summary>
/// name of column
///</summary>
public string name;
///<summary>
/// table of column if column was a field
///</summary>
public string table;
//public string org_table; /* org table name if table was an alias */
//public string db; /* database for table */
///<summary>
/// def
///</summary>
public string def;
///<summary>
/// length
///</summary>
public long length;
///<summary>
/// max_length
///</summary>
public long max_length;
///<summary>
/// div flags
///</summary>
public uint flags;
///<summary>
/// number of decimals in field
///</summary>
public uint decimals;
///<summary>
/// type of field. se mysql_com.h for types
///</summary>
public uint type;
///<summary>
/// name
///</summary>
public string name
{
get{return name;}
}
///<summary>
/// type
///</summary>
public uint type
{
get{return type;}
}
///<summary>
/// max_length
///</summary>
public long max_length
{
get {return max_length;}
}
}
///<summary>
/// field descriptor
///</summary>
[structlayout(layoutkind.sequential)]
internal class mysql_field_version_5: imysql_field
{
///<summary>
/// name of column
///</summary>
public string name;
///<summary>
/// original column name, if an alias
///</summary>
public string org_name;
///<summary>
/// table of column if column was a field
///</summary>
public string table;
///<summary>
/// org table name if table was an alias
///</summary>
public string org_table;
///<summary>
/// database for table
///</summary>
public string db;
///<summary>
/// catalog for table
///</summary>
//public string catalog;
///<summary>
/// def
///</summary>
public string def;
///<summary>
/// length
///</summary>
public long length;
///<summary>
/// max_length
///</summary>
public long max_length;
///<summary>
/// name_length
///</summary>
//public uint name_length;
///<summary>
/// org_name_length
///</summary>
public uint org_name_length;
///<summary>
/// table_length
///</summary>
public uint table_length;
///<summary>
/// org_table_length
///</summary>
public uint org_table_length;
///<summary>
/// db_length
///</summary>
public uint db_length;
///<summary>
/// catalog_length
///</summary>
public uint catalog_length;
///<summary>
/// def_length
///</summary>
public uint def_length;
///<summary>
/// div flags
///</summary>
public uint flags;
///<summary>
/// number of decimals in field
///</summary>
public uint decimals;
///<summary>
/// character set
///</summary>
public uint charsetnr;
///<summary>
/// type of field. se mysql_com.h for types
///</summary>
public uint type;
///<summary>
/// name
///</summary>
public string name
{
get {return name;}
}
///<summary>
/// type
///</summary>
public uint type
{
get {return type;}
}
///<summary>
/// max_length
///</summary>
public long max_length
{
get {return max_length;}
}
}
//[structlayout(layoutkind.explicit)]
public enum enum_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>
internal class cprototypes
{
[ dllimport( "libmysql.dll", entrypoint="mysql_init" )]
unsafe public static extern void* mysql_init(void* must_be_null);
[ dllimport( "libmysql.dll", entrypoint="mysql_close" )]
unsafe public static extern void mysql_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" )]
unsafe public static extern void mysql_options(void* mysql, uint option, uint *value);
// end addition 2004-07-01 by alex seewald
[ dllimport( "libmysql.dll", entrypoint="mysql_real_connect" )]
unsafe public static extern void* mysql_real_connect(void* mysql, string host, string user, string passwd, string db, uint port, string unix_socket, int client_flag);
[ dllimport( "libmysql.dll", entrypoint="mysql_query" )]
unsafe public static extern int mysql_query(void*mysql, string query);
[ dllimport( "libmysql.dll", entrypoint="mysql_store_result" )]
unsafe public static extern void *mysql_store_result(void *mysql);
[ dllimport( "libmysql.dll", entrypoint="mysql_free_result" )]
unsafe public static extern void mysql_free_result(void*result);
[ dllimport( "libmysql.dll", entrypoint="mysql_errno" )]
unsafe public static extern uint mysql_errno(void*mysql);
[ dllimport( "libmysql.dll", entrypoint="mysql_error" )]
unsafe public static extern string mysql_error(void*mysql);
[ dllimport( "libmysql.dll", entrypoint="mysql_field_count" )]
unsafe public static extern uint mysql_field_count(void*mysql);
[ dllimport( "libmysql.dll", entrypoint="mysql_affected_rows" )]
unsafe public static extern ulong mysql_affected_rows(void*mysql);
[ dllimport( "libmysql.dll", entrypoint="mysql_num_fields" )]
unsafe public static extern uint mysql_num_fields(void*result);
[ dllimport( "libmysql.dll", entrypoint="mysql_num_rows" )]
unsafe public static extern ulong mysql_num_rows(void *result);
[ dllimport( "libmysql.dll", entrypoint="mysql_fetch_field_direct" )]
unsafe public static extern intptr mysql_fetch_field_direct(void*result, uint fieldnr);
///<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)]
public static extern string getclientinfo();
[ dllimport( "libmysql.dll", entrypoint="mysql_fetch_row" )]
unsafe public static extern intptr mysql_fetch_row(void*result);
[ dllimport( "libmysql.dll", entrypoint="mysql_select_db" )]
unsafe public static extern int mysql_select_db(void*mysql,string dbname);
[ dllimport( "libmysql.dll", entrypoint="mysql_fetch_lengths" )]
unsafe public static extern uint32 *mysql_fetch_lengths(void*result);
}
}
基本上是将c风格的基础数据结构进行.net的重新定义,然后通过interopservices进行访问。
具体如何利用这个库进行操作,可以参考其中的例子。