/// <summary>
 /// 得到一个对象实体
 /// </summary>
 public yonghuModel DataRowToModel(DataRow row)
 {
     yonghuModel model=new yonghuModel();
     if (row != null)
     {
         if(row["nc_uid"]!=null)
         {
             model.nc_uid=row["nc_uid"].ToString();
         }
         if(row["nvc_username"]!=null)
         {
             model.nvc_username=row["nvc_username"].ToString();
         }
         if(row["nvc_pwd"]!=null)
         {
             model.nvc_pwd=row["nvc_pwd"].ToString();
         }
         if(row["int_right"]!=null && row["int_right"].ToString()!="")
         {
             model.int_right=int.Parse(row["int_right"].ToString());
         }
         if(row["dt_register"]!=null && row["dt_register"].ToString()!="")
         {
             model.dt_register=DateTime.Parse(row["dt_register"].ToString());
         }
     }
     return model;
 }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(yonghuModel model)
        {
            StringBuilder strSql=new StringBuilder();
            strSql.Append("insert into yonghu(");
            strSql.Append("nvc_username,nvc_pwd,int_right)");
            strSql.Append(" values (");
            strSql.Append("@nvc_username,@nvc_pwd,@int_right)");
            SqlParameter[] parameters = {
                    new SqlParameter("@nvc_username", SqlDbType.NVarChar,50),
                    new SqlParameter("@nvc_pwd", SqlDbType.NVarChar,50),
                    new SqlParameter("@int_right", SqlDbType.Int,4)};
            parameters[0].Value = model.nvc_username;
            parameters[1].Value = model.nvc_pwd;
            parameters[2].Value = model.int_right;

            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    protected void Page_Load(object sender, EventArgs e)
    {
        string caozuo = null;
        if (!string.IsNullOrEmpty(Request.Form["action"]))//获取数据类型
        {
            caozuo = Request.Form["action"];
        }
        if (caozuo == "Register")
        {
            #region 注册
            #region 用户名
            string uname = null;
            if (!string.IsNullOrEmpty(Request.Form["CreateUsername"]))//获取数据类型
            {
                string un = Request.Form["CreateUsername"];
                if (un.Length > 0 && un.Length <= 20)
                {
                    if (Regex.IsMatch(un, @"^([\u4e00-\u9fa5]|[a-zA-Z]|[0-9]|-){0,}$") || Regex.IsMatch(un, @"^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,4}$"))
                    {
                        uname = un;
                    }
                }
            }
            #endregion
            #region 密码
            string pwd = null;
            if (!string.IsNullOrEmpty(Request.Form["CreatePassword"]))//获取数据类型
            {
                string up = Request.Form["CreatePassword"];
                if (up.Length > 6 && up.Length <= 16 && up != "123456" && up != "654321" && up != "111222")
                {
                    pwd = up;
                }
            }
            #endregion
            #region 重复密码
            string repwd = null;
            if (!string.IsNullOrEmpty(Request.Form["CreateRePassword"]))//获取数据类型
            {
                string urd = Request.Form["CreateRePassword"];
                if (urd == pwd)
                {
                    repwd = urd;
                }
            }
            #endregion
            #region 操作
            string action = null;
            if (!string.IsNullOrEmpty(Request.Form["RegisterSubmit"]))//获取数据类型
            {
                action = Request.Form["RegisterSubmit"];
            }
            #endregion
            if (pwd == repwd && action == "创建账户" && string.IsNullOrEmpty(uname) != null)
            {

                //添加一条记录
                yonghuModel yhm = new yonghuModel();
                yhm.nvc_username = uname;
                yhm.nvc_pwd = new MD5Encrypt().GetMD5(pwd + uname);
                yhm.int_right = 1;
                bool isInsertOk = new yonghuDAL().Add(yhm);

                if (isInsertOk == true)
                {
                    Response.Write("<script>javascript:alert('注册成功!');window.parent.location.reload();</script>");
                }
            }
            else
            {
                Response.Write("<script>javascript:alert('注册失败!请重试!');</script>");
            }
            #endregion
            Response.End();
        }
        Page.DataBind();
    }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(yonghuModel model)
        {
            StringBuilder strSql=new StringBuilder();
            strSql.Append("update yonghu set ");
            strSql.Append("nvc_username=@nvc_username,");
            strSql.Append("nvc_pwd=@nvc_pwd,");
            strSql.Append("int_right=@int_right,");
            strSql.Append("dt_register=@dt_register");
            strSql.Append(" where nc_uid=@nc_uid ");
            SqlParameter[] parameters = {
                    new SqlParameter("@nvc_username", SqlDbType.NVarChar,50),
                    new SqlParameter("@nvc_pwd", SqlDbType.NVarChar,50),
                    new SqlParameter("@int_right", SqlDbType.Int,4),
                    new SqlParameter("@dt_register", SqlDbType.DateTime),
                    new SqlParameter("@nc_uid", SqlDbType.NChar,20)};
            parameters[0].Value = model.nvc_username;
            parameters[1].Value = model.nvc_pwd;
            parameters[2].Value = model.int_right;
            parameters[3].Value = model.dt_register;
            parameters[4].Value = model.nc_uid;

            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public yonghuModel GetModelByUsername(string username)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 nc_uid,nvc_username,nvc_pwd,int_right,dt_register from yonghu ");
            strSql.Append(" where nvc_username=@nvc_username ");
            SqlParameter[] parameters = {
                    new SqlParameter("@nvc_username", SqlDbType.NVarChar,50)			};
            parameters[0].Value = username;

            yonghuModel model = new yonghuModel();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }