2012-08-05 75 views
0

存儲過程無法執行。任何人都可以請告訴我並指出我愚蠢的錯誤?存儲過程無法到達

,我得到的錯誤信息是

操作無效。關閉連接

代碼:

public void Update(RepliesBAL RPBAL) 
{ 
    using (SqlConnection connection = new SqlConnection(@"Data Source=19NNZP;Initial Catalog=ivr;Persist Security Info=True;User ID=sa;Password=sa")) 
    { 
     SqlCommand command = new SqlCommand(); 
     command.CommandType = CommandType.StoredProcedure; 
     command.CommandText = "dbo.fax_UpdateFaxReply"; 
     command.Parameters.Add("@uid", SqlDbType.VarChar, 50).Value = RPBAL.UlyssesID ; 

     SqlTransaction transaction; 

     transaction = connection.BeginTransaction("SampleTransaction"); 

     command.Connection = connection; 
     command.Transaction = transaction; 

     try 
     { 
      connection.Open(); 
      command.ExecuteNonQuery(); 
      Console.WriteLine("OK"); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); 
      Console.WriteLine(" Message: {0}", ex.Message); 
      try 
      { 
       transaction.Rollback(); 
      } 
      catch (Exception ex2) 
      { 
       Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); 
       Console.WriteLine(" Message: {0}", ex2.Message); 
       throw new Exception(ex.Message); 
      } 
     } 
    } 
} 
+1

您是否嘗試使用相同的登錄名和參數直接運行SP? '@ ulyssesId'參數是什麼類型?是否有其他參數 - 特別是必需的參數? – Oded 2012-08-05 10:32:28

+0

是的,通過使用相同登錄憑證直接運行sp沒有問題。只有一個參數,它是nvarchar(50)。這是錯誤消息,我得到了「操作無效,連接已關閉」。 – 2012-08-05 10:34:50

回答

1

爲了調用.BeginTransaction(),你的連接需要已經打開 - 使你的代碼更改爲:

using (SqlConnection connection = new SqlConnection(@"Data Source=19NNZP;Initial Catalog=ivr;Persist Security Info=True;User ID=sa;Password=sa")) 
{ 
     // set up the SqlCommand 
     SqlCommand command = new SqlCommand(); 
     command.Connection = connection; 
     command.CommandType = CommandType.StoredProcedure; 
     command.CommandText = "dbo.fax_UpdateFaxReply"; 

     // SqlDbType should be *NVarChar* to exactly match the stored procedure parameter's type! 
     // Otherwise you'll have an implicit conversion happening.... 
     command.Parameters.Add("@uid", SqlDbType.NVarChar, 50).Value = RPBAL.UlyssesID ; 

     SqlTransaction transaction; 

     try 
     { 
      // open connection, start transaction 
      connection.Open(); 

      transaction = connection.BeginTransaction("SampleTransaction"); 

      // assign transaction to SqlCommand and execute it 
      command.Transaction = transaction; 
      command.ExecuteNonQuery(); 

      // if successful - commit the transaction! 
      transaction.Commit(); 
      connection.Close(); 

      Console.WriteLine("OK"); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); 
      Console.WriteLine(" Message: {0}", ex.Message); 
      try 
      { 
       transaction.Rollback(); 
      } 
      catch (Exception ex2) 
      { 
       Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); 
       Console.WriteLine(" Message: {0}", ex2.Message); 
       throw new Exception(ex.Message); 
      } 
     } 
    } 

這種變化之後,希望這段代碼能夠正常工作。