以下的文章主要介紹的是用.NET訪問MySQL數據庫的實際操作步驟,我們大家都知道 .NET的數據庫本身就支持mssql(WINDOWS平台上強大的數據庫平台)Server,但是並不是其他數據庫不支持,而是微軟基於自身利益需要。
在支持、營銷上推自己的MySQL數據庫產品;但是作為平台戰略,他並非排斥其他數據庫,而是參考java體系提出了一套數據庫訪問規范,讓各個第三方進行開發,提供特定的驅動。
MySQL(和PHP搭配之最佳組合)是免費的數據庫,在成本上具有無可替代的優勢,但是目前來講,並沒有提供。微軟把MySQL(和PHP搭配之最佳組合)當作ODBC數據庫,可以按照ODBC.Net規范進行訪問,具體參考
而實際上,針對ODBC。Net的需要配置DSN的麻煩,而是出現了一個開源的系統MySQL(和PHP搭配之最佳組合)DriverCS,對MySQL(和PHP搭配之最佳組合)的開發進行了封裝,實現了.net環境下對於MySQL數據庫系統的訪問。
通過閱讀源代碼,我們看到MySQL(和PHP搭配之最佳組合)DriverCS的思路是利用C函數的底層庫來操縱數據庫的,通常提供對MySQL(和PHP搭配之最佳組合)數據庫的訪問的MySQL數據庫的C DLL是名為libMySQL(和PHP搭配之最佳組合).dll的驅動文件,MySQL(和PHP搭配之最佳組合)DriverCS作為一個.net庫進行封裝C風格的驅動。
具體如何進行呢?
打開工程後,我們看到其中有一個比較特殊的.cs文件CPrototypes.cs:
以下是引用片段:
- #region LICENSE
- /*
- MySQL(和PHP搭配之最佳組合)DriverCS: An C# driver for MySQL(和PHP搭配之最佳組合).
- Copyright (c) 2002 Manuel Lucas Vi馻s Livschitz.
- This file is part of MySQL(和PHP搭配之最佳組合)DriverCS.
- MySQL(和PHP搭配之最佳組合)DriverCS 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.
- MySQL(和PHP搭配之最佳組合)DriverCS 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 MySQL(和PHP搭配之最佳組合)DriverCS; 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 MySQL(和PHP搭配之最佳組合)DriverCS
- {
- //[StructLayout(LayoutKind.Sequential)]
- public class MySQL(和PHP搭配之最佳組合)_FIELD_FACTORY
- {
- static string version;
- public static IMySQL(和PHP搭配之最佳組合)_FIELD GetInstance()
- {
- if (version==null)
- {
- version = CPrototypes.GetClientInfo();
- }
- if (version.CompareTo("4.1.2-alpha")>=0)
- {
- return new MySQL(和PHP搭配之最佳組合)_FIELD_VERSION_5();
- }
- else
- return new MySQL(和PHP搭配之最佳組合)_FIELD_VERSION_3();
- }
- }
- public interface IMySQL(和PHP搭配之最佳組合)_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(和PHP搭配之最佳組合)_FIELD_VERSION_3: IMySQL(和PHP搭配之最佳組合)_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(和PHP搭配之最佳組合)_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(和PHP搭配之最佳組合)_FIELD_VERSION_5: IMySQL(和PHP搭配之最佳組合)_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(和PHP搭配之最佳組合)_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 MySQL(和PHP搭配之最佳組合)lib.
- ///</summary>
- internal class CPrototypes
- {
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_init" )]
- unsafe public static extern void* MySQL(和PHP搭配之最佳組合)_init(void* must_be_null);
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_close" )]
- unsafe public static extern void MySQL(和PHP搭配之最佳組合)_close(void* handle);
- // BEGIN ADDITION 2004-07-01 BY Alex Seewald
- // Enables us to call MySQL(和PHP搭配之最佳組合)_option to activate compression and timeout
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_options" )]
- unsafe public static extern void MySQL(和PHP搭配之最佳組合)_options(void* MySQL(和PHP搭配之最佳組合), uint option, uint *value);
- // END ADDITION 2004-07-01 By Alex Seewald
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_real_connect" )]
- unsafe public static extern void* MySQL(和PHP搭配之最佳組合)_real_connect(void* MySQL(和PHP搭配之最佳組合),
string host, string user, string passwd, string db, uint port, string unix_socket, int client_flag);- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_query" )]
- unsafe public static extern int MySQL(和PHP搭配之最佳組合)_query(void*MySQL(和PHP搭配之最佳組合), string query);
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_store_result" )]
- unsafe public static extern void *MySQL(和PHP搭配之最佳組合)_store_result(void *MySQL(和PHP搭配之最佳組合));
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_free_result" )]
- unsafe public static extern void MySQL(和PHP搭配之最佳組合)_free_result(void*result);
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_errno" )]
- unsafe public static extern uint MySQL(和PHP搭配之最佳組合)_errno(void*MySQL(和PHP搭配之最佳組合));
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_error" )]
- unsafe public static extern string MySQL(和PHP搭配之最佳組合)_error(void*MySQL(和PHP搭配之最佳組合));
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_field_count" )]
- unsafe public static extern uint MySQL(和PHP搭配之最佳組合)_field_count(void*MySQL(和PHP搭配之最佳組合));
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_affected_rows" )]
- unsafe public static extern ulong MySQL(和PHP搭配之最佳組合)_affected_rows(void*MySQL(和PHP搭配之最佳組合));
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_num_fields" )]
- unsafe public static extern uint MySQL(和PHP搭配之最佳組合)_num_fields(void*result);
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_num_rows" )]
- unsafe public static extern ulong MySQL(和PHP搭配之最佳組合)_num_rows(void *result);
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_fetch_field_direct" )]
- unsafe public static extern IntPtr MySQL(和PHP搭配之最佳組合)_fetch_field_direct(void*result, uint fieldnr);
- ///<returns>Returns a string that represents the client library version</returns>
- [DllImport("libMySQL(和PHP搭配之最佳組合).dll",CharSet=System.Runtime.InteropServices.CharSet.Ansi,
- EntryPoint="MySQL(和PHP搭配之最佳組合)_get_client_info", ExactSpelling=true)]
- public static extern string GetClientInfo();
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_fetch_row" )]
- unsafe public static extern IntPtr MySQL(和PHP搭配之最佳組合)_fetch_row(void*result);
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_select_db" )]
- unsafe public static extern int MySQL(和PHP搭配之最佳組合)_select_db(void*MySQL(和PHP搭配之最佳組合),string dbname);
- [ DllImport( "libMySQL(和PHP搭配之最佳組合).dll", EntryPoint="MySQL(和PHP搭配之最佳組合)_fetch_lengths" )]
- unsafe public static extern UInt32 *MySQL(和PHP搭配之最佳組合)_fetch_lengths(void*result);
- }
- }
基本上是將C風格的基礎數據結構進行.net的重新定義,然後通過InteropServices進行訪問。具體如何利用這個庫進行操作,可以參考其中的例子。以上的相關內容就是對.NET如何訪問MySQL數據庫的介紹,望你能有所收獲。