C# 如何在Dapper中使用一条sql语句传递多条记录以进行更新

C# 如何在Dapper中使用一条sql语句传递多条记录以进行更新,c#,sql-server,dapper,C#,Sql Server,Dapper,我尝试使用一个Update语句来更新多个具有不同值的记录,我不尝试更新许多行以使其具有相同的值,这是非常简单的。以下是我现在正在尝试的: using (var cn = GetOpenConnection()) { // get items where we need to set calculated fields that will now be persisted in the DB var items = cn.Query<Maintena

我尝试使用一个Update语句来更新多个具有不同值的记录,我不尝试更新许多行以使其具有相同的值,这是非常简单的。以下是我现在正在尝试的:

    using (var cn = GetOpenConnection()) {

        // get items where we need to set calculated fields that will now be persisted in the DB
        var items = cn.Query<MaintenanceItem>("select TOP 500 * from [Maintenance] where Tolerance IS NOT NULL");

        foreach (var mi in maintItems)
        {
            // Set calculated fields on multiple recrods
            logic.CalculateToleranceFields(mi, true);
        }


        var updateInput = items.Select(a => new {a.ToleranceMonths, a.ToleranceDays, a.ToleranceHours, a.ToleranceLandings, a.ToleranceCycles, a.ToleranceRIN }).ToList();

       // THIS DOESN'T WORK - attempting to update multiple rows with different values
       var numResults = cn.Execute(@"UPDATE rm 
                SET rm.ToleranceMonths=ur.ToleranceMonths, 
                rm.ToleranceDays=ur.ToleranceDays, 
                rm.ToleranceHours=ur.ToleranceHours, 
                rm.ToleranceLandings=ur.ToleranceLandings, 
                rm.ToleranceCycles=ur.ToleranceCycles, 
                rm.ToleranceRIN=ur.ToleranceRIN 
            from [RoutineItems] rm
            Inner Join @UpdatedRecords ur ON rm.AircraftId=ur.AircraftId AND rm.ItemNumber=ur.ItemNumber", updateInput);

        Assert.IsTrue(numResults == maintItems.Count());

    }

使用Dapper是否可以进行这种批量更新?我宁愿批量更新,也不愿使用for循环将数据推送到数据库中

看起来这在一个简洁的语句中是不可能的。这是完全可以理解的,当考虑到需要在幕后做些什么来实现这一点

最后,我使用3条语句创建了一个临时表,将需要更新的数据填充到临时表中,然后调用一个与我的临时表进行内部连接的更新:

cn.Execute(@"create table #routineUpdatedRecords
                        (
                            AircraftId int, 
                            ItemNumber int,
                            ToleranceMonths int,
                            ToleranceDays int,
                            ToleranceLandings int,
                            ToleranceCycles decimal(12,2),
                            ToleranceRIN decimal(12,2),
                            ToleranceHours decimal(12,2)
                        );");


cn.Execute(@"Insert INTO #routineUpdatedRecords 
    VALUES(@AircraftId, @ItemNumber, @ToleranceMonths, @ToleranceDays, 
@ToleranceLandings, @ToleranceCycles, @ToleranceRIN, @ToleranceHours)", updateInput);

var numResults = cn.Execute(@"UPDATE rm 
                                SET rm.ToleranceMonths=ur.ToleranceMonths, 
                                rm.ToleranceDays=ur.ToleranceDays, 
                                rm.ToleranceHours=ur.ToleranceHours, 
                                rm.ToleranceLandings=ur.ToleranceLandings, 
                                rm.ToleranceCycles=ur.ToleranceCycles, 
                                rm.ToleranceRIN=ur.ToleranceRIN 
                            from [RoutineItems] rm
                            Inner Join #routineUpdatedRecords ur ON rm.AircraftId=ur.AircraftId AND rm.ItemNumber=ur.ItemNumber");

我相信这比在循环中调用update要快,因为我更新了大约600K行

我知道这根线有点旧了。但您可以这样做,而不是使用临时表。提供了更好的语法

string sql = @"UPDATE rm 
    SET rm.ToleranceMonths=@ToleranceMonths, 
    rm.ToleranceDays=@ToleranceDays, 
    rm.ToleranceHours=@ToleranceHours, 
    rm.ToleranceLandings=@ToleranceLandings, 
    rm.ToleranceCycles=@ToleranceCycles, 
    rm.ToleranceRIN=@ToleranceRIN 
    FROM [RoutineItems] rm
    WHERE rm.AircraftId=@AircraftId AND rm.ItemNumber=@ItemNumber
    ";

var numResults = cn.Execute(sql, updateInput);

您可以在Sql Server中创建用户类型,也可以通过使用XML和创建临时表等来模拟大容量插入功能。。做谷歌搜索在线上有很多例子。嗨,肯托,我们到底需要删除routineUpdatedRecords吗?@ThienLong我不认为删除临时表是必要的,因为它是为创建临时表的会话/连接评分的。完成dapper查询后,此会话将关闭,因为sql server将删除临时表。