国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

技巧 .NET如何訪問MySQL數據庫

2024-07-10 13:08:34
字體:
來源:轉載
供稿:網友
中國最大的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進行訪問。

  具體如何利用這個庫進行操作,可以參考其中的例子。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 历史| 临清市| 抚州市| 安顺市| 宣恩县| 麻城市| 靖西县| 八宿县| 崇文区| 湖口县| 讷河市| 昌平区| 楚雄市| 永兴县| 德化县| 肇州县| 囊谦县| 新化县| 黔南| 思茅市| 阳谷县| 南充市| 比如县| 通河县| 罗定市| 沧源| 佛学| 清河县| 沁水县| 红河县| 阿坝| 盐池县| 张家口市| 新巴尔虎左旗| 尉犁县| 池州市| 大宁县| 滨海县| 松原市| 屯门区| 富宁县|