C# Dapper-将int的列表传递给存储过程
我的存储过程SQL statemet非常简单C# Dapper-将int的列表传递给存储过程,c#,stored-procedures,dapper,C#,Stored Procedures,Dapper,我的存储过程SQL statemet非常简单 Delete From TableName where ID IN (@id) 我想从C代码中传递列表或数组,并想返回已删除的行数 下面是我的代码,不知何故我不相信,认为这不是正确的方法。这样做的有效方式是什么 using (SqlConnection connection = new SqlConnection(_connectionString)) { await connection.OpenAsync(); Dynamic
Delete From TableName where ID IN (@id)
我想从C代码中传递列表或数组,并想返回已删除的行数
下面是我的代码,不知何故我不相信,认为这不是正确的方法。这样做的有效方式是什么
using (SqlConnection connection = new SqlConnection(_connectionString))
{
await connection.OpenAsync();
DynamicParameters parameters = new DynamicParameters();
foreach (var id in ids)
{
parameters.Add("@Id", id, DbType.Int32, ParameterDirection.Input);
await connection.ExecuteAsync(
ProcedureNames.DeleteRules,
parameters,
commandType: CommandType.StoredProcedure);
}
}
创建一个模型以将ID存储在参数中。要获取受影响的行,请获取execute调用的结果
using (SqlConnection connection = new SqlConnection(_connectionString)) {
await connection.OpenAsync();
var affectedRows = await connection.ExecuteAsync(
ProcedureNames.DeleteRules,
new { id = ids.ToArray() }, //<-- Passing collection
commandType: CommandType.StoredProcedure);
}
使用(SqlConnection连接=新的SqlConnection(_connectionString)){
等待连接。OpenAsync();
var affectedRows=await connection.ExecuteAsync(
ProcedureNames.DeleteRules,
new{id=ids.ToArray()},//new{id=id}).ToArray(),//请参阅以下发布:
using (SqlConnection connection = new SqlConnection(_connectionString)) {
await connection.OpenAsync();
var affectedRows = await connection.ExecuteAsync(
ProcedureNames.DeleteRules,
ids.Select(id => new { id = id }).ToArray(), //<-- Passing collection
commandType: CommandType.StoredProcedure);
}