我使用的是Microsoft Enterprise Library,有一种情况是插入多行,因此我使用foreach循环,但我觉得有很好的方法可以实现
public int UddateSet
{
int rtn=0;
foreach (Entities.Details list in day)
{
Database db = DatabaseFactory.CreateDatabase();
string sp = "spUpdateSet";
DbCommand dbcommand = db.GetStoredProcCommand(sp);
db.AddInParameter(dbcommand, "@date", DbType.DateTime,date);
db.AddInParameter(dbcommand, "@Id", DbType.Int32, list.Id);
db.AddInParameter(dbcommand, "@Role", DbType.Int32, list.Role);
rtn+= db.ExecuteNonQuery(dbcommand);
}
return rtn;
}
死锁是一个棘手的问题。如果单独执行,您提供的代码不应该导致任何死锁。通常,当其他进程在表上有锁,而您正试图插入来自另一个进程的数据时,就会发生死锁。
此外,您可能只想创建一个Connection和DbCommand,并在循环中重用它。然后,完成后,确保对DbCommand和Database对象调用dispose(或使用using语句)。