>

批量插入,in的解决方案

- 编辑:金沙国际平台登录 -

批量插入,in的解决方案

1. 批量插入

public async Task CreateBusinessItemAsync(IEnumerable<BusinessItemsEntity> businessItemsEntities)
{
var bizid = businessItemsEntities.First().BizId;
await _connection.DbConnection.ExecuteAsync("delete from BusinessItems where bizid=@BizId;", new { bizId= bizid });


string sql = @"INSERT INTO BusinessItems
([BizId]
,[ExpiredTime]
,[DisposeTime]
,[Remark]
,[ProductCategoryCode],Status)
values
(@BizId
,@ExpiredTime
,@DisposeTime,@Remark, @ProductCategoryCode,@Status);";
await _connection.DbConnection.ExecuteAsync(sql, businessItemsEntities);

对, 便是那样暴躁, 直接把列表传进去就到位了

 

2. 批量更新.

参考

  1. where in 批量更新, style="color: #ff0000">那时候具备需求更新的值都以三个大同小异的值

图片 1

  1. 一旦每条语句更新的值都不平等吧

图片 2

简单的说记一下,一会出去多少事情~

3. 歪曲查询

方法1 https://stackoverflow.com/questions/6030099/does-dapper-support-the-like-operator

db.Query<Remitente>("SELECT *                      FROM Remitentes                      WHERE Nombre LIKE @n", new { n = "%" + nombre + "%" })                   .ToList();

方法2

大家一般写sql都是==》update NoteInfo set NDataStatus=@NDataStatus where NId in (@NIds)

Dapper生成的sql是:

图片 3

也就自然出错了:

图片 4

 

一句话来讲修改一下:

int i = await NoteInfoBLL.ExecuteAsync("update NoteInfo set NDataStatus=@NDataStatus where NId in @NIds", new
{
  NDataStatus = status,
  NIds = ids.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
});

Dapper生成sql:(类型再转移一下就ok了,那边只是说下方法,先闪了)

exec sp_executesql N'update NoteInfo set NDataStatus=@NDataStatus where NId in (@NIds1,@NIds2,@NIds3)',N'@NDataStatus int,@NIds1 nvarchar(4000),@NIds2 nvarchar(4000),@NIds3 nvarchar(4000)',@NDataStatus=99,@NIds1=N'1',@NIds2=N'2',@NIds3=N'3'

本文由编程发布,转载请注明来源:批量插入,in的解决方案