|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
因为各系统的API不同,代码调用API编写程序就会遇到很多不兼容的地方,比如Java改写后的Serv-U就不能在手机上执行,手机的游戏也不能直接在微机上执行。mysql|会见|技能|数据|数据库.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数据库的会见的数据库的CDLL是名为libmySQL.dll的驱动文件,MySQLDriverCS作为一个.net库举行封装C作风的驱动。
详细怎样举行呢?
翻开工程后,我们看到个中有一个对照特别的.cs文件CPrototypes.cs:
以下是援用片断:
#regionLICENSE
/*
MySQLDriverCS:AnC#driverforMySQL.
Copyright(c)2002ManuelLucasViasLivschitz.
ThisfileispartofMySQLDriverCS.
MySQLDriverCSisfreesoftware;youcanredistributeitand/ormodify
itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
theFreeSoftwareFoundation;eitherversion2oftheLicense,or
(atyouroption)anylaterversion.
MySQLDriverCSisdistributedinthehopethatitwillbeuseful,
butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
GNUGeneralPublicLicenseformoredetails.
YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
alongwithMySQLDriverCS;ifnot,writetotheFreeSoftware
Foundation,Inc.,59TemplePlace,Suite330,Boston,MA02111-1307USA
*/
#endregion
usingSystem;
usingSystem.Data;
usingSystem.Runtime.InteropServices;
namespaceMySQLDriverCS
{
//[StructLayout(LayoutKind.Sequential)]
publicclassMYSQL_FIELD_FACTORY
{
staticstringversion;
publicstaticIMYSQL_FIELDGetInstance()
{
if(version==null)
{
version=CPrototypes.GetClientInfo();
}
if(version.CompareTo("4.1.2-alpha")>=0)
{
returnnewMYSQL_FIELD_VERSION_5();
}
else
returnnewMYSQL_FIELD_VERSION_3();
}
}
publicinterfaceIMYSQL_FIELD
{
stringName{get;}
uintType{get;}
longMax_Length{get;}
}
///<summary>
///Fielddescriptor
///</summary>
[StructLayout(LayoutKind.Sequential)]//"3.23.32",4.0.1-alpha
internalclassMYSQL_FIELD_VERSION_3:IMYSQL_FIELD
{
///<summary>
///Nameofcolumn
///</summary>
publicstringname;
///<summary>
///Tableofcolumnifcolumnwasafield
///</summary>
publicstringtable;
//publicstringorg_table;/*Orgtablenameiftablewasanalias*/
//publicstringdb;/*Databasefortable*/
///<summary>
///def
///</summary>
publicstringdef;
///<summary>
///length
///</summary>
publiclonglength;
///<summary>
///max_length
///</summary>
publiclongmax_length;
///<summary>
///Divflags
///</summary>
publicuintflags;
///<summary>
///Numberofdecimalsinfield
///</summary>
publicuintdecimals;
///<summary>
///Typeoffield.Semysql_com.hfortypes
///</summary>
publicuinttype;
///<summary>
///Name
///</summary>
publicstringName
{
get{returnname;}
}
///<summary>
///Type
///</summary>
publicuintType
{
get{returntype;}
}
///<summary>
///Max_Length
///</summary>
publiclongMax_Length
{
get{returnmax_length;}
}
}
///<summary>
///Fielddescriptor
///</summary>
[StructLayout(LayoutKind.Sequential)]
internalclassMYSQL_FIELD_VERSION_5:IMYSQL_FIELD
{
///<summary>
///Nameofcolumn
///</summary>
publicstringname;
///<summary>
///Originalcolumnname,ifanalias
///</summary>
publicstringorg_name;
///<summary>
///Tableofcolumnifcolumnwasafield
///</summary>
publicstringtable;
///<summary>
///Orgtablenameiftablewasanalias
///</summary>
publicstringorg_table;
///<summary>
///Databasefortable
///</summary>
publicstringdb;
///<summary>
///Catalogfortable
///</summary>
//publicstringcatalog;
///<summary>
///def
///</summary>
publicstringdef;
///<summary>
///length
///</summary>
publiclonglength;
///<summary>
///max_length
///</summary>
publiclongmax_length;
///<summary>
///name_length
///</summary>
//publicuintname_length;
///<summary>
///org_name_length
///</summary>
publicuintorg_name_length;
///<summary>
///table_length
///</summary>
publicuinttable_length;
///<summary>
///org_table_length
///</summary>
publicuintorg_table_length;
///<summary>
///db_length
///</summary>
publicuintdb_length;
///<summary>
///catalog_length
///</summary>
publicuintcatalog_length;
///<summary>
///def_length
///</summary>
publicuintdef_length;
///<summary>
///Divflags
///</summary>
publicuintflags;
///<summary>
///Numberofdecimalsinfield
///</summary>
publicuintdecimals;
///<summary>
///Characterset
///</summary>
publicuintcharsetnr;
///<summary>
///Typeoffield.Semysql_com.hfortypes
///</summary>
publicuinttype;
///<summary>
///Name
///</summary>
publicstringName
{
get{returnname;}
}
///<summary>
///Type
///</summary>
publicuintType
{
get{returntype;}
}
///<summary>
///Max_Length
///</summary>
publiclongMax_Length
{
get{returnmax_length;}
}
}
//[StructLayout(LayoutKind.Explicit)]
publicenumenum_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>
///Cprototypeswarpperformysqllib.
///</summary>
internalclassCPrototypes
{
[DllImport("libmySQL.dll",EntryPoint="mysql_init")]
unsafepublicstaticexternvoid*mysql_init(void*must_be_null);
[DllImport("libmySQL.dll",EntryPoint="mysql_close")]
unsafepublicstaticexternvoidmysql_close(void*handle);
//BEGINADDITION2004-07-01BYAlexSeewald
//Enablesustocallmysql_optiontoactivatecompressionandtimeout
[DllImport("libmySQL.dll",EntryPoint="mysql_options")]
unsafepublicstaticexternvoidmysql_options(void*mysql,uintoption,uint*value);
//ENDADDITION2004-07-01ByAlexSeewald
[DllImport("libmySQL.dll",EntryPoint="mysql_real_connect")]
unsafepublicstaticexternvoid*mysql_real_connect(void*mysql,stringhost,stringuser,stringpasswd,stringdb,uintport,stringunix_socket,intclient_flag);
[DllImport("libmySQL.dll",EntryPoint="mysql_query")]
unsafepublicstaticexternintmysql_query(void*mysql,stringquery);
[DllImport("libmySQL.dll",EntryPoint="mysql_store_result")]
unsafepublicstaticexternvoid*mysql_store_result(void*mysql);
[DllImport("libmySQL.dll",EntryPoint="mysql_free_result")]
unsafepublicstaticexternvoidmysql_free_result(void*result);
[DllImport("libmySQL.dll",EntryPoint="mysql_errno")]
unsafepublicstaticexternuintmysql_errno(void*mysql);
[DllImport("libmySQL.dll",EntryPoint="mysql_error")]
unsafepublicstaticexternstringmysql_error(void*mysql);
[DllImport("libmySQL.dll",EntryPoint="mysql_field_count")]
unsafepublicstaticexternuintmysql_field_count(void*mysql);
[DllImport("libmySQL.dll",EntryPoint="mysql_affected_rows")]
unsafepublicstaticexternulongmysql_affected_rows(void*mysql);
[DllImport("libmySQL.dll",EntryPoint="mysql_num_fields")]
unsafepublicstaticexternuintmysql_num_fields(void*result);
[DllImport("libmySQL.dll",EntryPoint="mysql_num_rows")]
unsafepublicstaticexternulongmysql_num_rows(void*result);
[DllImport("libmySQL.dll",EntryPoint="mysql_fetch_field_direct")]
unsafepublicstaticexternIntPtrmysql_fetch_field_direct(void*result,uintfieldnr);
///<returns>Returnsastringthatrepresentstheclientlibraryversion</returns>
[DllImport("libmySQL.dll",CharSet=System.Runtime.InteropServices.CharSet.Ansi,
EntryPoint="mysql_get_client_info",ExactSpelling=true)]
publicstaticexternstringGetClientInfo();
[DllImport("libmySQL.dll",EntryPoint="mysql_fetch_row")]
unsafepublicstaticexternIntPtrmysql_fetch_row(void*result);
[DllImport("libmySQL.dll",EntryPoint="mysql_select_db")]
unsafepublicstaticexternintmysql_select_db(void*mysql,stringdbname);
[DllImport("libmySQL.dll",EntryPoint="mysql_fetch_lengths")]
unsafepublicstaticexternUInt32*mysql_fetch_lengths(void*result);
}
}
基础上是将C作风的基本数据布局举行.net的从头界说,然后经由过程InteropServices举行会见。
详细怎样使用这个库举行操纵,能够参考个中的例子。效率会有不少的变化。而实际上java是基于堆栈机器来设计,这和我们常见的基于寄存器的本地机器是差异比较大的。总体来说,这是一种虚拟机的设计思路。 |
|