新疆软件开发

本站首页 软件开发 成功案例 公司新闻 公司简介 客服中心 软件技术 网站建设
  您现在的位置: 新疆二域软件开发公司 >> 数据库开发 >> 文章正文

使用.NET连接MySQL数据库

.NET的数据库天然支持MSSQLServer,但是并非其他数据库不支持,而是微软基于自身利益需要,在支持、营销上推自己的数据库产品;但是作为平台战略,他并非排斥其他数据库,而是参考java体系提出了一套数据库访问规范,让各个第三方进行开发,提供特定的驱动。新疆软件开发网搜集整理 Cq3pXq{G  
CkS8rX'  
  MySQL是免费的数据库,在成本上具有无可替代的优势,但是目前来讲,并没有提供。微软把MySQL当作ODBC数据库,可以按照ODBC.Net规范进行访问,具体参考 Y.gASJL/`  
u q_&*$  
  http://www.microsoft.com/china/community/Columns/Luyan/6.mspx 7s'uHx  
m.6D^J!  
  而实际上,针对ODBC。Net的需要配置DSN的麻烦,而是出现了一个开源的系统MySQLDriverCS,对MySQL的开发进行了封装,实现了.net环境下对于MySQL数据库系统的访问。 z}@&%"'  
9Ljkl=Ppc  
  http://sourceforge.net/projects/mysqldrivercs/ "Lu,8Eq  
d mWqvP/D  
  通过阅读源代码,我们看到MySQLDriverCS的思路是利用C函数的底层库来操纵数据库的,通常提供对MySQL数据库的访问的数据库的C DLL是名为libmySQL.dll的驱动文件,MySQLDriverCS作为一个.net库进行封装C风格的驱动。 hg1; I(  
MeVKx5 y  
  具体如何进行呢? vR K::   
1l]kskJ>D  
  打开工程后,我们看到其中有一个比较特殊的.cs文件CPrototypes.cs: W}^NH/7E  
^l.{S%|4  
以下是引用片段: 8o20:OA,X  
Xar*yE;e  
#region LICENSE  c!9zei  
/* Tc%a6F:Ke  
MySQLDriverCS: An C# driver for MySQL. ?2kVCs&>5  
Copyright (c) 2002 Manuel Lucas Vi馻s Livschitz. a0&-H{=/z  
3^xuVUDX  
This file is part of MySQLDriverCS. Y4a4/XEa  
5Kd1IP  
MySQLDriverCS is free software; you can redistribute it and/or modify 4C`n2{i  
it under the terms of the GNU General Public License as published by d.yK?Fj  
the Free Software Foundation; either version 2 of the License, or :TNdqHcv  
(at your option) any later version. |VR;,aE%\  
abQ{Qw&  
MySQLDriverCS is distributed in the hope that it will be useful, vg5T{~XN  
but WITHOUT ANY WARRANTY; without even the implied warranty of )I{<E m3  
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the {z>H&,`n  
GNU General Public License for more details. A.|o {>  
|K{5W(t.  
You should have received a copy of the GNU General Public License I[ELwFt {W  
along with MySQLDriverCS; if not, write to the Free Software p17YBfC  
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }CVb'2 $1  
*/ 0<ZV),<c  
#endregion Xh ><glF  
using System; vN2n\M^J^  
using System.Data; A~wZ2I@^  
using System.Runtime.InteropServices; Bz.Pc);  
namespace MySQLDriverCS P7'ztDm"  
{ zr/#73q&L  
Z' iD_mtZ  
//[StructLayout(LayoutKind.Sequential)] sS>v%`_  
public class MYSQL_FIELD_FACTORY 9y7:F (1  
{ ~wNU=*,O  
static string version; x}o{'  
public static IMYSQL_FIELD GetInstance() {z-8\LH s  
{ =3R l\  
E"t\! eSW  
if (version==null) u,E*WcM/  
{ Hl& Fh0k  
version = CPrototypes.GetClientInfo(); `TG[y 0m  
} * ^'E  
if (version.CompareTo("4.1.2-alpha")>=0) x_}JRcny^  
{ {TU;3DR   
return new MYSQL_FIELD_VERSION_5(); \|&;b<S!  
} Gg?rNB <  
else SxY-C{gR  
return new MYSQL_FIELD_VERSION_3(); D8Gp1H  
} m o|O`UG\  
} N#UAoGIt;  
public interface IMYSQL_FIELD !Lo?oG+  
{ pPk 9le  
string Name{get;} @ 0u9`  
uint Type{get;} 6eF~u.i   
long Max_Length {get;} g~NzFv=:G  
} J 7\lW U  
///<summary> gFI/X!k%  
/// Field descriptor ={k|[:S  
///</summary> Ks*xm1l3  
[StructLayout(LayoutKind.Sequential)]//"3.23.32", 4.0.1-alpha x24L"hJCK  
internal class MYSQL_FIELD_VERSION_3: IMYSQL_FIELD Bs338 s{}  
{ lD~XV:+  
///<summary> jU3x@?JH  
/// Name of column |D 7PRio  
///</summary> li:UK][l@  
public string name; *\vn1`%  
///<summary> d@wVr A!  
/// Table of column if column was a field 9Y<Z}E$g9  
///</summary> c|7@^h`  
public string table; KJOh/.mM  
//public string org_table; /* Org table name if table was an alias */ E ^eS   
//public string db; /* Database for table */ \uNZ&qy(  
///<summary> ~k!qd9YN3  
/// def "%qp?R-zy  
///</summary> x|* y;dB  
public string def; Y'% s'k  
///<summary> ^xv<MQ^N  
/// length hAL Ul/_  
///</summary> eUl0Q  
public long length; _]=K&~@/s  
///<summary> !@"i+8]6V  
/// max_length BkaMb,~  
///</summary> ,CpX fEm  
public long max_length; =$IYhuS~  
///<summary> O4pj~uQ7  
/// Div flags L+]=g(@E  
///</summary> \,| 3p  
public uint flags; %ty'Z&&k  
///<summary> MXF>HW  
/// Number of decimals in field MyeO+IF2  
///</summary> $;bff"bO  
public uint decimals; $4ht`R  
///<summary> =0) C3:q  
/// Type of field. Se mysql_com.h for types ka-n[Hz  
///</summary> W]]-NWk  
public uint type; t.!9<p =>  
|3e' 5jf$  
///<summary> %q!1M-8  
/// Name V/ hP  
///</summary> <L{Vx+0I  
public string Name !%L~p]|w  
{ 1oe C&se  
get{return name;} ^h5qUrulL  
} EmT{#o9  
///<summary> a cU#;]"s  
/// Type # !!MQTk  
///</summary> P`a-C3FF  
public uint Type K4u$4)as  
{ P LHr)  
get{return type;} @uync8?%  
} cp)NZWz  
///<summary> g=?e3 .  
/// Max_Length V#@ VsgKK  
///</summary> # L h~CpnS  
public long Max_Length CJ"Be'u Xy  
{ 2H*2N=  
get {return max_length;} \gTV[OB  
} ~$?~NiZ,)  
} +`xIuic  
-fu}K W  
///<summary> ~`fOP Q  
/// Field descriptor HR~+yFp8  
///</summary> _;{_m{u  
[StructLayout(LayoutKind.Sequential)] u>^|OcW  
internal class MYSQL_FIELD_VERSION_5: IMYSQL_FIELD cJ|h=mv  
{ T&"ao4Io}  
///<summary>  /T\|=Q  
/// Name of column z QP*t~  
///</summary> OC;|% s#m  
public string name; pM`#7lE  
///<summary> 4^Kk?ib}L  
/// Original column name, if an alias ,AhB !(  
///</summary> @SKjhi  
public string org_name; tn6$Px  
///<summary> : +S>8  
/// Table of column if column was a field  j vA%b  
///</summary> %LO&\{,1U  
public string table; `#'kUYQ[]  
///<summary> }ifhThTa  
/// Org table name if table was an alias 'shu2(  
///</summary> e9n3sv/  
public string org_table; oL /UPl`7s  
///<summary> gRUxL  
/// Database for table %q0D+[ TV  
///</summary> wh c fX  
public string db; Ga+v@,J1  
///<summary> c~ zR(BO  
/// Catalog for table 7yOn''N  
///</summary> s]o o~DX  
//public string catalog; '.3l 8!  
///<summary> 9 *iRa %:^  
/// def 4e6Pm :  
///</summary> c_OFJ3n!n  
public string def; i'{<D7(  
///<summary> &6&PjZfIL  
/// length YUubM"7  
///</summary> a$"z$<Y.  
public long length; 1s n7xaj  
///<summary> y$?8_L  
/// max_length 9bf0+h)9  
///</summary> i4rkoekJ  
public long max_length; AW9j3UE  
///<summary> 5+~' R{~~  
/// name_length Ic3BE2r  
///</summary> }^-uv;d\  
//public uint name_length; ]x~OXF'I~  
///<summary> !G5)q> {  
/// org_name_length *AYo\u  
///</summary> ^. _|FT  
public uint org_name_length; |u]S)TcVju  
///<summary> lF_DBPFko  
/// table_length d_})1S~SB  
///</summary> t & v  
public uint table_length; PB.LF  
///<summary> A@M &ah :  
/// org_table_length Ws[LxBkE  
///</summary> R#NamBWl  
public uint org_table_length; Fo` lo.)*]  
///<summary> IY:EX#)  
/// db_length p $zIXQ( g  
///</summary> q`YSi  
public uint db_length; 38<39Lz  
///<summary> rK2HZ 5  
/// catalog_length 'xTgSf.  
///</summary> 5Do& #G9  
public uint catalog_length; -@K*sQ  
///<summary> ;2dxdUUQj  
/// def_length -SmSW /  
///</summary> @.c(LzxKc  
public uint def_length; 4=M-fv%#  
///<summary> NM ?F7b]?  
/// Div flags jq26D$>K  
///</summary> {.,'>hw>U  
public uint flags; ,Tl:9y?2  
///<summary> /%8jto(  
/// Number of decimals in field XoPL-GBa  
///</summary> 5 /[s^/eE  
public uint decimals; ;WR%0fp'  
///<summary> NYA^xh  
/// Character set G}Vh;%T#  
///</summary> -=]HbX@i  
public uint charsetnr; ?Rvu`   
///<summary>  U!w11P  
/// Type of field. Se mysql_com.h for types hFch#a-;  
///</summary> ua#[^I+&  
public uint type; g{\.V(  
_VXg0  
///<summary> #[}2KA QLx  
/// Name ML[?hj  
///</summary> (njL[#u2  
public string Name J0 {iXI  
{ p*%pk=  
get {return name;} E'Sy d\  
} +n%73=  
///<summary> 8:o"   
/// Type odNkG:R.  
///</summary> "?{2) +G  
public uint Type Oi) 5r}f  
{ Y3:5YSTk  
get {return type;} >y"tuh5:H  
} %^W.i"H0f  
///<summary> *b,:5lKXK  
/// Max_Length D,R{vjP-  
///</summary> bZ>6V0{=  
public long Max_Length w,lz]Y,  
{ Fcbf"T2J  
get {return max_length;} 4>8#Q0  
} qaLYL`ak  
} !]EC`]zYj  
//[StructLayout(LayoutKind.Explicit)] c|*`s.gh  
public enum enum_field_types +vN".EU  
{ $hsOZdR  
FIELD_TYPE_DECIMAL, FIELD_TYPE_TINY, oCCa  
FIELD_TYPE_SHORT, FIELD_TYPE_LONG, "b.gK6  
FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE, >*l2R6qzn  
FIELD_TYPE_NULL, FIELD_TYPE_TIMESTAMP, ,2y8(  
FIELD_TYPE_LONGLONG,FIELD_TYPE_INT24, +;G^bx_ #|  
FIELD_TYPE_DATE, FIELD_TYPE_TIME, @?@J;  
FIELD_TYPE_DATETIME, FIELD_TYPE_YEAR, RDFx i=&a  
FIELD_TYPE_NEWDATE,  b?eIjkn  
FIELD_TYPE_ENUM=247, e!J|c^c5'  
FIELD_TYPE_SET=248, x [c2/a_X  
FIELD_TYPE_TINY_BLOB=249, $f0c'%UW  
FIELD_TYPE_MEDIUM_BLOB=250, 5Qd_2e`  
FIELD_TYPE_LONG_BLOB=251, \kvZ/>G3xZ  
FIELD_TYPE_BLOB=252, 6p\=sL  
FIELD_TYPE_VAR_STRING=253, 6tN!'l+P  
FIELD_TYPE_STRING=254, 2wh/#  
FIELD_TYPE_GEOMETRY=255 m mZ.  
V3}PXO=C  
}; *7FLogi  
Vs?"@'g&  
///<summary> r?-2sCQF  
/// C prototypes warpper for mysqllib. ]3|LTB=wO  
///</summary> `I/ub  
internal class CPrototypes \!f YFt  
{ %eW`W/i]  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_init" )] jZ-PZMhFl  
unsafe public static extern void* mysql_init(void* must_be_null); r1]5!0EvNB  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_close" )] Su$Pi  
unsafe public static extern void mysql_close(void* handle); @vS xG$Tz  
"9i5TpH1  
// BEGIN ADDITION 2004-07-01 BY Alex Seewald YQC8O Q  
// Enables us to call mysql_option to activate compression and timeout Uamz\o  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_options" )] 5 k.D8%M  
unsafe public static extern void mysql_options(void* mysql, uint option, uint *value); fMFUyx!L  
// END ADDITION 2004-07-01 By Alex Seewald V4:Qd~+}  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_real_connect" )] ]t}}T '  
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); ";b`Dt-9k  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_query" )] 17Yzeyo  
unsafe public static extern int mysql_query(void*mysql, string query); ErSB8FL]  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_store_result" )]  4:l5+$  
unsafe public static extern void *mysql_store_result(void *mysql); ct$7{ 9  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_free_result" )] 6pkNC\a   
unsafe public static extern void mysql_free_result(void*result); ;(n0)  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_errno" )] yM6{+<}  
unsafe public static extern uint mysql_errno(void*mysql); _+BIqsZ[8q  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_error" )] Mr)GhH.\  
unsafe public static extern string mysql_error(void*mysql); ?rIqSfE_  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_field_count" )]  }yq  
unsafe public static extern uint mysql_field_count(void*mysql); bn3&D^"!  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_affected_rows" )] Xd?.}9|  
unsafe public static extern ulong mysql_affected_rows(void*mysql); S*~ >6>2P  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_num_fields" )] Zl6Kr8T5  
unsafe public static extern uint mysql_num_fields(void*result); !cI{j(_  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_num_rows" )] ;]S{Rw3K[  
unsafe public static extern ulong mysql_num_rows(void *result); $rNRB:  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_fetch_field_direct" )] S}Wmnx %  
unsafe public static extern IntPtr mysql_fetch_field_direct(void*result, uint fieldnr); x`((=!  
Ve`;7uY{s  
///<returns>Returns a string that represents the client library version</returns> (ghqCOT Xl  
[DllImport("libmySQL.dll",CharSet=System.Runtime.InteropServices.CharSet.Ansi, c12`"Ng  
EntryPoint="mysql_get_client_info", ExactSpelling=true)] W!grr}4OU9  
public static extern string GetClientInfo(); =q>F* !(N  
G8_AS|  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_fetch_row" )] w$@:Ss[d  
unsafe public static extern IntPtr mysql_fetch_row(void*result); cRhkNI$  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_select_db" )] 0n O38  
unsafe public static extern int mysql_select_db(void*mysql,string dbname); W[VeoO  
[ DllImport( "libmySQL.dll", EntryPoint="mysql_fetch_lengths" )] }tI` n Hc  
unsafe public static extern UInt32 *mysql_fetch_lengths(void*result); knVy6H  
jTE:i:aZN  
} a]pC6(g  
} m.r5 4(t  
=m"KiJ46  
q0tu$ j  
  基本上是将C风格的基础数据结构进行.net的重新定义,然后通过InteropServices进行访问。 n KcJ#y9  
lb{XRgdXDL  
  具体如何利用这个库进行操作,可以参考其中的例子。
作者:未知 | 文章来源:未知 | 更新时间:2007-11-8 11:50:13

  • 上一篇文章:

  • 下一篇文章:

  • 相关文章:
    Domino平台的优缺点分析
    使用经验整理-TestDirector8.0数据库备份和还原
    丢失日志文件后数据库还能不能恢复了
    在MySQL中 describe命令怎样使用
    如果忘记了MySQL的root用户的密码怎么办?
    c#如何连接各类数据库
    基础学习:基于SQL的sysColumns 数据库字段表
    处理一个数据库实例在登陆时报ORA-27140错误
    利用CodeSmith生成sql
    如何解决MySQL数据库中文问题
    软件技术
    · 开发语言
    · Java技术
    · .Net技术
    · 数据库开发
    最新文章  
    ·Domino平台的优缺点分析
    ·oracle不能连接本地库的解
    ·使用经验整理-TestDirecto
    ·学习sqlserver数据库的维度
    ·sqlserver 多用户并发中如
    ·丢失日志文件后数据库还能
    ·人工智能在数据库sql语句编
    ·数据库学习:Oracle应用程
    ·基础知识:软件测试的要点
    ·技术文章:sqlserver 2008
    ·SQL Server虚拟内存和物理
    ·在MySQL中 describe命令怎
    ·怎样解决视图刷新时出现的
    ·如果忘记了MySQL的root用户
    ·基础学习:基于SQL的sysob
    关于我们 | 软件开发 | 下载试用 | 客服中心 | 联系我们 | 友情链接 | 网站地图 | 新疆电子地图 | RSS订阅
    版权所有 © 2016 新疆二域软件开发网 www.k8w.net All Rights Reserved 新ICP备14003571号
    新疆软件开发总机:0991-4842803、4811639.
    客服QQ:596589785 ;地址:新疆乌鲁木齐北京中路华联大厦A-5C 邮编:830000