我有一个表employee
,我必须将他们的位置更新到新位置,所以我需要批量更新。请使用Dapper O.R.M。
我的主要密钥是Employee-id
。
您可以在下面看到一次更新单个记录的示例代码。
// Employees is list of class class Employee
SqlConnection connection = new SqlConnection(connectionstring);
connection.open();
foreach (Employee employee in Employees)
{
string query = @"UPDATE [dbo].[Employee]
SET Name = @Name, Age = @Age, Sex = @Sex,
Location = @Location
WHERE Id = @Id";
connection.QueryAsync<bool>(query, new { @Id = employee.Id, @Name = employee.Name,
@Sex = employee.sex, @Age = employee.age,
@Location = employee.location})).SingleOrDefault();
}
答案 0 :(得分:12)
Dapper支持从List插入/更新。
internal class Employee
{
public int Id { get; set; }
public int Age { get; set; }
public string Name { get; set; }
}
[TestFixture]
public class DapperTests
{
private IDbConnection _connection;
[SetUp]
public void SetUp()
{
_connection = new SqlConnection(@"Data Source=.\sqlexpress; Integrated Security=true; Initial Catalog=mydb");
_connection.Open();
_connection.Execute("create table employees(Id int, Name varchar(100), Age int)");
_connection.Execute("insert into employees(Id, Name) values(1, 'xxx'), (2, 'yyy')");
}
[TearDown]
public void TearDown()
{
_connection.Execute("drop table employees");
_connection.Close();
}
[Test]
public void BulkUpdateFromAListTest()
{
_connection.Execute(@"update employees set Name = @Name where Id = @Id",
new List<Employee>
{
new Employee{Age = 1, Name = "foo", Id = 1},
new Employee{Age = 2, Name = "bar", Id = 2}
});
var result = _connection.Query<Employee>("select * from employees").ToList();
Assert.That(result.Count, Is.EqualTo(2));
Assert.That(result.FirstOrDefault(x => x.Id == 1).Name == "foo");
Assert.That(result.FirstOrDefault(x => x.Id == 2).Name == "bar");
}
}
Dapper的问题在于它分别执行每个命令,所以如果你有多个,那么就会出现性能问题。
另一种方法是创建临时表,然后使用连接进行更新。为此,您可以执行以下操作:
[TestFixture]
public class BatchRunnerTests
{
private readonly IDbConnection _dbConnection;
public BatchRunnerTests()
{
_dbConnection = new SqlConnection(@"Data Source=.\sqlexpress; Integrated Security=true; Initial Catalog=Bktb4_CaseMgr_Db"); ;
_dbConnection.Open();
}
[Test]
public void TestBatchRunner()
{
var records = new List<Employee>
{
new Employee {Age = 1, Name = "foo", Id = 1},
new Employee {Age = 2, Name = "bar", Id = 2}
};
var tablwToUpdateFrom = BuildTable(records);
_dbConnection.Execute("update a set Name = b.Name from employees a join " + tablwToUpdateFrom + " b on a.Id = b.Id");
}
public string BuildTable(List<Employee> data)
{
var tableName = "#" + Guid.NewGuid();
_dbConnection.Execute("create table [" + tableName + "] ( Id int null, Name varchar(50) null)");
var batchRunner = new SqlBatchRunner(_dbConnection);
data.ToList().ForEach(x =>
batchRunner.RecordingConnection.Execute(@"insert into [" + tableName + "] values(@Id, @Name)", x));
batchRunner.Run();
return tableName;
}
}
我在这里使用的library是由朋友写的。它只会生成一组insert语句并立即执行它们。