简体   繁体   English

如果尚未上载所有文件,如何回滚

[英]How to roll back if all files have not been uploaded

Ok so i have a webform and 5 FileUpload control..a user can upload any number of files from 1 to 5 but if anyone of these files does not get uploaded then I want to rollback everything... For ex: if user has selected 4 files and if something unexpected occurs at 4th then I want to remove or rollback all the previous 3 file uploads.. I tried this.. 好吧,我有一个webform和5 FileUpload控件..用户可以上传任意数量的文件从1到5,但如果这些文件中的任何一个没有上传,那么我想回滚所有...例如:如果用户选择4个文件,如果在第4个发生意外,那么我想删除或回滚所有以前的3个文件上传..我试过这个..

 try
{
  using (TransactionScope scope = new TransactionScope())
                {
dboperation dbinsert=new dboperation();
if (file1.ContentLength > 0)
{
      .......
      .......
dbo.insert(bytes, lastid, file2.FileName);
}

if (file2.ContentLength > 0)
{
      .......
      .......
dbo.insert(bytes, lastid, file2.FileName);
}

if (file3.ContentLength > 0)
{
      .......
      .......
dbo.insert(bytes, lastid, file2.FileName);
}//till ...file5

scope.Complete();
}//end of transactionscope
}


catch { }

'dboperation' is a class in c# file and 'dbinsert' is a method which is executing an insert stored procedure. 'dboperation'是c#文件中的类,'dbinsert'是执行插入存储过程的方法。 My guess is that I need to use Transaction Scope but I am not sure if I am correct and even if I am how am I supposed to achieve this? 我的猜测是我需要使用交易范围,但我不确定我是否正确,即使我是如何实现这一目标的呢?

You need to implement transaction . 您需要实现交易 You should start the transaction before inserting first one and catch any errors that occur. 您应该在插入第一个事务之前启动事务并捕获发生的任何错误。 in case of error you have to rollback the transaction. 如果出现错误,您必须回滚事务。 And if all goes well you can commit your transaction. 如果一切顺利,您可以提交您的交易。

You should also move you connection outside the dboperation or make a method in dboperation that takes connection from outside and uses that 您还应该在dboperation之外移动连接,或者在dboperation中创建一个从外部获取连接并使用它的方法

for this you need to use Transaction something like this. 为此你需要使用像这样的Transaction I give you example. 我举个例子。

class WithTransaction
{
    public WithTransaction()
    {
        string FirstQuery = "INSERT INTO Table1 VALUES('Vineeth',24)";
        string SecondQuery = "INSERT INTO Table2 VALUES('HisAddress')";
        int ErrorVar = 0;
        using (SqlConnection con = new SqlConnection("your connection string"))
        {
            try
            {
                SqlCommand ObjCommand = new SqlCommand(FirstQuery, con);
                SqlTransaction trans;
                con.Open();
                trans = con.BeginTransaction();
                ObjCommand.Transaction = trans;
                //Executing first query

                //What ever operation on your database do here

                ObjCommand.ExecuteNonQuery();  //Exected first query
                ObjCommand.CommandText = SecondQuery;
                ObjCommand.ExecuteNonQuery();  //Exected first query
                //Everything gone fine. So commiting
                ObjCommand.Transaction.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error but we are rollbacking");
                ObjCommand.Transaction.Rollback();
            }
            con.Close();
        }
    }
}

Or you can use TransactionScope 或者您可以使用TransactionScope

check this Link 检查此链接

TransactionScope 的TransactionScope

I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM