码迷,mamicode.com
首页 > 其他好文 > 详细

C#数据层添加事务功能

时间:2014-07-13 11:17:39      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   strong   数据   

例子
        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;
        }

 

C#数据层添加事务功能,布布扣,bubuko.com

C#数据层添加事务功能

标签:des   style   blog   color   strong   数据   

原文地址:http://www.cnblogs.com/PeaCode/p/3837782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!