[英]ADO.Net Transaction Not Working
I have the following code (I'm new to .NET transactions) and it does seem to be executing at all. 我有以下代码(我是.NET事务的新手),它似乎根本没有执行。 It's called through a webservice and is difficult for me to debug:
它是通过Web服务调用的,我很难调试:
public void UpdateJailFiles(List<RMS.NameFile> names, RMS.Incident incident, int currentDate, int currentTime, int incidentKey)
{
String library = RMSLIBRARY;
IDbTransaction transaction;
using (IDbConnection conn = Connection)
{
conn.Open();
transaction = conn.BeginTransaction();
try
{
names.ForEach(nameFile =>
{
IDbCommand command = conn.CreateCommand();
command.CommandText = "call " + library + ".SP_NAMEFILE_UPDATE(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 0;
command.Transaction = transaction;
IDbDataParameter param = command.CreateParameter();
param.ParameterName = "@INCIDENTKEY";
param.DbType = DbType.Int32;
param.Value = nameFile.IncidentKey;
command.Parameters.Add(param);
///Many more params...
command.ExecuteNonQuery();
});
IDbCommand command2 = conn.CreateCommand();
command2.CommandText = "call " + library + ".SP_INCIDENTFILE_UPDATE(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
command2.CommandType = CommandType.StoredProcedure;
command2.Transaction = transaction;
IDbDataParameter param1b = command2.CreateParameter();
param1b.ParameterName = "@CASENUMBER";
param1b.DbType = DbType.String;
param1b.Value = incident.CaseNumber;
command2.Parameters.Add(param1b);
IDbDataParameter param2b = command2.CreateParameter();
param2b.ParameterName = "@INCIDENTFROMDATE";
param2b.DbType = DbType.Int32;
param2b.Value = incident.IncidentFromDate;
command2.Parameters.Add(param2b);
///Many more params...
command2.ExecuteNonQuery();
transaction.Commit();
} // end try
catch (Exception)
{
transaction.Rollback();
}
} // end using
}
See anything wrong with this code? 看到这段代码有什么问题吗?
Your use of a property ( Connection
) to load up the using
statement is suspect to me. 我怀疑您使用属性(
Connection
)来加载using
语句。 Typically using
is used on a new, local object instance like so: 通常在新的本地对象实例上
using
,如下所示:
using (IDbConnection conn = new MyConnectionClass())
{
}
When you exit the using
statement, your IDbConnection
gets Dispose()
called on it, but I don't see anywhere that the property it is updated to reflect that status. 当您退出
using
语句时,您的IDbConnection
会调用它的Dispose()
,但是在任何地方都看不到它被更新以反映该状态的属性。 Is that what you want? 那是你要的吗?
You may also need to Dispose()
your IDbCommand
, it's not clear here - but for SqlCommand
, that's true. 您可能还需要
Dispose()
您的IDbCommand
,在这里还不清楚-但是对于SqlCommand
,这是事实。
It could also be helpful to wrap the whole code that accesses the DB in a try..except
so you can diagnose and/or propagate ANY DB exception. 将尝试访问数据库的全部代码包装在
try..except
可能也很有帮助,这样您就可以诊断和/或传播任何数据库异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.