c#中SQL脚本的执行流程

时间:2013-09-25 07:02:35

标签: c# sql-server transactions rollback

这不是rollback sql transactions if any failed in c#的重复问题。但这个问题是基于前一个问题的内容。感谢所有对我的上一个问题发表意见的人。我尝试了所有这些解决方案,最终发现在我的应用程序(DDL)中使用的脚本不允许在多语句事务中使用。

我的整个代码是这样的:

script1 = "CREATE USER " + username + " FROM LOGIN " + username;

script2 = "CREATE ROLE " + rolename;

script3 = @"CREATE ROLE [db_execute] AUTHORIZATION [dbo]
                        GRANT EXECUTE TO [db_execute]";

script4 = @"DECLARE @rolename varchar(max)
                    SET @rolename ='{0}'
                    EXEC sp_addrolemember N'db_execute',@rolename
                    EXEC sp_addrolemember N'db_datareader', @rolename
                    EXEC sp_addrolemember N'db_datawriter', @rolename";
script_result = string.Format(script4, rolename);

script6 = "EXEC sp_addrolemember '{0}', '{1}'";
            outp = string.Format(script6, rolename, username);

script7 = @"select m.name as Member, r.name as Role
                    from sys.database_role_members
                    inner join sys.database_principals m on sys.database_role_members.member_principal_id = m.principal_id
                    inner join sys.database_principals r on sys.database_role_members.role_principal_id = r.principal_id";

以与如下所示相同的方式执行所有这些:

SqlCommand SqlCmd = new SqlCommand();

//Create User Script Execution
SqlCmd.CommandText = script1;
SqlCmd.Connection = oConnection;        
lvinfo.Items.Add("Executing Create User script in " + dbname + " database");
var answer = SqlCmd.ExecuteNonQuery();

//Checking whether execution completed successfully
if (!answer.Equals(0))
{
lvinfo.Items.Add("Create User script executed successfully in " + dbname + " database");                                                                          
}
else
{
lvinfo.Items.Add("Create User script execution failed in " + db_select.SelectedItem.ToString() + " database");
}

即使我只编写了这段代码,现在我对这些脚本的执行有些怀疑。只需单击一个按钮,就可以一次执行上面显示的所有脚本。我想知道的是,这些脚本是否会在一次完全执行?意味着,一次执行这些脚本时是否有任何机会导致执行失败?以数据库断开或任何其他错误的形式出现。 / p>

让我向您展示一个我要问的例子:如果前三个脚本以某种方式执行,其余所有脚本都失败了,那么是否有机会自动回滚?

或者我的代码在整个代码执行之前检查是否有任何错误或失败的可能性,以便它不需要回滚?

任何人都可以用他们宝贵的建议和想法来支持我。

任何帮助都会非常感激..

1 个答案:

答案 0 :(得分:0)

我已经为它的外观给出了一个骨架。 你可以告诉我你想要回滚什么和什么不是场景。

var scriptToExecute = new Dictionary<int, string>();
            var scriptToRollback = new Dictionary<int, string>();

        try
        {
            using (SqlCommand SqlCmd = new SqlCommand())
            {
                //Create User Script Execution
                SqlCmd.CommandText = script1;
                SqlCmd.Connection = oConnection;
                lvinfo.Items.Add("Executing Create User script in " + dbname + " database");
                var answer = SqlCmd.ExecuteNonQuery();

                //Checking whether execution completed successfully
                if (!answer.Equals(0))
                {
                    lvinfo.Items.Add("Create User script executed successfully in " + dbname + " database");
                }
                else
                {
                    lvinfo.Items.Add("Create User script execution failed in " + db_select.SelectedItem.ToString() + " database");
                }
            }
        }
        catch (SqlException)
        {
            string rollbackScript = string.Empty;
            for (int i = scriptExecuting; i > 0; i--)
            {
                scriptToRollback.TryGetValue(i, rollbackScript);
                if (!string.IsEmpty(rollbackScript))
                {
                    //Execute the scripts here.
                }
            }
        }



        finally
        {
            //If connection is open then close the active connection
        }