熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

如何使用 .NET訪問MySQL數據庫

2022-06-13   來源: .NET編程 

  NET的數據庫天然支持MSSQLServer但是並非其他數據庫不支持而是微軟基於自身利益需要在支持營銷上推自己的數據庫產品;但是作為平台戰略他並非排斥其他數據庫而是參考java體系提出了一套數據庫訪問規范讓各個第三方進行開發提供特定的驅動

  MySQL是免費的數據庫在成本上具有無可替代的優勢但是目前來講並沒有提供微軟把MySQL當作ODBC數據庫可以按照ODBCNet規范進行訪問具體參考

  

  而實際上針對ODBCNet的需要配置DSN的麻煩而是出現了一個開源的系統MySQLDriverCS對MySQL的開發進行了封裝實現環境下對於MySQL數據庫系統的訪問

  

  通過閱讀源代碼我們看到MySQLDriverCS的思路是利用C函數的底層庫來操縱數據庫的通常提供對MySQL數據庫的訪問的數據庫的C DLL是名為libmySQLdll的驅動文件MySQLDriverCS作為一庫進行封裝C風格的驅動

  具體如何進行呢?

  打開工程後我們看到其中有一個比較特殊的cs文件CPrototypescs

以下是引用片段

#region LICENSE
/*
MySQLDriverCS: An C# driver for MySQL
Copyright (c) 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 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 Temple Place Suite Boston MA USA

  */
#endregion
using System;
using SystemData;
using SystemRuntimeInteropServices;
namespace MySQLDriverCS
{

//[StructLayout(LayoutKindSequential)]
public class MYSQL_FIELD_FACTORY
{
static string version;
public static IMYSQL_FIELD GetInstance()
{

if (version==null)
{
version = CPrototypesGetClientInfo();
}
if (versionCompareTo(alpha)>=)
{
return new MYSQL_FIELD_VERSION_();
}
else
return new MYSQL_FIELD_VERSION_();
}
}
public interface IMYSQL_FIELD
{
string Name{get;}
uint Type{get;}
long Max_Length {get;}
}
///<summary>
/// Field descriptor
///</summary>
[StructLayout(LayoutKindSequential)]// alpha
internal class MYSQL_FIELD_VERSION_: 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_comh 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(LayoutKindSequential)]
internal class MYSQL_FIELD_VERSION_: 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 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>

  //[StructLayout(LayoutKindExplicit)]
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_LONGLONGFIELD_TYPE_INT
FIELD_TYPE_DATE FIELD_TYPE_TIME
FIELD_TYPE_DATETIME FIELD_TYPE_YEAR
FIELD_TYPE_NEWDATE
FIELD_TYPE_ENUM=
FIELD_TYPE_SET=
FIELD_TYPE_TINY_BLOB=
FIELD_TYPE_MEDIUM_BLOB=
FIELD_TYPE_LONG_BLOB=
FIELD_TYPE_BLOB=
FIELD_TYPE_VAR_STRING=
FIELD_TYPE_STRING=
FIELD_TYPE_GEOMETRY=

};

///<summary>
/// C prototypes warpper for mysqllib
///</summary>
internal class CPrototypes
{
[ DllImport( libmySQLdll EntryPoint=mysql_init )]
unsafe public static extern void* mysql_init(void* must_be_null);
[ DllImport( libmySQLdll EntryPoint=mysql_close )]
unsafe public static extern void mysql_close(void* handle);

// BEGIN ADDITION BY Alex Seewald
// Enables us to call mysql_option to activate compression and timeout
[ DllImport( libmySQLdll EntryPoint=mysql_options )]
unsafe public static extern void mysql_options(void* mysql uint option uint *value);
// END ADDITION By Alex Seewald

  [ DllImport( libmySQLdll 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( libmySQLdll EntryPoint=mysql_query )]
unsafe public static extern int mysql_query(void*mysql string query);
[ DllImport( libmySQLdll EntryPoint=mysql_store_result )]
unsafe public static extern void *mysql_store_result(void *mysql);
[ DllImport( libmySQLdll EntryPoint=mysql_free_result )]
unsafe public static extern void mysql_free_result(void*result);
[ DllImport( libmySQLdll EntryPoint=mysql_errno )]
unsafe public static extern uint mysql_errno(void*mysql);
[ DllImport( libmySQLdll EntryPoint=mysql_error )]
unsafe public static extern string mysql_error(void*mysql);
[ DllImport( libmySQLdll EntryPoint=mysql_field_count )]
unsafe public static extern uint mysql_field_count(void*mysql);
[ DllImport( libmySQLdll EntryPoint=mysql_affected_rows )]
unsafe public static extern ulong mysql_affected_rows(void*mysql);
[ DllImport( libmySQLdll EntryPoint=mysql_num_fields )]
unsafe public static extern uint mysql_num_fields(void*result);
[ DllImport( libmySQLdll EntryPoint=mysql_num_rows )]
unsafe public static extern ulong mysql_num_rows(void *result);
[ DllImport( libmySQLdll 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(libmySQLdllCharSet=SystemRuntimeInteropServicesCharSetAnsi
EntryPoint=mysql_get_client_info ExactSpelling=true)]
public static extern string GetClientInfo();

[ DllImport( libmySQLdll EntryPoint=mysql_fetch_row )]
unsafe public static extern IntPtr mysql_fetch_row(void*result);
[ DllImport( libmySQLdll EntryPoint=mysql_select_db )]
unsafe public static extern int mysql_select_db(void*mysqlstring dbname);
[ DllImport( libmySQLdll EntryPoint=mysql_fetch_lengths )]
unsafe public static extern UInt *mysql_fetch_lengths(void*result);

}
}


  基本上是將C風格的基礎數據結構進的重新定義然後通過InteropServices進行訪問

  具體如何利用這個庫進行操作可以參考其中的例子


From:http://tw.wingwit.com/Article/program/net/201311/11753.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.