0

在我的窗口应用程序中,我需要执行一系列 sql 命令。如果所有 sqlcommands 都成功执行,则没有问题,但是当第 2 或第 3 个命令出错时,这对我来说是个大问题,因为第 1 条命令已执行,但第 2 和第 3 条命令没有执行。在这里,我想要执行所有命令或不执行任何命令。我的代码如下:

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(); 
4

3 回答 3

1

使用TransactionScope...请参阅 [MSDN 页面] 底部的示例。(http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx

于 2013-10-03T14:36:23.917 回答
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

重要提示:考虑使用参数化查询来避免SQL 注入

于 2013-10-03T14:40:38.230 回答
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();
}
于 2013-10-03T14:52:50.087 回答