C#安装 MySQL 驱动以及实现简单的 MySQL 操作 by 草梅友仁 一、驱动下载 首先前往 MySql 官网下载驱动。下载地址:https://dev.mysql.com/downloads/connector/net/
注意驱动版本即可。注意:要下载.zip 版本,解压后会看到几个 dll,那个就是需要的驱动
最新的 8.0.16 版本驱动要求.net 版本在 4.5 以上,如果需要 4.0 版本的则需要 6.8.8 版本,选择 4.0 版本驱动即可
由于本人安装的 Visual Studio 2010 最高只支持.net4.0,因此只能使用旧版驱动。不管哪个版本,MySQL 的核心驱动都是 MySql.Data.dll,使用的时候只需要引入这个即可
二、驱动引入
新建一个项目,名称随意。
找到项目位置,在项目根目录下新建 lib 文件夹
将 MySql.Data.dll 复制到该目录下
回到 Visual Studio 2010 中,右键添加添加引用
选择浏览,找到 MySql.Data.dll,选中并点击确定
三、连接数据库
新建一个 Database.cs 类,并引用 MySql.Data.MySqlClient 类。注意类名前添加 public,否则无法在其他类中调用这个类
写一个初始化方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class Database { static MySqlConnection conn; const String server = "localhost" ; const String uid = "test" ; const String pw = "123456" ; const String db = "test" ; public static Boolean Init () { try { if (conn == null ) { conn = new MySqlConnection("server=" + server + ";user id=" + uid + ";password=" + pw + ";database=" + db); conn.Open(); Console.WriteLine("数据库连接成功" ); } return true ; } catch (Exception e) { Console.WriteLine("Exception caught: {0}" , e); return false ; } } }
3. 在主程序中调用该方法,观察控制台是否有输出,如果显示“数据库连接成功”则表明数据库连接已成功,操作正确。
四、编写数据库操作辅助类 对数据库的操作有很多重复的内容,因此这些可以像初始化方法这样提取出来。
查询操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public static MySqlDataReader Select (String sql ){ try { Init(); MySqlCommand command = new MySqlCommand(sql, conn); MySqlDataReader data = command.ExecuteReader(); return data; } catch (Exception e) { Console.WriteLine("Exception caught: {0}" , e); return null ; } }
增删改操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public static Boolean DbOp (String sql ){ try { Init(); MySqlCommand command = new MySqlCommand(sql, conn); int result = command.ExecuteNonQuery(); return result != 0 ; } catch (Exception e) { Console.WriteLine("Exception caught: {0}" , e); return false ; } }
实现字符串的转义
1 2 3 4 5 6 7 8 9 10 11 public static String Escape (Object str ){ if (str.GetType() == "" .GetType()) { return "'" + MySqlHelper.EscapeString(str.ToString()) + "'" ; } return str.ToString(); }
五、实现简单的增删查改功能
数据表名称:user
字段:id,类型 int;name,类型 text;pw,类型 text
如果要用 sql 语句创建,可参考如下,也可以用可视化工具创建
1 2 3 4 5 CREATE TABLE IF NOT EXISTS `user `( `id` INT AUTO_INCREMENT PRIMARY KEY , `name` TEXT NOT NULL , `pw` TEXT NOT NULL )ENGINE= InnoDB DEFAULT CHARSET= utf8;
在项目中新建一个 User.cs 类,内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 using System;using System.Collections.Generic;using System.Linq;using System.Text;using MySql.Data.MySqlClient;namespace MySqlTest { class User { public int id; public String name; public String pw; public User () { } public User (MySqlDataReader data ) { this .id = data.GetInt16("id" ); this .name = data.GetString("name" ); this .pw = data.GetString("pw" ); } public User (String name, String pw ) { this .name = name; this .pw = pw; } public User (int id, String name, String pw ) { this .id = id; this .name = name; this .pw = pw; } } }
实现 user 的查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public static User SelectUser (int id ){ string sql = "SELECT * from user WHERE id = " + id; MySqlDataReader data = Select(sql); if (data != null && data.Read()) { User user = new User(data); data.Close(); return user; } else { if (data != null ) { data.Close(); } return null ; } }
3.实现 user 的增加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public static User AddUser (User user ){ String sql = "INSERT into user(id,name,pw) values(0," + Escape(user.name) + "," + Escape(user.pw) + ")" ; if (DbOp(sql)) { return SelectUser(user.name); } else { return null ; } }
4.实现 user 的更新
1 2 3 4 5 6 7 8 public static Boolean UpdateUser (User user ){ String sql = "UPDATE user SET name = " + Escape(user.name) + ",pw = " + Escape(user.pw) + " WHERE id = " + user.id; return DbOp(sql); }
5.实现 user 的删除
1 2 3 4 5 6 7 8 public static Boolean DeleteUser (int id ){ String sql = "DELETE from user WHERE id = " + id; return DbOp(sql); }
本文作者:草梅友仁 本文地址: https://blog.cmyr.ltd/archives/9925ae75.html 版权声明:本文采用 CC BY-NC-SA 4.0 协议 进行分发,转载请注明出处!