Dapper基本用法--MySql

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     }

 

配置文件数据库连接

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

 

MySql连接字符串

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

查询语句

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

 

插入语句

 1         /// 
 2         /// 插入数据
 3         /// 
 4         /// student
 5         /// -1:连接字符串为空,-2:新增失败
 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         /// 
 2         /// 删除数据
 3         /// 
 4         /// 
 5         /// 
 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         /// 
 2         /// 更新信息
 3         /// 
 4         /// 
 5         /// -1:连接字符串为空,-2:更新失败
 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         }

 

你可能感兴趣的:(Dapper基本用法--MySql)