.NET 事务使用

  /// <summary>
        /// 更新项目信息
        /// </summary>
        /// <param name="project">项目实体</param>
        /// <param name="lm">日志实体</param>
        ///  <param name="lm">系统默认项目状态文本</param>
        /// <returns>业务逻辑更新成功返回True,否则返回False</returns>
        public bool SalesEdit(ProjectModel project, LogModel lm, string txtProjectStageSys)
        {
            int Result = 0;
            SqlConnection Conn = new SqlConnection(SqlHelper.connectionString);
            Conn.Open();
            SqlTransaction Trans;
            Trans = Conn.BeginTransaction();
            try
            {
                //1更新项目信息
                string sqlText1 = "";
                sqlText1 += "Update Project Set ProjectGJUpdateDatetime='" + DateTime.Now + "',ProjectUpdateDate='" + DateTime.Now + "',";
                sqlText1 += "COrderDate=@COrderDate,";
                sqlText1 += "UserName=@UserName,";
                sqlText1 += "ProjectName=@ProjectName,";
                sqlText1 += "ProjectTypeID=@ProjectTypeID,";
                sqlText1 += "Remark=@Remark,";
                sqlText1 += "ProjectBudget=@ProjectBudget,";
                sqlText1 += "MLEstimate=@MLEstimate,";
                sqlText1 += "ProjectMoney=@ProjectMoney,";
                sqlText1 += "ProjectUpdateUser=@ProjectUpdateUser,";
                sqlText1 += "ProjectStateID=@ProjectStateID,";
                sqlText1 += "AgreementDate=@AgreementDate,";
                sqlText1 += "LXDate=@LXDate,";
                sqlText1 += "ProjectResult=@ProjectResult,";
                sqlText1 += "kkd=@kkd,";
                //sqlText1 += "CZC=@CZC,";
                //sqlText1 += "CZCDesc=@CZCDesc,";
                //sqlText1 += "ZCBz=@ZCBz,";
                sqlText1 += "MLEstimateRemark=@MLEstimateRemark,";

                sqlText1 += "RelationZC=@RelationZC,";
                sqlText1 += "RelationZCDesc=@RelationZCDesc,";
                sqlText1 += "RelationZCOver=@RelationZCOver,";
                sqlText1 += "RelationZCType=@RelationZCType,";
                sqlText1 += "KKX=@KKX,";
                sqlText1 += "KKXRemark=@KKXRemark,";
                sqlText1 += "ProjectStage=@ProjectStage where ProjectID=@ProjectID";
                SqlParameter[] sqlParameter1 = new SqlParameter[]{
                    new SqlParameter{ ParameterName="@COrderDate",Value= project.COrderDate},
                    new SqlParameter{ ParameterName="@UserName",Value= project.UserName},
                    new SqlParameter{ ParameterName="@ProjectName",Value= project.ProjectName},
                    new SqlParameter{ ParameterName="@ProjectTypeID",Value= project.ProjectTypeID},
                    new SqlParameter{ ParameterName="@Remark",Value= project.Remark},
                    new SqlParameter{ ParameterName="@ProjectBudget",Value= project.ProjectBudget},
                    new SqlParameter{ ParameterName="@MLEstimate",Value= project.MLEstimate},
                    new SqlParameter{ ParameterName="@ProjectMoney",Value= project.ProjectMoney},
                    new SqlParameter{ ParameterName="@ProjectUpdateUser",Value= project.ProjectUpdateUser},
                    new SqlParameter{ ParameterName="@ProjectStage",Value=project.ProjectStage},
                    new SqlParameter{ ParameterName="@ProjectID",Value=project.ProjectID},
                    new SqlParameter{ ParameterName="@ProjectStateID",Value=project.ProjectState.Trim()},
                    new SqlParameter { ParameterName = "@AgreementDate", Value =project.AgreementDate },
                    new SqlParameter { ParameterName = "@LXDate", Value =project.LXDate },
                    new SqlParameter { ParameterName = "@ProjectResult", Value =project.ProjectResult },
                    new SqlParameter { ParameterName = "@kkd", Value =project.KKD },
                    //new SqlParameter { ParameterName = "@CZC", Value =project.ISZC },
                    //new SqlParameter { ParameterName = "@CZCDesc", Value =project.CZC },
                    //new SqlParameter { ParameterName = "@ZCBz", Value =project.ZCOver },
                    new SqlParameter { ParameterName = "@MLEstimateRemark", Value =project.MLEstimateRemark },

                    new SqlParameter { ParameterName = "@RelationZC", Value =project.RelationZC },
                    new SqlParameter { ParameterName = "@RelationZCDesc", Value =project.RelationZCDesc },
                    new SqlParameter { ParameterName = "@RelationZCOver", Value =project.RelationZCOver },

                    new SqlParameter { ParameterName = "@KKX", Value =project.KKX },
                    new SqlParameter { ParameterName = "@KKXRemark", Value =project.KKXRemark },

                    new SqlParameter { ParameterName = "@RelationZCType", Value =project.RelationZCType }
                 };
                Result += SqlHelper.ExecuteSql(sqlText1, sqlParameter1);



                //3检测 插入项目对应的客户联系人信息删除再添加

                string sqltext5 = "Delete from CustomerLinkman where ProjectID='" + project.ProjectID + "'";
                SqlHelper.ExecuteSql(sqltext5);
                string sqlText = "";
                for (int i = 0; i < project.LinkManinfoList.Count; i++)
                {
                    sqlText = "Insert into CustomerLinkman(ProjectID,CustomerName,Position,Support,Phone,dept)values('" + project.ProjectID + "','" + project.LinkManinfoList[i].LinkManName + "','" + project.LinkManinfoList[i].Position + "','" + project.LinkManinfoList[i].Support + "','" + project.LinkManinfoList[i].Tel + "','" + project.LinkManinfoList[i].Dept + "')";
                    Result += SqlHelper.ExecuteSql(sqlText);
                }

                //系统项目进展
                if (txtProjectStageSys != "")
                {
                    string sqlText3 = "insert into ProjectStageCondition (ProjectID,ProjectStageDesc,CreateUser,type) values (@ProjectID,@ProjectStageDesc,@CreateUser,@type)";
                    SqlParameter[] sqlParameter3 = new SqlParameter[]{
                                    new SqlParameter{ ParameterName="@ProjectStageDesc",Value= txtProjectStageSys},
                                    new SqlParameter{ ParameterName="@ProjectID",Value= project.ProjectID},
                                    new SqlParameter{ ParameterName="@CreateUser",Value= project.ProjectRecordUser},
                                    new SqlParameter{ ParameterName="@type",Value=3}
                        };
                    Result += SqlHelper.ExecuteSql(sqlText3, sqlParameter3);
                }


                //4项目进展表
                if (project.ProjectStage != "" && project.ProjectStage != txtProjectStageSys)
                {

                    string sqlText3 = "insert into ProjectStageCondition (ProjectID,ProjectStageDesc,CreateUser,type) values (@ProjectID,@ProjectStageDesc,@CreateUser,@type)";
                    SqlParameter[] sqlParameter3 = new SqlParameter[]{
                                    new SqlParameter{ ParameterName="@ProjectStageDesc",Value= project.ProjectStage},
                                    new SqlParameter{ ParameterName="@ProjectID",Value= project.ProjectID},
                                    new SqlParameter{ ParameterName="@CreateUser",Value= project.ProjectRecordUser},
                                    new SqlParameter{ ParameterName="@type",Value= 0}
                        };
                    Result += SqlHelper.ExecuteSql(sqlText3, sqlParameter3);

                }

                //更新售前方案的部门
                if (!string.IsNullOrEmpty(project.PSDepartment) && project.PSDepartment != "0")
                {
                    string tempText = "SELECT count(*) FROM [PS_ProjectDepartment]  where ProjectID ='" + project.ProjectID + "' and mark='0'";
                    if (int.Parse(SqlHelper.ExecuteScalar(CommandType.Text, tempText, null).ToString()) == 0)   //不存在记录,插入
                    {

                        string sqlText15 = "insert into PS_ProjectDepartment (ProjectID,PSDepartmentid,mark) values ('" + project.ProjectID + "','" + project.PSDepartment + "','0')";
                        Result += SqlHelper.ExecuteSql(sqlText15);

                    }
                    else
                    {
                        string sqlText11 = "update PS_ProjectDepartment set PSDepartmentid='" + project.PSDepartment + "' where  ProjectID='" + project.ProjectID + "' and mark='0'";
                        Result += SqlHelper.ExecuteSql(sqlText11);
                    }
                }

                if (project.MLEstimateRemark != null && project.MLEstimateRemark.Trim() != "")
                {
                    string sqlText4 = "insert into MLEstimateLog (ProjectID,Remark,CreatTime) values (@ProjectID,@Remark,@CreatTime)";
                    SqlParameter[] sqlParameter3 = new SqlParameter[]{
                                    new SqlParameter{ ParameterName="@Remark",Value= project.MLEstimateRemark},
                                    new SqlParameter{ ParameterName="@ProjectID",Value= project.ProjectID},
                                    new SqlParameter{ ParameterName="@CreatTime",Value= DateTime.Now}
                        };
                    Result += SqlHelper.ExecuteSql(sqlText4, sqlParameter3);
                }

                //5增加系统日志
                Result += LogHelp.SaveLog(lm);
                Trans.Commit();
            }
            catch (Exception ex)
            {
                Trans.Rollback();
            }
            finally
            {
                Conn.Close();
            }
            return Result > 0;
        }
posted @ 2014-07-25 15:12  feimon  阅读(115)  评论(0)    收藏  举报