110,539
社区成员
发帖
与我相关
我的任务
分享
public static int ExecuteNonQueryTran(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if (connection == null) throw new ArgumentNullException("connection");
// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
//Undo
cmd.CommandTimeout = 0;
bool mustCloseConnection = false;
connection.Open();
PrepareCommand(cmd, connection, connection.BeginTransaction(), commandType, commandText, commandParameters, out mustCloseConnection);
try
{
// Finally, execute the command
int retval = cmd.ExecuteNonQuery();
cmd.Transaction.Commit();
// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
if (mustCloseConnection)
connection.Close();
return retval;
}
catch(Exception e)
{
cmd.Transaction.Rollback();
return 0;
}
finally
{
connection.Close();
}
}
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if (connection == null) throw new ArgumentNullException("connection");
// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
//Undo
cmd.CommandTimeout = 0;
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
// Finally, execute the command
int retval = cmd.ExecuteNonQuery();
// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
if (mustCloseConnection)
connection.Close();
return retval;
}
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if (connection == null) throw new ArgumentNullException("connection");
// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
//Undo
cmd.CommandTimeout = 0;
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
// Finally, execute the command
int retval = cmd.ExecuteNonQuery();
// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
if (mustCloseConnection)
connection.Close();
return retval;
}
出现了下面的异常
System.Data.SqlClient.SqlException (0x80131904): 事务(进程 ID 221)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品。请重新运行该事务。
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at SqlHelper.ExecuteNonQuery(SqlConnection connection, CommandType commandType, String commandText, SqlParameter[] commandParameters) in E:\SVN\system\sourcecode\MPS\MPS_Trigger\LotTrigger\LotTrigger\LotTrigger\SQLHelper.cs:line 364
at LotTrigger.MQLots_DAL.Update_MQLotTransactions_MoveOut_DAL(MQLots_BLL objBLL) in E:\SVN\system\sourcecode\MPS\MPS_Trigger\LotTrigger\LotTrigger\LotTrigger\MQLots_DAL.cs:line 1173
at LotTrigger.MQLots_BLL.Update_MQLotTransactions_MoveOut_BLL() in E:\SVN\system\sourcecode\MPS\MPS_Trigger\LotTrigger\LotTrigger\LotTrigger\MQLots_BLL.cs:line 235
at LotTrigger.Program.CheckMQData(MQLots_BLL objBLL, ILog log, Int32 mqDataId) in E:\SVN\system\sourcecode\MPS\MPS_Trigger\LotTrigger\LotTrigger\LotTrigger\Program.cs:line 186
ClientConnectionId:807f373d-4661-4fa5-9516-728cb2327712
Error Number:1205,State:52,Class:13
System.Data.SqlClient.SqlException (0x80131904): 事务(进程 ID 283)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品。请重新运行该事务。
at SqlHelper.ExecuteNonQueryTran(SqlConnection connection, CommandType commandType, String commandText, SqlParameter[] commandParameters) in E:\SVN\system\sourcecode\MPS\MPS_Trigger\LotTrigger\LotTrigger\LotTrigger\SQLHelper.cs:line 425
at LotTrigger.MQLots_DAL.Update_MQLotTransactions_MoveOut_DAL(MQLots_BLL objBLL) in E:\SVN\system\sourcecode\MPS\MPS_Trigger\LotTrigger\LotTrigger\LotTrigger\MQLots_DAL.cs:line 1173
at LotTrigger.MQLots_BLL.Update_MQLotTransactions_MoveOut_BLL() in E:\SVN\system\sourcecode\MPS\MPS_Trigger\LotTrigger\LotTrigger\LotTrigger\MQLots_BLL.cs:line 235
at LotTrigger.Program.CheckMQData(MQLots_BLL objBLL, ILog log, Int32 mqDataId) in E:\SVN\system\sourcecode\MPS\MPS_Trigger\LotTrigger\LotTrigger\LotTrigger\Program.cs:line 186
ClientConnectionId:0b3e71e3-6770-4c9d-ab70-903863118b2c
Error Number:1205,State:52,Class:13
加了事务之后的异常
System.NullReferenceException: Object reference not set to an instance of an object.
at SqlHelper.ExecuteNonQueryTran(SqlConnection connection, CommandType commandType, String commandText, SqlParameter[] commandParameters) in 。。。\SQLHelper.cs:line 415