コード例 #1
0
        /// <summary>
        /// 更新一条数据,使用事务
        /// </summary>
        public bool Update(Info_Logs_Model model, Sys_ProcessLog_Model sysProcessLogModel)
        {
            bool result = false;

            // 使用数据库链接
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                //使用事务 开始事务
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update Info_Logs set ");
                        strSql.Append("LogsTitle=@LogsTitle,");
                        strSql.Append("LogsContent=@LogsContent,");
                        strSql.Append("CoverPictureUrl=@CoverPictureUrl,");
                        strSql.Append("CreateUser=@CreateUser,");
                        strSql.Append("CreateTime=@CreateTime,");
                        strSql.Append("UpdateUser=@UpdateUser,");
                        strSql.Append("UpdateTime=@UpdateTime,");
                        strSql.Append("isDelete=@isDelete,");
                        strSql.Append("Remark=@Remark");
                        strSql.Append(" where LogsID=@LogsID ");
                        strSql.Append(";select @LogsID;");

                        SqlParameter[] parameters =
                        {
                            new SqlParameter("@LogsTitle",       SqlDbType.NVarChar,          50),
                            new SqlParameter("@LogsContent",     SqlDbType.NVarChar,          -1),
                            new SqlParameter("@CoverPictureUrl", SqlDbType.NVarChar,         255),
                            new SqlParameter("@CreateUser",      SqlDbType.UniqueIdentifier,  16),
                            new SqlParameter("@CreateTime",      SqlDbType.DateTime),
                            new SqlParameter("@UpdateUser",      SqlDbType.UniqueIdentifier,  16),
                            new SqlParameter("@UpdateTime",      SqlDbType.DateTime),
                            new SqlParameter("@isDelete",        SqlDbType.Bit,                1),
                            new SqlParameter("@Remark",          SqlDbType.NVarChar,          50),
                            new SqlParameter("@LogsID",          SqlDbType.UniqueIdentifier, 16)
                        };
                        parameters[0].Value = model.LogsTitle;
                        parameters[1].Value = model.LogsContent;
                        parameters[2].Value = model.CoverPictureUrl;
                        parameters[3].Value = model.CreateUser;
                        parameters[4].Value = model.CreateTime;
                        parameters[5].Value = model.UpdateUser;
                        parameters[6].Value = model.UpdateTime;
                        parameters[7].Value = model.isDelete;
                        parameters[8].Value = model.Remark;
                        parameters[9].Value = model.LogsID;

                        object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters);
                        if (obj != null)
                        {
                            model.LogsID = new Guid(Convert.ToString(obj));
                            // 判断想不想要加入日志,如果不想加入日志,可以传null
                            bool processLogResult = true;
                            if (sysProcessLogModel != null)
                            {
                                //添加时要告诉另一个语句用的是,哪一个连接,哪一个事务
                                processLogResult = new Sys_ProcessLog_DAL().Add(conn, trans, sysProcessLogModel);
                            }

                            if (processLogResult)
                            {
                                result = true;
                                trans.Commit();
                            }
                            else
                            {
                                result = false;
                                trans.Rollback();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        result = false;
                        trans.Rollback();
                    }
                }
            }

            return(result);
        }
コード例 #2
0
        /*
         * /// <summary>
         * /// 分页获取数据列表
         * /// </summary>
         * public DataSet GetList(int PageSize,int PageIndex,string strWhere)
         * {
         *  SqlParameter[] parameters = {
         *          new SqlParameter("@tblName", SqlDbType.VarChar, 255),
         *          new SqlParameter("@fldName", SqlDbType.VarChar, 255),
         *          new SqlParameter("@PageSize", SqlDbType.Int),
         *          new SqlParameter("@PageIndex", SqlDbType.Int),
         *          new SqlParameter("@IsReCount", SqlDbType.Bit),
         *          new SqlParameter("@OrderType", SqlDbType.Bit),
         *          new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
         *          };
         *  parameters[0].Value = "Info_User";
         *  parameters[1].Value = "UserID";
         *  parameters[2].Value = PageSize;
         *  parameters[3].Value = PageIndex;
         *  parameters[4].Value = 0;
         *  parameters[5].Value = 0;
         *  parameters[6].Value = strWhere;
         *  return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
         * }*/

        #endregion BasicMethod

        #region ExtensionMethod

        /// <summary>
        /// 添加一个用户并且加入日志
        /// </summary>
        public bool Add(Info_User_Model model, Sys_ProcessLog_Model sysProcessLogModel)
        {
            bool result = false;

            // 使用数据库链接
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                //使用事务 开始事务
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into Info_User(");
                        strSql.Append(
                            "UserID,UserName,UserAvatar,UserSex,UserPhone,AccountNum,Pwd,CreateUser,CreateTime,UpdateUser,UpdateTime,IsDelete,Remark)");
                        strSql.Append(" values (");
                        strSql.Append(
                            "@UserID,@UserName,@UserAvatar,@UserSex,@UserPhone,@AccountNum,@Pwd,@CreateUser,@CreateTime,@UpdateUser,@UpdateTime,@IsDelete,@Remark)");
                        // 添加查询结果
                        strSql.Append(";select @UserID;");
                        SqlParameter[] parameters =
                        {
                            new SqlParameter("@UserID",     SqlDbType.UniqueIdentifier,  16),
                            new SqlParameter("@UserName",   SqlDbType.NVarChar,         255),
                            new SqlParameter("@UserAvatar", SqlDbType.NVarChar,         255),
                            new SqlParameter("@UserSex",    SqlDbType.Int,                4),
                            new SqlParameter("@UserPhone",  SqlDbType.NVarChar,          50),
                            new SqlParameter("@AccountNum", SqlDbType.NVarChar,          50),
                            new SqlParameter("@Pwd",        SqlDbType.NVarChar,         255),
                            new SqlParameter("@CreateUser", SqlDbType.UniqueIdentifier,  16),
                            new SqlParameter("@CreateTime", SqlDbType.DateTime),
                            new SqlParameter("@UpdateUser", SqlDbType.UniqueIdentifier,  16),
                            new SqlParameter("@UpdateTime", SqlDbType.DateTime),
                            new SqlParameter("@IsDelete",   SqlDbType.Bit,                1),
                            new SqlParameter("@Remark",     SqlDbType.NVarChar, 50)
                        };
                        parameters[0].Value = Guid.NewGuid();
                        parameters[1].Value = model.UserName;
                        parameters[2].Value = model.UserAvatar;
                        parameters[3].Value = model.UserSex;
                        parameters[4].Value = model.UserPhone;
                        parameters[5].Value = model.AccountNum;
                        parameters[6].Value = model.Pwd;
                        // parameters[7].Value = Guid.NewGuid();
                        parameters[7].Value = model.CreateUser;

                        parameters[8].Value = model.CreateTime;
                        // parameters[9].Value = Guid.NewGuid();
                        parameters[9].Value = model.UpdateUser;

                        parameters[10].Value = model.UpdateTime;
                        parameters[11].Value = model.IsDelete;
                        parameters[12].Value = model.Remark;


                        object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters);
                        if (obj != null)
                        {
                            model.UserID = new Guid(Convert.ToString(obj));
                            // 判断想不想要加入日志,如果不想加入日志,可以传null
                            bool processLogResult = true;
                            if (sysProcessLogModel != null)
                            {
                                //添加时要告诉另一个语句用的是,哪一个连接,哪一个事务
                                processLogResult = new Sys_ProcessLog_DAL().Add(conn, trans, sysProcessLogModel);
                            }
                            if (processLogResult)
                            {
                                result = true;
                                trans.Commit();
                            }
                            else
                            {
                                result = false;
                                trans.Rollback();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        result = false;
                        trans.Rollback();
                    }
                }
            }

            return(result);
        }