zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    Aras前端的一些知识
    Eclipse 实用快捷键大全
    Eclipse插件使用links目录的用法
    extjs portal 保存 事件
    NDRS SQL
    [VB]修改注册表让程序开机自动运行
    [C++]数组参数
    [C++]指针类型出参
    [C++]函数返回值
    [VBA]Excel输出utf8编码格式文件 使用WideCharToMultiByte
  • 原文地址:https://www.cnblogs.com/PeaCode/p/3837782.html
Copyright © 2011-2022 走看看