当前位置:首页 > 系统运维 > 正文内容

访问MySQL数据库用.NET!

a8116255316年前 (2010-05-20)系统运维11

以下的文章主要介绍的是用.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数据库的介绍,不知道各位对MySQL的认识是不是更上一层楼了,时时关注ITjs,学习最新Mysql技术。

扫描二维码推送至手机访问。

版权声明:本文由2345好导航站长资讯发布,如需转载请注明出处。

本文链接:http://www.2345hao.cn/blog/index.php/post/20471.html

分享给朋友:

“访问MySQL数据库用.NET!” 的相关文章

如何删除自带的不常用应用为windows 7减负

如何删除自带的不常用应用为windows 7减负

对于Windows 7系统来说,其默认安装的许多工具是我们很少使用或从来不用的,比如系统自带的扫雷、纸牌游戏等。删除此类长期不用的系统组件,不但可以让系统更清爽,更重要的是还可以提高系统的运行速度,特别是对于硬件配置相对较低的上网本来说,尤其如此。接下来,笔者就给大家介绍一下如何删除Windows...

Windows7开机后出现黑一下屏性能降低

Windows7开机后出现黑一下屏性能降低

打开IE浏览器的时候、聊QQ的时候、玩游戏的时候… … 这到底是谁惹的祸呢? 很多人都知道Windows 7桌面特效全部开启会使系统性能降低,那到底是哪个特效导致的这一情况呢? 解决办法: 右键计算机,高级系统设置-性能-设置-关闭”任务栏和开始菜单使用动画“和”最大化和最小化动态显示窗口“...

windows 7查看电脑近期使用情况确定有没有被他人使用

windows 7查看电脑近期使用情况确定有没有被他人使用

我一个朋友最近装装了Windows 7,他总感觉有人用了他的电脑,但是一直不确定,他想知道有没有人动用他的电脑,问我有没有办法 我说设置一下就行了: 启动Windows 7,在搜索栏中输入编辑组,马上就搜索到了编辑组策略,点击即可启动程序编辑组策略。依次展开组策略左侧树形列表的计算机配置/管理模...

windows 7系统件夹和文件都不显示名字如何解决

windows 7系统件夹和文件都不显示名字如何解决

假如碰到电脑里的文件夹和文件全都不显示名字了(如下图),是不是电脑中毒了呢 请教下笔者有没好的解决方法!下面是笔者给出的答案,希望能够帮助到大家! 首先打开你用来装图片的文件夹(缩略图下面的文字不显示的文件夹),然后但击工具栏上的“查看”选择“平铺”, 下面就是关键:要按住SHIFT键不...

Windows7截图出现黑屏导致截的图黑呼呼一片

Windows7截图出现黑屏导致截的图黑呼呼一片

最近一些Windows 7用户反馈说,自己在截屏的时候出现黑屏,导致自己截的图黑呼呼一片。这个问题该怎么解决?今天小编就为大家提供一个解决的方法。 步骤 1 在windows xp时代,很多用户都曾被视频截图后的“全黑图片”困扰。对此,在解决本地播放时出现的截图变黑问题,一般有两个方法可以用来解...

ubuntu14.04打开个几个应用窗口最小化后怎么切换呢?

ubuntu14.04打开个几个应用窗口最小化后怎么切换呢?

ubuntu14.04应用窗口列表在哪里?在ubuntu下打开个几个应用窗口最小化后怎么切换呢,应用窗口列表在哪里呢? 1、多打开几个chrome浏览器窗口。 2、然后都做最小化处理。 3、现在我们在桌面左边的面板上找到"左右两边有小白点"的chrome浏览器应用图标,这就是chr...