<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[静怡家园]]></title> 
<link>http://www.zhanghaijun.com/index.php</link> 
<description><![CDATA[书山有路勤为径，学海无涯苦作舟！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[静怡家园]]></copyright>
<item>
<link>http://www.zhanghaijun.com/post//</link>
<title><![CDATA[.NET如何访问MySQL数据库]]></title> 
<author>碟舞飞扬 &lt;webmaster@zhanghaijun.com&gt;</author>
<category><![CDATA[Windows相关]]></category>
<pubDate>Wed, 30 Jul 2008 12:50:18 +0000</pubDate> 
<guid>http://www.zhanghaijun.com/post//</guid> 
<description>
<![CDATA[ 
	.NET的数据库天然支持MSSQLServer，但是并非其他数据库不支持，而是微软基于自身利益需要，在支持、营销上推自己的数据库产品;但是作为平台战略，他并非排斥其他数据库，而是参考java体系提出了一套数据库访问规范，让各个第三方进行开发，提供特定的驱动。 <br/><br/>　　MySQL是免费的数据库，在成本上具有无可替代的优势，但是目前来讲，并没有提供。微软把MySQL当作ODBC数据库，可以按照ODBC.Net规范进行访问，具体参考 <br/><br/>　　http://www.microsoft.com/china/community/Columns/Luyan/6.mspx <br/><br/>　　而实际上，针对ODBC。Net的需要配置DSN的麻烦，而是出现了一个开源的系统MySQLDriverCS，对MySQL的开发进行了封装，实现了.net环境下对于MySQL数据库系统的访问。 <br/><br/>　　http://sourceforge.net/projects/mysqldrivercs/ <br/><br/>　　通过阅读源代码，我们看到MySQLDriverCS的思路是利用C函数的底层库来操纵数据库的，通常提供对MySQL数据库的访问的数据库的C DLL是名为libmySQL.dll的驱动文件，MySQLDriverCS作为一个.net库进行封装C风格的驱动。 <br/><br/>　　具体如何进行呢? <br/><br/>　　打开工程后，我们看到其中有一个比较特殊的.cs文件CPrototypes.cs： <br/><br/>以下是引用片段： <br/><br/>#region LICENSE <br/>/* <br/>MySQLDriverCS: An C# driver for MySQL. <br/>Copyright (c) 2002 Manuel Lucas Vi馻s Livschitz. <br/><br/>This file is part of MySQLDriverCS. <br/><br/>MySQLDriverCS is free software; you can redistribute it and/or modify <br/>it under the terms of the GNU General Public License as published by <br/>the Free Software Foundation; either version 2 of the License, o <br/>(at your option) any later version. <br/><br/>MySQLDriverCS is distributed in the hope that it will be useful, <br/>but WITHOUT ANY WARRANTY; without even the implied warranty of <br/>MERCHANTABILITY o FITNESS FOR A PARTICULAR PURPOSE. See the <br/>GNU General Public License for more details. <br/><br/>You should have received a copy of the GNU General Public License <br/>along with MySQLDriverCS; if not, write to the Free Software <br/>Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02-307 USA <br/>*/ <br/>#endregion <br/>using System; <br/>using System.Data; <br/>using System.Runtime.InteropServices; <br/>namespace MySQLDriverCS <br/>&#123; <br/><br/>//[StructLayout(LayoutKind.Sequential)] <br/>public class MYSQL_FIELD_FACTORY <br/>&#123; <br/>static string version; <br/>public static IMYSQL_FIELD GetInstance() <br/>&#123; <br/><br/>if (version==null) <br/>&#123; <br/>version = CPrototypes.GetClientInfo(); <br/>&#125; <br/>if (version.CompareTo("4..2-alpha")＞=0) <br/>&#123; <br/>return new MYSQL_FIELD_VERSION_5(); <br/>&#125; <br/>else <br/>return new MYSQL_FIELD_VERSION_3(); <br/>&#125; <br/>&#125; <br/>public interface IMYSQL_FIELD <br/>&#123; <br/>string Name&#123;get;&#125; <br/>uint Type&#123;get;&#125; <br/>long Max_Length &#123;get;&#125; <br/>&#125; <br/>///＜summary＞ <br/>/// Field descriptor <br/>///＜/summary＞ <br/>[StructLayout(LayoutKind.Sequential)]//"3.23.32", 4.0.-alpha <br/>internal class MYSQL_FIELD_VERSION_3: IMYSQL_FIELD <br/>&#123; <br/>///＜summary＞ <br/>/// Name of column <br/>///＜/summary＞ <br/>public string name; <br/>///＜summary＞ <br/>/// Table of column if column was a field <br/>///＜/summary＞ <br/>public string table; <br/>//public string og_table; /* og table name if table was an alias */ <br/>//public string db; /* Database for table */ <br/>///＜summary＞ <br/>/// def <br/>///＜/summary＞ <br/>public string def; <br/>///＜summary＞ <br/>/// length <br/>///＜/summary＞ <br/>public long length; <br/>///＜summary＞ <br/>/// max_length <br/>///＜/summary＞ <br/>public long max_length; <br/>///＜summary＞ <br/>/// Div flags <br/>///＜/summary＞ <br/>public uint flags; <br/>///＜summary＞ <br/>/// Number of decimals in field <br/>///＜/summary＞ <br/>public uint decimals; <br/>///＜summary＞ <br/>/// Type of field. Se mysql_com.h for types <br/>///＜/summary＞ <br/>public uint type; <br/><br/>///＜summary＞ <br/>/// Name <br/>///＜/summary＞ <br/>public string Name <br/>&#123; <br/>get&#123;return name;&#125; <br/>&#125; <br/>///＜summary＞ <br/>/// Type <br/>///＜/summary＞ <br/>public uint Type <br/>&#123; <br/>get&#123;return type;&#125; <br/>&#125; <br/>///＜summary＞ <br/>/// Max_Length <br/>///＜/summary＞ <br/>public long Max_Length <br/>&#123; <br/>get &#123;return max_length;&#125; <br/>&#125; <br/>&#125; <br/><br/>///＜summary＞ <br/>/// Field descriptor <br/>///＜/summary＞ <br/>[StructLayout(LayoutKind.Sequential)] <br/>internal class MYSQL_FIELD_VERSION_5: IMYSQL_FIELD <br/>&#123; <br/>///＜summary＞ <br/>/// Name of column <br/>///＜/summary＞ <br/>public string name; <br/>///＜summary＞ <br/>/// oiginal column name, if an alias <br/>///＜/summary＞ <br/>public string og_name; <br/>///＜summary＞ <br/>/// Table of column if column was a field <br/>///＜/summary＞ <br/>public string table; <br/>///＜summary＞ <br/>/// og table name if table was an alias <br/>///＜/summary＞ <br/>public string og_table; <br/>///＜summary＞ <br/>/// Database for table <br/>///＜/summary＞ <br/>public string db; <br/>///＜summary＞ <br/>/// Catalog for table <br/>///＜/summary＞ <br/>//public string catalog; <br/>///＜summary＞ <br/>/// def <br/>///＜/summary＞ <br/>public string def; <br/>///＜summary＞ <br/>/// length <br/>///＜/summary＞ <br/>public long length; <br/>///＜summary＞ <br/>/// max_length <br/>///＜/summary＞ <br/>public long max_length; <br/>///＜summary＞ <br/>/// name_length <br/>///＜/summary＞ <br/>//public uint name_length; <br/>///＜summary＞ <br/>/// og_name_length <br/>///＜/summary＞ <br/>public uint og_name_length; <br/>///＜summary＞ <br/>/// table_length <br/>///＜/summary＞ <br/>public uint table_length; <br/>///＜summary＞ <br/>/// og_table_length <br/>///＜/summary＞ <br/>public uint og_table_length; <br/>///＜summary＞ <br/>/// db_length <br/>///＜/summary＞ <br/>public uint db_length; <br/>///＜summary＞ <br/>/// catalog_length <br/>///＜/summary＞ <br/>public uint catalog_length; <br/>///＜summary＞ <br/>/// def_length <br/>///＜/summary＞ <br/>public uint def_length; <br/>///＜summary＞ <br/>/// Div flags <br/>///＜/summary＞ <br/>public uint flags; <br/>///＜summary＞ <br/>/// Number of decimals in field <br/>///＜/summary＞ <br/>public uint decimals; <br/>///＜summary＞ <br/>/// Character set <br/>///＜/summary＞ <br/>public uint charsetnr; <br/>///＜summary＞ <br/>/// Type of field. Se mysql_com.h for types <br/>///＜/summary＞ <br/>public uint type; <br/><br/>///＜summary＞ <br/>/// Name <br/>///＜/summary＞ <br/>public string Name <br/>&#123; <br/>get &#123;return name;&#125; <br/>&#125; <br/>///＜summary＞ <br/>/// Type <br/>///＜/summary＞ <br/>public uint Type <br/>&#123; <br/>get &#123;return type;&#125; <br/>&#125; <br/>///＜summary＞ <br/>/// Max_Length <br/>///＜/summary＞ <br/>public long Max_Length <br/>&#123; <br/>get &#123;return max_length;&#125; <br/>&#125; <br/>&#125; <br/>//[StructLayout(LayoutKind.Explicit)] <br/>public enum enum_field_types <br/>&#123; <br/>FIELD_TYPE_DECIMAL, FIELD_TYPE_TINY, <br/>FIELD_TYPE_SHORT, FIELD_TYPE_LONG, <br/>FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE, <br/>FIELD_TYPE_NULL, FIELD_TYPE_TIMESTAMP, <br/>FIELD_TYPE_LONGLONG,FIELD_TYPE_INT24, <br/>FIELD_TYPE_DATE, FIELD_TYPE_TIME, <br/>FIELD_TYPE_DATETIME, FIELD_TYPE_YEAR, <br/>FIELD_TYPE_NEWDATE, <br/>FIELD_TYPE_ENUM=247, <br/>FIELD_TYPE_SET=248, <br/>FIELD_TYPE_TINY_BLOB=249, <br/>FIELD_TYPE_MEDIUM_BLOB=250, <br/>FIELD_TYPE_LONG_BLOB=25, <br/>FIELD_TYPE_BLOB=252, <br/>FIELD_TYPE_VAR_STRING=253, <br/>FIELD_TYPE_STRING=254, <br/>FIELD_TYPE_GEOMETRY=255 <br/><br/>&#125;; <br/><br/>///＜summary＞ <br/>/// C prototypes warpper for mysqllib. <br/>///＜/summary＞ <br/>internal class CPrototypes <br/>&#123; <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_init" )] <br/>unsafe public static extern void* mysql_init(void* must_be_null); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_close" )] <br/>unsafe public static extern void mysql_close(void* handle); <br/><br/>// BEGIN ADDITION 2004-07-0 BY Alex Seewald <br/>// Enables us to call mysql_option to activate compression and timeout <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_options" )] <br/>unsafe public static extern void mysql_options(void* mysql, uint option, uint *value); <br/>// END ADDITION 2004-07-0 By Alex Seewald <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_real_connect" )] <br/>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); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_query" )] <br/>unsafe public static extern int mysql_query(void*mysql, string query); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_store_result" )] <br/>unsafe public static extern void *mysql_store_result(void *mysql); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_free_result" )] <br/>unsafe public static extern void mysql_free_result(void*result); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_errno" )] <br/>unsafe public static extern uint mysql_errno(void*mysql); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_error" )] <br/>unsafe public static extern string mysql_error(void*mysql); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_field_count" )] <br/>unsafe public static extern uint mysql_field_count(void*mysql); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_affected_rows" )] <br/>unsafe public static extern ulong mysql_affected_rows(void*mysql); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_num_fields" )] <br/>unsafe public static extern uint mysql_num_fields(void*result); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_num_rows" )] <br/>unsafe public static extern ulong mysql_num_rows(void *result); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_fetch_field_direct" )] <br/>unsafe public static extern IntPtr mysql_fetch_field_direct(void*result, uint fieldnr); <br/><br/>///＜returns＞Returns a string that represents the client library version＜/returns＞ <br/>[DllImport("libmySQL.dll",CharSet=System.Runtime.InteropServices.CharSet.Ansi, <br/>EntryPoint="mysql_get_client_info", ExactSpelling=true)] <br/>public static extern string GetClientInfo(); <br/><br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_fetch_row" )] <br/>unsafe public static extern IntPtr mysql_fetch_row(void*result); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_sel&#0;ct_db" )] <br/>unsafe public static extern int mysql_sel&#0;ct_db(void*mysql,string dbname); <br/>[ DllImport( "libmySQL.dll", EntryPoint="mysql_fetch_lengths" )] <br/>unsafe public static extern UInt32 *mysql_fetch_lengths(void*result); <br/><br/>&#125; <br/>&#125; <br/>　　基本上是将C风格的基础数据结构进行.net的重新定义，然后通过InteropServices进行访问。 <br/><br/>　　具体如何利用这个库进行操作，可以参考其中的例子。<br/>Tags - <a href="http://www.zhanghaijun.com/tags/mysql/" rel="tag">mysql</a>
]]>
</description>
</item><item>
<link>http://www.zhanghaijun.com/post//#blogcomment</link>
<title><![CDATA[[评论] .NET如何访问MySQL数据库]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.zhanghaijun.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>