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

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、数据库连接配置

<connectionStrings> 
  <add name="MysqlServer" connectionString="Database=用数据库名称;Data Source=IP;Port=端口; User Id=用户名;Password=密码;Charset=utf8mb4;TreatTinyAsBoolean=false;" /> //Mysql 
 
  <add name="LinqConnection" connectionString="Data Source=IP;Initial Catalog=数据库名称; User ID=用户名;Password=密码" providerName="System.Data.SqlClient"/> //sql
</connectionStrings>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> 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> 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> carModel) 
{ 
  using (IDbConnection conn = DapperService.MySqlConnection())
   { 
    return conn.Execute("update UserInfo set name=@name where id=@ID", carModel); 
  } 
}
7、查询​

无参查询

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

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

In操作

public static List<CarModel> QueryIn() 
{ 
  using (IDbConnection conn = DapperService.MySqlConnection()) 
  { 
    var sql = "select * from UserInfo where id in @ids"; 
     return conn.Query<CarModel>(sql, new { ids = new int[2] { 1, 2 }, }).ToList(); 
  } 
}
 
public static List<CarModel> QueryIn(int[] ids) 
{ 
  using (IDbConnection conn = DapperService.MySqlConnection()) 
  { 
    var sql = "select * from UserInfo where id in @ids";
    return conn.Query<CarModel>(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<Person>(); 
      var bookList = multiReader.Read<CarModel>(); multiReader.Dispose(); 
    } 
    return Json("success"); 
  } 
  catch(Exception e) 
  { 
    return Json("failed"); 
  } 
}

 

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、数据库连接配置

  1.  
    <connectionStrings>
  2.  
      <add name="MysqlServer" connectionString="Database=用数据库名称;Data Source=IP;Port=端口; User Id=用户名;Password=密码;Charset=utf8mb4;TreatTinyAsBoolean=false;" /> //Mysql
  3.  
     
  4.  
      <add name="LinqConnection" connectionString="Data Source=IP;Initial Catalog=数据库名称; User ID=用户名;Password=密码" providerName="System.Data.SqlClient"/> //sql
  5.  
    </connectionStrings>

​3、dapper 数据库连接方法​

Mysql连接方法:

  1.  
    public class DapperService
  2.  
    {
  3.  
     
  4.  
      public static MySqlConnection MySqlConnection()
  5.  
      {
  6.  
         string mysqlConnectionStr = ConfigurationManager.ConnectionStrings["MysqlServer"].ToString();
  7.  
         var connection = new MySqlConnection(mysqlConnectionStr); connection.Open();
  8.  
        return connection;
  9.  
      }
  10.  
    }

mssql连接方法:

  1.  
    public class DapperService {
  2.  
     
  3.  
      public static SqlConnection MySqlConnection()
  4.  
      {
  5.  
     
  6.  
        string mysqlConnectionStr =ConfigurationManager.ConnectionStrings["LinqConnection"].ToString();
  7.  
        var connection = new SqlConnection(mysqlConnectionStr);
  8.  
        connection.Open();
  9.  
        return connection;
  10.  
      }
  11.  
    }

​4、新增方法(单体、批量)​

单体:

  1.  
    public ActionResult GetDapper(CarModel carModel)
  2.  
    {
  3.  
      try
  4.  
      {
  5.  
        using (IDbConnection conn = DapperService.MySqlConnection())
  6.  
       {
  7.  
          int result = conn.Execute("insert into UserInfo(UserTel,UserName,UserPwd) values (@UserTel,@UserName,@UserPwd)", carModel);
  8.  
        }
  9.  
        return Json("success");
  10.  
      }
  11.  
      catch(Exception e)
  12.  
      {
  13.  
       return Json("failed");
  14.  
      }
  15.  
    }

批量:

  1.  
    public ActionResult GetDapper(List<CarModel> carModel)
  2.  
    {
  3.  
     try
  4.  
      {
  5.  
        using (IDbConnection conn = DapperService.MySqlConnection())
  6.  
        {
  7.  
          int result = conn.Execute("insert into UserInfo(UserTel,UserName,UserPwd) values(@UserTel,@UserName,@UserPwd)", carModel);
  8.  
        }
  9.  
        return Json("success");
  10.  
      }
  11.  
      catch(Exception e)
  12.  
      {
  13.  
        return Json("failed");
  14.  
      }
  15.  
    }

​5、删除方法(单体、批量)

单体:

  1.  
    public static int Delete(CarModel carModel)
  2.  
    {
  3.  
      using (IDbConnection conn = DapperService.MySqlConnection())
  4.  
      {
  5.  
       return conn.Execute("delete from UserInfo where id=@ID", carModel);
  6.  
      }
  7.  
    }

批量:

  1.  
    public static int Delete(List<CarModel> carModel)
  2.  
    {
  3.  
      using (IDbConnection conn = DapperService.MySqlConnection())
  4.  
      {
  5.  
        return conn.Execute("delete from UserInfo where id=@ID", carModel);
  6.  
      }
  7.  
    }

​6、更新方法(单体、批量)​

单体:

  1.  
    public static int Update(CarModel carModel)
  2.  
    {
  3.  
      using (IDbConnection conn = DapperService.MySqlConnection())
  4.  
      {
  5.  
        return conn.Execute("update UserInfo set name=@name where id=@ID", carModel);
  6.  
      }
  7.  
    }

批量:

  1.  
    public static int Update(List<CarModel> carModel)
  2.  
    {
  3.  
      using (IDbConnection conn = DapperService.MySqlConnection())
  4.  
      {
  5.  
        return conn.Execute("update UserInfo set name=@name where id=@ID", carModel);
  6.  
      }
  7.  
    }

7、查询​

无参查询

  1.  
    public static List<CarModel> Query()
  2.  
    {
  3.  
      using (IDbConnection conn = DapperService.MySqlConnection())
  4.  
      {
  5.  
    return conn.Query<CarModel>("select * from UserInfo ").ToList();
  6.  
    }
  7.  
    }

有参查询

  1.  
    public static Person Query(CarModel carModel)
  2.  
    {
  3.  
      using (IDbConnection conn = DapperService.MySqlConnection())
  4.  
      {
  5.  
        return conn.Query<CarModel>("select * from UserInfo where
  6.  
    id=@ID",carModel).SingleOrDefault();
  7.  
      }
  8.  
    }

​8、Dapper的复杂操作​

In操作

  1.  
    public static List<CarModel> QueryIn()
  2.  
    {
  3.  
      using (IDbConnection conn = DapperService.MySqlConnection())
  4.  
      {
  5.  
        var sql = "select * from UserInfo where id in @ids";
  6.  
        return conn.Query<CarModel>(sql, new { ids = new int[2] { 1, 2 }, }).ToList();
  7.  
      }
  8.  
    }
  9.  
     
  10.  
    public static List<CarModel> QueryIn(int[] ids)
  11.  
    {
  12.  
      using (IDbConnection conn = DapperService.MySqlConnection())
  13.  
      {
  14.  
        var sql = "select * from UserInfo where id in @ids";
  15.  
        return conn.Query<CarModel>(sql, new { ids }).ToList();
  16.  
      }
  17.  
    }

9、多语句操作

  1.  
    public ActionResult QueryMultiple()
  2.  
    {
  3.  
      try
  4.  
      {
  5.  
        using (IDbConnection conn = DapperService.MySqlConnection())
  6.  
        {
  7.  
          var sql= "select * from Person; select * from UserInfo";
  8.  
          var multiReader = conn.QueryMultiple(sql);
  9.  
          var personList = multiReader.Read<Person>();
  10.  
          var bookList = multiReader.Read<CarModel>(); multiReader.Dispose();
  11.  
        }
  12.  
        return Json("success");
  13.  
      }
  14.  
      catch(Exception e)
  15.  
      {
  16.  
        return Json("failed");
  17.  
      }
  18.  
    }
posted @ 2023-02-26 16:47  跟着阿笨一起玩.NET  阅读(1231)  评论(0编辑  收藏  举报