[英]C# Sql Updating row by row from a List of Objects
public int UpdateAmount(List<MyTable> myBizObjList)
{
SqlTransaction sqltxn;
DbClass db = new DbClass();
SqlConnection cs;
cs = db.GetConnection();
string commandText = @"Update MyTable Set amt = @amt where empno = @empno and mydate = @mydate";
int x = myBizObjList.Count;
int y = 0,rowsaffected;
cs.Open();
using (cs)
{
sqltxn = cs.BeginTransaction();
foreach (MyTable myBizObj in myBizObjList)
{
SqlCommand command = new SqlCommand(commandText, cs, sqltxn);
command.Parameters.Add("@empno", SqlDbType.Int);
command.Parameters["@empno"].Value = myBizObj.Empno;
command.Parameters.Add("@mydate", SqlDbType.Date);
command.Parameters["@mydate"].Value = myBizObj.Mydate;
command.Parameters.Add("@amt", SqlDbType.Decimal);
command.Parameters["@amt"].Value = myBizObj.Amt;
try
{
rowsAffected = command.ExecuteNonQuery();
if (rowsAffected == 1)
y++;
}
catch (Exception ex)
{
throw (ex);
}
}
if (y == x)
{
sqltxn.Commit();
}
else
{
sqltxn.Rollback();
y = 0;
}
cs.Close();
return y;
}
}
Question: I am querying a table and getting say 50K records which I am converting to a List of objects.问题:我正在查询一个表并得到说我正在转换为对象列表的 50K 记录。 I am processing the List in my BLL and sending to my DAL.
我正在处理我的 BLL 中的列表并发送到我的 DAL。 The above is a method in my DAL.
以上是我的 DAL 中的一个方法。 Is there a better way?
有没有更好的办法? I am also checking if all rows are updated & then Commit or Rollback.
我还在检查是否所有行都已更新,然后是 Commit 或 Rollback。
You can convert this to a table-valued parameter.您可以将其转换为表值参数。
First we need a table type:首先我们需要一个表类型:
CREATE TYPE dbo.MyTVP (
empno int not null,
mydate date not null,
amt decimal not null
primary key (empno, mydate)
);
Then we pass it through.然后我们通过它。 You don't necessarily need a stored procedure, you can do this as an ad-hoc batch:
您不一定需要存储过程,您可以将其作为临时批处理执行:
public int UpdateAmount(List<MyTable> myBizObjList)
{
var table = new DataTable();
table.Columns.Add("empno", typeof(int));
table.Columns.Add("mydate", typeof(datetime));
table.Columns.Add("amt", typeof(decimal));
foreach (MyTable myBizObj in myBizObjList)
table.Rows.Add(myBizObj.Empno, myBizObj.Mydate, myBizObj.Amt);
const string commandText = @"
Update tbl
Set amt = t.amt
FROM MyTable AS tbl
JOIN @tmp AS t ON t.empno = tbl.empno AND t.mydate = tbl.mydate;
";
using (var cs = db.GetConnection())
{
SqlCommand command = new SqlCommand(commandText, cs, sqltxn);
command.Parameters.Add(
new SqlParameter("@tmp", SqlDbType.Structured)
{
Direction = ParameterDirection.Input,
TypeName = "dbo.MyTVP",
Value = table
});
cs.Open();
return command.ExecuteNonQuery();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.