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
/*
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
{
///<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)]
{
///<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
{
unsafe public static extern void* Mysql_init(void* must_be_null);
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
// END ADDITION 2004-07-01 By Alex Seewald
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);
unsafe public static extern void Mysql_free_result(void*result);
unsafe public static extern uint Mysql_num_fields(void*result);
unsafe public static extern ulong Mysql_num_rows(void *result);
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();
unsafe public static extern IntPtr Mysql_fetch_row(void*result);
unsafe public static extern UInt32 *Mysql_fetch_lengths(void*result);
}
} |