欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

访问MySQL数据库用.NET!

程序员文章站 2022-05-14 23:26:46
...

以下的文章主要介绍的是用.NET访问MySQL数据库的实际操作步骤,我们大家都知道.NET的数据库本身就支持mssql(WINDOWS平台上强大的数据库平台)Server,但是并不是其他数据库不支持,而是微软基于自身利益需要。 在支持、营销上推自己的MySQL数据库产品;但是作

以下的文章主要介绍的是用.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:

以下是引用片段:

  1. #region LICENSE
  2. /*
  3. MySQL(和PHP搭配之最佳组合)DriverCS: An C# driver for MySQL(和PHP搭配之最佳组合).
  4. Copyright (c) 2002 Manuel Lucas Vi馻s Livschitz.
  5. This file is part of MySQL(和PHP搭配之最佳组合)DriverCS.
  6. MySQL(和PHP搭配之最佳组合)DriverCS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. MySQL(和PHP搭配之最佳组合)DriverCS is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with MySQL(和PHP搭配之最佳组合)DriverCS; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #endregion
  19. using System;
  20. using System.Data;
  21. using System.Runtime.InteropServices;
  22. namespace MySQL(和PHP搭配之最佳组合)DriverCS
  23. {
  24. //[StructLayout(LayoutKind.Sequential)]
  25. public class MySQL(和PHP搭配之最佳组合)_FIELD_FACTORY
  26. {
  27. static string version;
  28. public static IMySQL(和PHP搭配之最佳组合)_FIELD GetInstance()
  29. {
  30. if (version==null)
  31. {
  32. version = CPrototypes.GetClientInfo();
  33. }
  34. if (version.CompareTo("4.1.2-alpha")>=0)
  35. {
  36. return new MySQL(和PHP搭配之最佳组合)_FIELD_VERSION_5();
  37. }
  38. else
  39. return new MySQL(和PHP搭配之最佳组合)_FIELD_VERSION_3();
  40. }
  41. }
  42. public interface IMySQL(和PHP搭配之最佳组合)_FIELD
  43. {
  44. string Name{get;}
  45. uint Type{get;}
  46. long Max_Length {get;}
  47. }
  48. ///summary>
  49. /// Field descriptor
  50. ///summary>
  51. [StructLayout(LayoutKind.Sequential)]//"3.23.32", 4.0.1-alpha
  52. internal class MySQL(和PHP搭配之最佳组合)_FIELD_VERSION_3: IMySQL(和PHP搭配之最佳组合)_FIELD
  53. {
  54. ///summary>
  55. /// Name of column
  56. ///summary>
  57. public string name;
  58. ///summary>
  59. /// Table of column if column was a field
  60. ///summary>
  61. public string table;
  62. //public string org_table; /* Org table name if table was an alias */
  63. //public string db; /* Database for table */
  64. ///summary>
  65. /// def
  66. ///summary>
  67. public string def;
  68. ///summary>
  69. /// length
  70. ///summary>
  71. public long length;
  72. ///summary>
  73. /// max_length
  74. ///summary>
  75. public long max_length;
  76. ///summary>
  77. /// Div flags
  78. ///summary>
  79. public uint flags;
  80. ///summary>
  81. /// Number of decimals in field
  82. ///summary>
  83. public uint decimals;
  84. ///summary>
  85. /// Type of field. Se MySQL(和PHP搭配之最佳组合)_com.h for types
  86. ///summary>
  87. public uint type;
  88. ///summary>
  89. /// Name
  90. ///summary>
  91. public string Name
  92. {
  93. get{return name;}
  94. }
  95. ///summary>
  96. /// Type
  97. ///summary>
  98. public uint Type
  99. {
  100. get{return type;}
  101. }
  102. ///summary>
  103. /// Max_Length
  104. ///summary>
  105. public long Max_Length
  106. {
  107. get {return max_length;}
  108. }
  109. }
  110. ///summary>
  111. /// Field descriptor
  112. ///summary>
  113. [StructLayout(LayoutKind.Sequential)]
  114. internal class MySQL(和PHP搭配之最佳组合)_FIELD_VERSION_5: IMySQL(和PHP搭配之最佳组合)_FIELD
  115. {
  116. ///summary>
  117. /// Name of column
  118. ///summary>
  119. public string name;
  120. ///summary>
  121. /// Original column name, if an alias
  122. ///summary>
  123. public string org_name;
  124. ///summary>
  125. /// Table of column if column was a field
  126. ///summary>
  127. public string table;
  128. ///summary>
  129. /// Org table name if table was an alias
  130. ///summary>
  131. public string org_table;
  132. ///summary>
  133. /// Database for table
  134. ///summary>
  135. public string db;
  136. ///summary>
  137. /// Catalog for table
  138. ///summary>
  139. //public string catalog;
  140. ///summary>
  141. /// def
  142. ///summary>
  143. public string def;
  144. ///summary>
  145. /// length
  146. ///summary>
  147. public long length;
  148. ///summary>
  149. /// max_length
  150. ///summary>
  151. public long max_length;
  152. ///summary>
  153. /// name_length
  154. ///summary>
  155. //public uint name_length;
  156. ///summary>
  157. /// org_name_length
  158. ///summary>
  159. public uint org_name_length;
  160. ///summary>
  161. /// table_length
  162. ///summary>
  163. public uint table_length;
  164. ///summary>
  165. /// org_table_length
  166. ///summary>
  167. public uint org_table_length;
  168. ///summary>
  169. /// db_length
  170. ///summary>
  171. public uint db_length;
  172. ///summary>
  173. /// catalog_length
  174. ///summary>
  175. public uint catalog_length;
  176. ///summary>
  177. /// def_length
  178. ///summary>
  179. public uint def_length;
  180. ///summary>
  181. /// Div flags
  182. ///summary>
  183. public uint flags;
  184. ///summary>
  185. /// Number of decimals in field
  186. ///summary>
  187. public uint decimals;
  188. ///summary>
  189. /// Character set
  190. ///summary>
  191. public uint charsetnr;
  192. ///summary>
  193. /// Type of field. Se MySQL(和PHP搭配之最佳组合)_com.h for types
  194. ///summary>
  195. public uint type;
  196. ///summary>
  197. /// Name
  198. ///summary>
  199. public string Name
  200. {
  201. get {return name;}
  202. }
  203. ///summary>
  204. /// Type
  205. ///summary>
  206. public uint Type
  207. {
  208. get {return type;}
  209. }
  210. ///summary>
  211. /// Max_Length
  212. ///summary>
  213. public long Max_Length
  214. {
  215. get {return max_length;}
  216. }
  217. }
  218. //[StructLayout(LayoutKind.Explicit)]
  219. public enum enum_field_types
  220. {
  221. FIELD_TYPE_DECIMAL, FIELD_TYPE_TINY,
  222. FIELD_TYPE_SHORT, FIELD_TYPE_LONG,
  223. FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE,
  224. FIELD_TYPE_NULL, FIELD_TYPE_TIMESTAMP,
  225. FIELD_TYPE_LONGLONG,FIELD_TYPE_INT24,
  226. FIELD_TYPE_DATE, FIELD_TYPE_TIME,
  227. FIELD_TYPE_DATETIME, FIELD_TYPE_YEAR,
  228. FIELD_TYPE_NEWDATE,
  229. FIELD_TYPE_ENUM=247,
  230. FIELD_TYPE_SET=248,
  231. FIELD_TYPE_TINY_BLOB=249,
  232. FIELD_TYPE_MEDIUM_BLOB=250,
  233. FIELD_TYPE_LONG_BLOB=251,
  234. FIELD_TYPE_BLOB=252,
  235. FIELD_TYPE_VAR_STRING=253,
  236. FIELD_TYPE_STRING=254,
  237. FIELD_TYPE_GEOMETRY=255
  238. };
  239. ///summary>
  240. /// C prototypes warpper for MySQL(和PHP搭配之最佳组合)lib.
  241. ///summary>
  242. internal class CPrototypes
  243. {
  244. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_init" )]
  245. unsafe public static extern void* MySQL(和PHP搭配之最佳组合)_init(void* must_be_null);
  246. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_close" )]
  247. unsafe public static extern void MySQL(和PHP搭配之最佳组合)_close(void* handle);
  248. // BEGIN ADDITION 2004-07-01 BY Alex Seewald
  249. // Enables us to call MySQL(和PHP搭配之最佳组合)_option to activate compression and timeout
  250. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_options" )]
  251. unsafe public static extern void MySQL(和PHP搭配之最佳组合)_options(void* MySQL(和PHP搭配之最佳组合), uint option, uint *value);
  252. // END ADDITION 2004-07-01 By Alex Seewald
  253. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_real_connect" )]
  254. 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);
  255. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_query" )]
  256. unsafe public static extern int MySQL(和PHP搭配之最佳组合)_query(void*MySQL(和PHP搭配之最佳组合), string query);
  257. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_store_result" )]
  258. unsafe public static extern void *MySQL(和PHP搭配之最佳组合)_store_result(void *MySQL(和PHP搭配之最佳组合));
  259. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_free_result" )]
  260. unsafe public static extern void MySQL(和PHP搭配之最佳组合)_free_result(void*result);
  261. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_errno" )]
  262. unsafe public static extern uint MySQL(和PHP搭配之最佳组合)_errno(void*MySQL(和PHP搭配之最佳组合));
  263. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_error" )]
  264. unsafe public static extern string MySQL(和PHP搭配之最佳组合)_error(void*MySQL(和PHP搭配之最佳组合));
  265. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_field_count" )]
  266. unsafe public static extern uint MySQL(和PHP搭配之最佳组合)_field_count(void*MySQL(和PHP搭配之最佳组合));
  267. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_affected_rows" )]
  268. unsafe public static extern ulong MySQL(和PHP搭配之最佳组合)_affected_rows(void*MySQL(和PHP搭配之最佳组合));
  269. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_num_fields" )]
  270. unsafe public static extern uint MySQL(和PHP搭配之最佳组合)_num_fields(void*result);
  271. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_num_rows" )]
  272. unsafe public static extern ulong MySQL(和PHP搭配之最佳组合)_num_rows(void *result);
  273. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_fetch_field_direct" )]
  274. unsafe public static extern IntPtr MySQL(和PHP搭配之最佳组合)_fetch_field_direct(void*result, uint fieldnr);
  275. ///returns>Returns a string that represents the client library versionreturns>
  276. [DllImport("libMySQL(和PHP搭配之最佳组合).dll",CharSet=System.Runtime.InteropServices.CharSet.Ansi,
  277. EntryPoint="MySQL(和PHP搭配之最佳组合)_get_client_info", ExactSpelling=true)]
  278. public static extern string GetClientInfo();
  279. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_fetch_row" )]
  280. unsafe public static extern IntPtr MySQL(和PHP搭配之最佳组合)_fetch_row(void*result);
  281. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_select_db" )]
  282. unsafe public static extern int MySQL(和PHP搭配之最佳组合)_select_db(void*MySQL(和PHP搭配之最佳组合),string dbname);
  283. [ DllImport( "libMySQL(和PHP搭配之最佳组合).dll", EntryPoint="MySQL(和PHP搭配之最佳组合)_fetch_lengths" )]
  284. unsafe public static extern UInt32 *MySQL(和PHP搭配之最佳组合)_fetch_lengths(void*result);
  285. }
  286. }

基本上是将C风格的基础数据结构进行.net的重新定义,然后通过InteropServices进行访问。具体如何利用这个库进行操作,可以参考其中的例子。以上的相关内容就是对.NET如何访问MySQL数据库的介绍,望你能有所收获。