如何使用交易就是这种情况.. [英] How to use transaction is this case..

查看:69
本文介绍了如何使用交易就是这种情况..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧所以我有一个webform和5个FileUpload控件..用户可以从1到5加载任意数量的文件,但如果有任何文件没有上传,那么我想回滚所有内容......

例如:

如果用户选择了4个文件,如果在第4个时发生了意外情况,那么我想删除或回滚所有之前的3个文件上传..



dboperation dbinsert = new dboperation();

Ok so i have a webform and 5 FileUpload control..a user can upoad any number of files from 1 to 5 but if anyone of the 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..

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



dboperation是c#文件中的一个类,dbinsert是一个方法正在执行插入存储过程。

//......till file5

dboperation is a class in c# file and dbinsert is a method which is executing an insert stored procedure.

推荐答案

检查一下,解释得很清楚。



http://stackoverflow.com/questions/ 2912112 / c-sharp-rollback-sqltransaction-in-catch-block-problem-with-object-accessabi
Check this out, explains it very well.

http://stackoverflow.com/questions/2912112/c-sharp-rollback-sqltransaction-in-catch-block-problem-with-object-accessabi


在Connection open的开头使用SqlTransaction对象
并在每个SqlCommand对象中传递该事务对象。



通过使用Commit()或Rollback()方法,您可以决定是否保存改变与否。





Use SqlTransaction Object in the beginning of Connection open
and pass that transaction object in each SqlCommand object.

And by using Commit() or Rollback() Method you can decide the whether to save the changes or not.


using (var Conn = new SqlConnection(_ConnectionString))
{
    SqlTransaction trans = null;
    try
    {
        Conn.Open();
        trans = Conn.BeginTransaction();

        using (SqlCommand Com = new SqlCommand(ComText, Conn, trans))
        {
            /* DB work */
        }
        trans.Commit();
    }
    catch (Exception Ex)
    {
        if (trans != null) trans.Rollback();
        return -1;
    }
}


这篇关于如何使用交易就是这种情况..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆