.net C# 事务处理

来源:互联网 发布:c语言闰年个数计算 编辑:程序博客网 时间:2024/05/21 10:32

在数据库中使用存储过程,通常遇到多个操作的时候,我们都会用事物来进行错误回滚。但是,有时候,需要保存大数据量的时候,将数据传到存储过程里面,是很麻烦的时候,varchar 8000,nvarchar 4000,当大于这个最大值的时候,怎么处理呢?多声明几个参数。?这个,你能预知多少个呢?

 所以,在这个情况下,我觉得在代码里面使用事物比较好。

下面我们看一下,怎么样在代码中实现事物操作数据:

1.首先要启动 

MSDTC 服务(Distributed Transaction Coordinator) 协调跨多个数据库、消息队列、文件系统等资源管理器的事务。如果停止此服务,这些事务将会失败。如果禁用此服务,显式依赖此服务的其他服务将无法启动。

确保MSDTC服务启动运行。

让后在项目中引用

System.Transactions  

代码中:

using System.Transactions;  


下面看一下具体代码

 using (TransactionScope scope = new TransactionScope())                {                    DbCommand cmd = new DbCommand(....)                   ...........//这里是您的代码。                    ret = ExecuteNonQuery(dbUnic, cmd);                    if (0 < ret)                    {                        scope.Complete();                    }                    else                    {                        scope.Dispose();                    }                }


以上是单个操作的伪代码,下面看一下多个操作:

                using (TransactionScope scope = new TransactionScope())                {                    DbCommand cmd =                        dbUnic.GetSqlStringCommand(........));                    dbUnic.AddInParameter(cmd, NAME_PARAM_CONSULTATION_SYSID, DbType.String, sysID);                    dbUnic.AddInParameter(cmd, NAME_PARAM_CONSULTATION_OBJID, DbType.String, objID);                    ............                    bool hasError = true;                    if (0 < ExecuteNonQuery(dbUnic, cmd))                    {                        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(DBConnectionString))                        {                            bulkCopy.DestinationTableName =                                DataAccessConsultationSlice.NAME_TBL_CONSULTATION_SLICE;                            try                            {                                // Write from the source to the destination.                                bulkCopy.WriteToServer(这个是datatable数据);                                hasError = false;                            }                            catch (Exception ex)                            {                                hasError = true;                            }                        }                        if (!hasError)                        {                            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(DBConnectionString))                            {                                bulkCopy.DestinationTableName =                                    DataAccessConsultationExpert.NAME_TBL_CONSULTATION_EXPERT;                                try                                {                                    // Write from the source to the destination.                                    bulkCopy.WriteToServer(这个是datatable数据);                                    hasError = false;                                }                                catch (Exception ex)                                {                                    hasError = true;                                }                            }                            if (!hasError)                            {                                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(DBConnectionString))                                {                                    bulkCopy.DestinationTableName =                                        DataAccessConsultationAnnex.NAME_TBL_CONSULTATION_ANNEX;                                    try                                    {                                        // Write from the source to the destination.                                        bulkCopy.WriteToServer(这个是datatable数据);                                        hasError = false;                                    }                                    catch (Exception ex)                                    {                                        hasError = true;                                    }                                }                            }                        }                    }                    ret = hasError ? 0 : 1;                    if (0 < ret)                    {                        scope.Complete();                    }                    else                    {                        scope.Dispose();                    }                }
这样,就不用担心数据太大的问题了,只要把数据拼装好了,就可以在代码中直接进行数据操作的事物了。





原创粉丝点击