• C#数据层添加事务功能


    例子
            public bool Save(ProjectModel project)
            {
                int Result = 0;
                SqlConnection Conn = new SqlConnection(SqlHelper.connectionString);
              Conn.Open();
              SqlTransaction Trans;
              Trans = Conn.BeginTransaction();
              try
                {
                    SqlParameter[] sqlParameter1 = new SqlParameter[]{
                        new SqlParameter{ ParameterName="@EmployeeID",Value= project.EmployeeID}               
                    };
                    
                    int projectID = Convert.ToInt16(SqlHelper.ExecuteScalar(Trans, CommandType.StoredProcedure, "SaveProject", sqlParameter1));
                    if (projectID != 0)
                    {
    
    
                        
                        if (project.LinkManinfoList.Count > 0)
                        {
                            string sqlText = "";
                            for (int i = 0; i < project.LinkManinfoList.Count; i++)
                            {
                                sqlText = "Insert into Customer(ProjectID)values('" + projectID + "')";
                                Result += SqlHelper.ExecuteSql(sqlText);
                            }
                        }
                   
                        if (!string.IsNullOrEmpty(project.ProjectStage))
                        {
                            string sqlText2 = "insert into ProjectStageCondition ()";
                            SqlParameter[] sqlParameter3 = new SqlParameter[]{
                                new SqlParameter{ ParameterName="@ProjectStageDesc",Value= project.ProjectStage}
                              
                            Result += SqlHelper.ExecuteSql(sqlText2, sqlParameter3);
                        }
                        else { Result++; }
    
    
                    }
                  
               Trans.Commit();
                }
                catch (Exception ex)
                {
                   Trans.Rollback();
                }
                finally
                {
                   Conn.Close();
                }
                return Result == project.LinkManinfoList.Count;
            }
  • 相关阅读:
    HTTP方法(转)(学习基础)
    正则表达式 学习手记 111221
    原型模式 学习手记
    分布式事务 MSDTC配置
    Ibatis.Net 学习手记二 缓存
    IIS 7.0 部署MVC
    事务与分布式事务
    Ibatis+MVC 3.0 开发手记
    Ibatis.Net 学习手记一 简单的Demo
    简单工厂 学习手记
  • 原文地址:https://www.cnblogs.com/PeaCode/p/3837782.html
Copyright © 2020-2023  润新知