2013-10-03 57 views
0

在我的窗口應用程序中,我需要執行一系列sqlcommands。如果所有sqlcommands成功執行都沒有問題,但是當第二個或第三個命令出錯時,它會給我造成很大問題,因爲執行1st命令但不執行第二個和第三個命令。在這裏我想要所有的命令執行或沒有。我的代碼如下:如何在C中一次執行一系列sqlcommand#

SqlCommand cmd = new SqlCommand("CREATE TABLE [dbo].[" + textBox8.Text + "_stock](" 
         + "[date] [date] NOT NULL PRIMARY KEY CLUSTERED," 
         + "[openingstock] [int] NOT NULL," 
         + "[receipt] [int] NOT NULL," 
         + "[totalstock] [int] NOT NULL," 
         + "[sell] [int] NOT NULL," 
         + "[closingstock] [int] NOT NULL," 
         + ") ON [PRIMARY]", connectionsql); 
        cmd.ExecuteNonQuery(); 
        cmd.Dispose();     

        SqlCommand cmd1 = new SqlCommand("insert into " + textBox8.Text + "_stock values(@date,0,0,0,0,0)", connectionsql); 
        cmd1.Parameters.AddWithValue("date", dateTimePicker3.Value); 
        cmd1.ExecuteNonQuery(); 
        cmd1.Dispose(); 
        cmd1.Parameters.Clear(); 

        SqlCommand cmd2 = new SqlCommand("insert into rate values ('" + textBox12.Text + "','" + textBox8.Text + "_stock','" + double.Parse(textBox7.Text) + "','" + comboBox4.SelectedItem + "')", connectionsql); 
        int z = cmd2.ExecuteNonQuery(); 
        cmd2.Dispose(); 
+0

[SQL注入警報(http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - 你應該不是** **將您的SQL語句連接在一起 - 使用**參數化查詢**來代替以避免SQL注入 –

回答

1

您可以使用transactions做到這一點。我的意思是這樣的:

transaction.BeginTransaction(); 
try{ 
    // your commands here 
    transaction.CommitTransaction(); // commit after all commands 
} catch (Exception){ 
    // if exception, rollback 
    transaction.RollbackTransaction(); 
} 

欲瞭解更多信息,請參見:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.commit(v=vs.110).aspx

重要:考慮使用parameterized queries避免SQL Injection

+0

我無法使用事務處理,它不顯示在intellisense中。我應該使用哪個程序集? –

+0

在鏈接中有一個完整的例子,請看看它。我認爲你可以用這個例子來做。我已更新了EN的鏈接。 –

1

交易是要走的路。你的情況:

using (SqlConnection connectionsql= new SqlConnection(connectionString)) 
{ 
    connectionsql.Open(); 

    SqlTransaction transaction; 

    // Start a local transaction. 
    transaction = connectionsql.BeginTransaction("SampleTransaction"); 

    SqlCommand cmd = new SqlCommand("CREATE TABLE [dbo].[" + textBox8.Text + "_stock](" 
     + "[date] [date] NOT NULL PRIMARY KEY CLUSTERED," 
     + "[openingstock] [int] NOT NULL," 
     + "[receipt] [int] NOT NULL," 
     + "[totalstock] [int] NOT NULL," 
     + "[sell] [int] NOT NULL," 
     + "[closingstock] [int] NOT NULL," 
     + ") ON [PRIMARY]", connectionsql); 

    if (cmd.ExecuteNonQuery() <1) // no table created 
    { 
     transaction.Rollback(); 
    } 
    else // no error 
    { 
     SqlCommand cmd1 = new SqlCommand("insert into " + textBox8.Text + "_stock values(@date,0,0,0,0,0)", connectionsql); 
     cmd1.Parameters.AddWithValue("date", dateTimePicker3.Value); 

     if (cmd1.ExecuteNonQuery() < 1) // no row inserted 
     { 
      transaction.Rollback(); 
     } 
     else // no error 
     { 
      cmd1.Dispose(); 

      SqlCommand cmd2 = new SqlCommand("insert into rate values ('" + textBox12.Text + "','" + textBox8.Text + "_stock','" + double.Parse(textBox7.Text) + "','" + comboBox4.SelectedItem + "')", connectionsql); 
      int z = cmd2.ExecuteNonQuery(); 

      if (z < 1) // no row inserted 
      { 
       transaction.Rollback(); 
      } 
      else // no error 
      { 
       transaction.Commit(); // everything was OK, you can commit the results 
      }   
      cmd2.Dispose(); 
     } 
     cmd1.Dispose(); 
    } 
    cmd.Dispose(); 
} 
+0

我已經使用你的代碼,但仍然存在問題。在第一個命令後,我得到一個需要ExecuteNonQuery()的錯誤。我認爲,因爲最後這樣的表的事務提交沒有被創建,所以II命令不能填充那些條目。對不起,如果我錯了,我不知道交易。 –

+0

對不起,我不明白。如果該表不會創建,該事務將被回滾。每一步都必須完成,代碼才能繼續。這就像你想要的 - 全部或全部。如果您發現任何錯誤,請準確填寫哪一行,以及錯誤所說的內容。 –

相關問題