翼度科技»论坛 编程开发 .net 查看内容

C#/.NET/.NET Core Dapper 批量删除、新增、修改说明

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
  1. Dapper是什么?
  2.   Dapper是一款.Net平台简单(Simple)的对象映射库,并且Dapper拥有着“微型ORM之王”的称号。
  3.   就速度而言与手写ADO.NET SqlDateReader相同。
  4.   ORM是Object Relational Mapper的缩写,代表着数据库与编程语言之间的映射。
  5.   简单来说就是使用Entity Framework、NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用牛刀,那么Dapper会是不错的选择。
  6. ​1、Dapper基本用法​
  7. 通过NuGet安装:Dapper包,如果要进行mysql连接还需要安装Mysql.Data包
  8. ​2、数据库连接配置
  9. <connectionStrings>
  10.   <add name="MysqlServer" connectionString="Database=用数据库名称;Data Source=IP;Port=端口; User Id=用户名;Password=密码;Charset=utf8mb4;TreatTinyAsBoolean=false;" /> //Mysql
  11.   <add name="LinqConnection" connectionString="Data Source=IP;Initial Catalog=数据库名称; User ID=用户名;Password=密码" providerName="System.Data.SqlClient"/> //sql
  12. </connectionStrings>
  13. ​3、dapper 数据库连接方法​
  14. Mysql连接方法:
  15. public class DapperService
  16. {
  17.   public static MySqlConnection MySqlConnection()
  18.   {
  19.      string mysqlConnectionStr = ConfigurationManager.ConnectionStrings["MysqlServer"].ToString();
  20.      var connection = new MySqlConnection(mysqlConnectionStr); connection.Open();
  21.      return connection;
  22.   }
  23. }
  24. mssql连接方法:
  25. public class DapperService {
  26.   public static SqlConnection MySqlConnection()
  27.   {
  28.     string mysqlConnectionStr =ConfigurationManager.ConnectionStrings["LinqConnection"].ToString();
  29.     var connection = new SqlConnection(mysqlConnectionStr);
  30.     connection.Open();
  31.     return connection;
  32.   }
  33. }
  34. ​4、新增方法(单体、批量)​
  35. 单体:
  36. public ActionResult GetDapper(CarModel carModel)
  37. {
  38.   try
  39.   {
  40.     using (IDbConnection conn = DapperService.MySqlConnection())
  41.    {
  42.       int result = conn.Execute("insert into UserInfo(UserTel,UserName,UserPwd) values (@UserTel,@UserName,@UserPwd)", carModel);
  43.     }
  44.     return Json("success");
  45.   }
  46.   catch(Exception e)
  47.   {
  48.    return Json("failed");
  49.   }
  50. }
  51. 批量:
  52. public ActionResult GetDapper(List<CarModel> carModel)
  53. {
  54.  try
  55.   {
  56.     using (IDbConnection conn = DapperService.MySqlConnection())
  57.     {
  58.       int result = conn.Execute("insert into UserInfo(UserTel,UserName,UserPwd) values(@UserTel,@UserName,@UserPwd)", carModel);
  59.     }
  60.     return Json("success");
  61.   }
  62.   catch(Exception e)
  63.   {
  64.     return Json("failed");
  65.   }
  66. }
  67. ​5、删除方法(单体、批量)
  68. 单体:
  69. public static int Delete(CarModel carModel)
  70. {
  71.   using (IDbConnection conn = DapperService.MySqlConnection())
  72.   {
  73.    return conn.Execute("delete from UserInfo where id=@ID", carModel);
  74.   }
  75. }
  76. 批量:
  77. public static int Delete(List<CarModel> carModel)
  78. {
  79.   using (IDbConnection conn = DapperService.MySqlConnection())
  80.   {
  81.     return conn.Execute("delete from UserInfo where id=@ID", carModel);
  82.   }
  83. }
  84. ​6、更新方法(单体、批量)​
  85. 单体:
  86. public static int Update(CarModel carModel)
  87. {
  88.   using (IDbConnection conn = DapperService.MySqlConnection())
  89.   {
  90.     return conn.Execute("update UserInfo set name=@name where id=@ID", carModel);
  91.   }
  92. }
  93. 批量:
  94. public static int Update(List<CarModel> carModel)
  95. {
  96.   using (IDbConnection conn = DapperService.MySqlConnection())
  97.   {
  98.     return conn.Execute("update UserInfo set name=@name where id=@ID", carModel);
  99.   }
  100. }
  101. 7、查询​
  102. 无参查询
  103. public static List<CarModel> Query()
  104. {
  105.   using (IDbConnection conn = DapperService.MySqlConnection())
  106.   {
  107.           return conn.Query<CarModel>("select * from UserInfo ").ToList();
  108.     }
  109. }
  110. 有参查询
  111. public static Person Query(CarModel carModel)
  112. {
  113.   using (IDbConnection conn = DapperService.MySqlConnection())
  114.   {
  115.     return conn.Query<CarModel>("select * from UserInfo where
  116.               id=@ID",carModel).SingleOrDefault();
  117.   }
  118. }
  119. ​8、Dapper的复杂操作​
  120. In操作
  121. public static List<CarModel> QueryIn()
  122. {
  123.   using (IDbConnection conn = DapperService.MySqlConnection())
  124.   {
  125.     var sql = "select * from UserInfo where id in @ids";
  126.     return conn.Query<CarModel>(sql, new { ids = new int[2] { 1, 2 }, }).ToList();
  127.   }
  128. }
  129. public static List<CarModel> QueryIn(int[] ids)
  130. {
  131.   using (IDbConnection conn = DapperService.MySqlConnection())
  132.   {
  133.     var sql = "select * from UserInfo where id in @ids";
  134.     return conn.Query<CarModel>(sql, new { ids }).ToList();
  135.   }
  136. }
  137. 9、多语句操作
  138. public ActionResult QueryMultiple()
  139. {
  140.   try
  141.   {  
  142.     using (IDbConnection conn = DapperService.MySqlConnection())
  143.     {
  144.       var sql= "select * from Person; select * from UserInfo";
  145.       var multiReader = conn.QueryMultiple(sql);
  146.       var personList = multiReader.Read<Person>();
  147.       var bookList = multiReader.Read<CarModel>(); multiReader.Dispose();
  148.     }
  149.     return Json("success");
  150.   }
  151.   catch(Exception e)
  152.   {
  153.     return Json("failed");
  154.   }
  155. }
复制代码
 
Dapper是什么?
  Dapper是一款.Net平台简单(Simple)的对象映射库,并且Dapper拥有着“微型ORM之王”的称号。
  就速度而言与手写ADO.NET SqlDateReader相同。
  ORM是Object Relational Mapper的缩写,代表着数据库与编程语言之间的映射。
  简单来说就是使用Entity Framework、NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用牛刀,那么Dapper会是不错的选择。
​1、Dapper基本用法​
通过NuGet安装:Dapper包,如果要进行mysql连接还需要安装Mysql.Data包
​2、数据库连接配置

  •  
  •     //Mysql
  •   
  •     //sql
  •  
​3、dapper 数据库连接方法​
Mysql连接方法:

  •  public class DapperService
  •  {
  •   
  •    public static MySqlConnection MySqlConnection()
  •    {
  •       string mysqlConnectionStr = ConfigurationManager.ConnectionStrings["MysqlServer"].ToString();
  •       var connection = new MySqlConnection(mysqlConnectionStr); connection.Open();
  •        return connection;
  •     }
  •  }
mssql连接方法:

  •  public class DapperService {
  •   
  •    public static SqlConnection MySqlConnection()
  •     {
  •   
  •      string mysqlConnectionStr =ConfigurationManager.ConnectionStrings["LinqConnection"].ToString();
  •      var connection = new SqlConnection(mysqlConnectionStr);
  •      connection.Open();
  •      return connection;
  •    }
  •  }
​4、新增方法(单体、批量)​
单体:

  •  public ActionResult GetDapper(CarModel carModel)
  •  {
  •    try
  •    {
  •      using (IDbConnection conn = DapperService.MySqlConnection())
  •     {
  •        int result = conn.Execute("insert into UserInfo(UserTel,UserName,UserPwd) values (@UserTel,@UserName,@UserPwd)", carModel);
  •      }
  •      return Json("success");
  •    }
  •    catch(Exception e)
  •    {
  •     return Json("failed");
  •    }
  •  }
批量:

  •  public ActionResult GetDapper(List carModel)
  •  {
  •   try
  •    {
  •      using (IDbConnection conn = DapperService.MySqlConnection())
  •      {
  •        int result = conn.Execute("insert into UserInfo(UserTel,UserName,UserPwd) values(@UserTel,@UserName,@UserPwd)", carModel);
  •      }
  •      return Json("success");
  •    }
  •    catch(Exception e)
  •    {
  •      return Json("failed");
  •    }
  •  }
​5、删除方法(单体、批量)
单体:

  •  public static int Delete(CarModel carModel)
  •  {
  •    using (IDbConnection conn = DapperService.MySqlConnection())
  •    {
  •     return conn.Execute("delete from UserInfo where id=@ID", carModel);
  •    }
  •  }
批量:

  •  public static int Delete(List carModel)
  •  {
  •    using (IDbConnection conn = DapperService.MySqlConnection())
  •    {
  •      return conn.Execute("delete from UserInfo where id=@ID", carModel);
  •    }
  •  }
​6、更新方法(单体、批量)​
单体:

  •  public static int Update(CarModel carModel)
  •  {
  •    using (IDbConnection conn = DapperService.MySqlConnection())
  •    {
  •       return conn.Execute("update UserInfo set name=@name where id=@ID", carModel);
  •     }
  •  }
批量:

  •  public static int Update(List carModel)
  •  {
  •    using (IDbConnection conn = DapperService.MySqlConnection())
  •    {
  •      return conn.Execute("update UserInfo set name=@name where id=@ID", carModel);
  •    }
  •  }
7、查询​
无参查询

  •  public static List Query()
  •  {
  •    using (IDbConnection conn = DapperService.MySqlConnection())
  •    {
  •      return conn.Query("select * from UserInfo ").ToList();
  •  }
  •  }
有参查询

  •  public static Person Query(CarModel carModel)
  •  {
  •    using (IDbConnection conn = DapperService.MySqlConnection())
  •    {
  •      return conn.Query("select * from UserInfo where
  •                id=@ID",carModel).SingleOrDefault();
  •    }
  •  }
​8、Dapper的复杂操作​
In操作

  •  public static List QueryIn()
  •  {
  •    using (IDbConnection conn = DapperService.MySqlConnection())
  •    {
  •      var sql = "select * from UserInfo where id in @ids";
  •      return conn.Query(sql, new { ids = new int[2] { 1, 2 }, }).ToList();
  •    }
  •  }
  •   
  •  public static List QueryIn(int[] ids)
  •  {
  •    using (IDbConnection conn = DapperService.MySqlConnection())
  •    {
  •      var sql = "select * from UserInfo where id in @ids";
  •      return conn.Query(sql, new { ids }).ToList();
  •    }
  •  }
9、多语句操作

  •  public ActionResult QueryMultiple()
  •  {
  •    try
  •    {
  •      using (IDbConnection conn = DapperService.MySqlConnection())
  •      {
  •        var sql= "select * from Person; select * from UserInfo";
  •        var multiReader = conn.QueryMultiple(sql);
  •        var personList = multiReader.Read();
  •        var bookList = multiReader.Read(); multiReader.Dispose();
  •      }
  •      return Json("success");
  •    }
  •    catch(Exception e)
  •    {
  •      return Json("failed");
  •    }
  •  }

来源:https://www.cnblogs.com/51net/archive/2023/02/26/17157012.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具