我有一个网络方法,将调用SaveEmployee()函数。
WebMethod函数如下:

[WebMethod]
public bool UpdateEmployee(DataSet dSet)
{
  try
  {
     COMAccess.Employee.SaveEmployee(Registry.GetDatabaseConnection(),dSet)
  }
  catch (Exception ex)
  {
    Log(ex.InnerException.ToString()
  }
}
SaveEmployee()函数如下:
public static bool SaveEmployee(string connection, DataSet dSet)
{
  bool rtnVal = false;
  SqlConnection  conn = new SqlConnection(connection);
  conn.Open();
  SqlTransaction tran = conn.BeginTransaction();
  SqlCommand command = conn.CreateCommand();
  command.Connection = conn;
  command.Transaction = tran;
  command.CommandTimeout = 320;
  command.CommandText = "SP_UPDATE_EMPLOYEE";
  command.CommandType = System.Data.CommandType.StoredProcedure;

  try
  {
     foreach(System.Data.DataRow dRow in dSet.Tables["Employee"].Rows)
     {
        command.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = dRow["EmployeeID"];
        command.Parameters.Add("@EmployeeName ", SqlDbType.VarChar).Value = dRow["EmployeeName"];
        command.ExecuteNonQuery();
        command.Parameters.Clear();
     }
     tran.Commit();
     myVal = true;
  }
  catch (Exception ex)
  {
     tran.Rollback();
     myVal = false;
  }
     return myVal;
  }

无论如何,我收到了以下InnerException消息:
System.Threading.ThreadAbortException: Thread was being aborted.
at SNINativeMethodWrapper.SNIPacketGetConnection(IntPtr packet)
at System.Data.SqlClient.TdsParserStateObject.ProcessSniPacket(IntPtr packet, UInt32 error)
at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
at System.Data.SqlClient.TdsParserStateObject.ReadByte()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
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)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at COMAccess.Employee.SaveEmployee(String connection, DataSet dSet)

我不知道发生了什么事。有人可以启发我吗?谢谢

最佳答案

在您的代码中,您将 CommandTimeout 属性设置为320秒:

command.CommandTimeout = 320;

但是,您的连接仍然具有等于30秒的默认超时,并且在该时间过去之后,该命令的内部线程将被取消,因为它是长时间运行的。

您必须为连接设置 ConnectionTimeout 属性,如下所示:
connection.ConnectionTimeout = 320;

09-26 20:36