NuGet:

  • Dapper 2.0.35
  • MySql.Data
  • System.Data

实体(Entity)

1    public class student
2     {
3         public int Id { get; set; }
4         public string RealName { get; set; }
5         public DateTime EnrollmenDate { get; set; }
6     }

 

配置文件数据库连接

  <connectionStrings>
    <add name="mysqlConn" connectionString="server=127.0.0.1;database=数据库;User ID=root;password=密码"/>
  </connectionStrings>

 

MySql连接字符串

1 private static string connStr = string.Empty;
2         public MySqlOperation()
3         {
4             connStr = System.Configuration.ConfigurationManager.ConnectionStrings["mysqlConn"].ConnectionString;
5         }

查询语句

 1         /// <summary>
 2         /// 查询所有
 3         /// </summary>
 4         /// <returns>lists</returns>
 5         public List<student> Query()
 6         {
 7             if (string.IsNullOrEmpty(connStr))
 8             {
 9                 return new List<student>(); ;
10             }
11             try
12             {
13                 using (IDbConnection db = new MySqlConnection(connStr))
14                 {
15                     db.Open();//打开数据库
16                     string mysqlStr = "select * from  student";
17                     List<student> lists = db.Query<student>(mysqlStr).ToList();
18                     db.Close();//关闭数据库
19                     return lists;
20                 }
21             }
22             catch (Exception)
23             {
24                 return new List<student>();
25             }
26         }
27         /// <summary>
28         /// 根据Id查询
29         /// </summary>
30         /// <param name="id"></param>
31         /// <returns>lists</returns>
32         public List<student> Query(string id)
33         {
34             if (string.IsNullOrEmpty(connStr))
35             {
36                 return new List<student>();
37             }
38             try
39             {
40                 using (IDbConnection db = new MySqlConnection(connStr))
41                 {
42                     db.Open();//打开数据库
43                     string mysqlStr = "select * from  student where Id=@id";
44                     List<student> lists = db.Query<student>(mysqlStr, new { Id = id }).ToList();
45                     db.Close();//关闭数据库
46                     return lists;
47                 }
48             }
49             catch (Exception)
50             {
51                 return new List<student>();
52             }
53         }

 

插入语句

 1         /// <summary>
 2         /// 插入数据
 3         /// </summary>
 4         /// <param name="student">student</param>
 5         /// <returns>-1:连接字符串为空,-2:新增失败</returns>
 6         public int Insert(student student)
 7         {
 8             if (string.IsNullOrEmpty(connStr))
 9             {
10                 return -1;
11             }
12             try
13             {
14                 using (IDbConnection db = new MySqlConnection(connStr))
15                 {
16                     db.Open();//打开数据库
17                     string mysqlStr = "insert into student(Id,RealName,EnrollmenDate) values(@Id,@RealName,@EnrollmenDate)";
18                     int list = db.Execute(mysqlStr, student);
19                     db.Close();//关闭数据库
20                     return list;
21                 }
22             }
23             catch (Exception)
24             {
25                 return -2;
26             }
27         }

 

删除数据

 1         /// <summary>
 2         /// 删除数据
 3         /// </summary>
 4         /// <param name="id"></param>
 5         /// <returns></returns>
 6         public int Delect(string id)
 7         {
 8             if (string.IsNullOrEmpty(connStr))
 9             {
10                 return -1;
11             }
12             try
13             {
14                 using (IDbConnection db = new MySqlConnection(connStr))
15                 {
16                     db.Open();//打开数据库
17                     string mysqlStr = "delete from student where Id=@id";
18                     int list = db.Execute(mysqlStr, new { Id = id });
19                     db.Close();//关闭数据库
20                     return list;
21                 }
22             }
23             catch (Exception)
24             {
25                 return -2;
26             }
27         }

更新数据

 1         /// <summary>
 2         /// 更新信息
 3         /// </summary>
 4         /// <param name="student"></param>
 5         /// <returns>-1:连接字符串为空,-2:更新失败</returns>
 6         public int Update(student student)
 7         {
 8             if (string.IsNullOrEmpty(connStr))
 9             {
10                 return -1;
11             }
12             try
13             {
14                 using (IDbConnection db = new MySqlConnection(connStr))
15                 {
16                     db.Open();//打开数据库
17                     string mysqlStr = "update student set RealName=@RealName where Id=@Id";
18                     int list = db.Execute(mysqlStr, student);
19                     db.Close();//关闭数据库
20                     return list;
21                 }
22             }
23             catch (Exception)
24             {
25                 return -2;
26             }
27         }