.NET訪問MySQL資料庫方法)

來源:互聯網
上載者:User

  .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);

  

  }

  

  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.