public static User GetItem(string username, string password) { User myUser = new User(); myUser.UserName = username; myUser.Pwd = password; return UserService.GetItem(myUser); }
/// <summary> /// 查询一条用户记录. /// </summary> /// <param name="myUser">User 模型</param> /// <returns>User模型 包含一条用户记录.</returns> public static User GetItem(User myUser) { string sql; MySqlParameter[] parms = null; if (myUser.UID > 0) { sql = "select * from T_Users where uid=?uid"; parms = new MySqlParameter[] { new MySqlParameter("?uid", MySqlDbType.Int32) }; parms[0].Value = myUser.UID; } else if(!string.IsNullOrEmpty(myUser.UserName) && !string.IsNullOrEmpty(myUser.Pwd)) { sql = "select * from T_Users where username=?username and password=?pwd"; parms = new MySqlParameter[] { new MySqlParameter("?username",MySqlDbType.VarChar), new MySqlParameter("?pwd",MySqlDbType.VarChar) }; parms[0].Value = myUser.UserName; parms[1].Value = myUser.Pwd; } else { sql = "select * from T_Users where username=?username"; parms = new MySqlParameter[] { new MySqlParameter("?username", MySqlDbType.VarChar) }; parms[0].Value = myUser.UserName; } User user = new User(); MySqlDataReader reader = DbHelper.ExecuteDataReader(sql, parms); if (reader.HasRows) { if (reader.Read()) { user = FillDataRecord(reader); } } reader.Close(); return user; }
protected void btnAdd_Click(object sender, EventArgs e) { User myUser = new User(); myUser.UserName = this.tbxUsername.Text.Trim(); myUser.Pwd = this.tbxPassword.Text; myUser.JoinDate = DateTime.Now; myUser.Login_IP = Request.ServerVariables["REMOTE_ADDR"].ToString(); myUser.Login_Date = DateTime.Now; if (UserManage.Insert(myUser)) { ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('添加用户成功!!!')", true); } else { this.lblError.Visible = true; } }
protected void Page_Load(object sender, EventArgs e) { RegexStringValidator rsv = new RegexStringValidator(@"^[1-9]\d*$"); try { rsv.Validate(Request.QueryString["id"].Trim()); myUser = UserManage.GetItem(Convert.ToInt32(Request.QueryString["id"])); this.tbxNewPass.Enabled = true; this.tbxOldPass.Enabled = true; this.tbxCfmPass.Enabled = true; this.btnConfirm.Enabled = true; this.lblUser.Text = myUser.UserName; } catch (ArgumentException ex) { this.lblError.Visible = true; } }
public void TestGetList() { User user = new User(); user.UID = 1; User myuser = UserService.GetItem(user); }
/// <summary> /// 初始化一个user类实例,并填充数据. /// </summary> /// <param name="myDataRecord"></param> /// <returns></returns> private static User FillDataRecord(IDataRecord myDataRecord) { User myUser = new User(); myUser.UID = myDataRecord.GetInt32(myDataRecord.GetOrdinal("uid")); myUser.UserName = myDataRecord.GetString(myDataRecord.GetOrdinal("username")); myUser.Pwd = myDataRecord.GetString(myDataRecord.GetOrdinal("password")); myUser.JoinDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("joindate")); if(!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("login_ip"))) { myUser.Login_IP = myDataRecord.GetString(myDataRecord.GetOrdinal("login_ip")); } if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("login_date"))) { myUser.Login_Date = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("login_date")); } return myUser; }
/// <summary> /// 修改用户记录. /// </summary> /// <param name="myUser">User 模型</param> /// <returns>返回<c>true</c>修改成功,或<c>false</c>修改失败.</returns> public static bool Update(User myUser) { int result = 0; string sql; MySqlParameter[] parms = null; if (!string.IsNullOrEmpty(myUser.Pwd)) { sql = "update T_Users set password=?pwd where uid=?uid"; parms = new MySqlParameter[]{ new MySqlParameter("?pwd",MySqlDbType.VarChar), new MySqlParameter("?uid",MySqlDbType.Int32) }; parms[0].Value = myUser.Pwd; parms[1].Value = myUser.UID; } else { sql = "update T_Users set login_ip=?login_ip, login_date=?login_date where uid=?uid"; parms = new MySqlParameter[] { new MySqlParameter("?login_ip",MySqlDbType.VarChar), new MySqlParameter("?login_date",MySqlDbType.Date), new MySqlParameter("?uid",MySqlDbType.Int32) }; parms[0].Value = myUser.Login_IP; parms[1].Value = myUser.Login_Date; parms[2].Value = myUser.UID; } result = DbHelper.ExecuteNonQuery(sql, parms); return result > 0; }
/// <summary> /// 插入用户记录. /// </summary> /// <param name="myUser">User 模型</param> /// <returns>返回<c>true</c>插入成功,或<c>false</c>插入失败.</returns> public static bool Insert(User myUser) { int result = 0; string sql = @" INSERT INTO T_Users ( username, password, joindate, login_ip, login_date ) VALUES ( ?username, ?pwd, ?joindate, ?login_ip, ?login_date )"; MySqlParameter[] parms = { new MySqlParameter("?username",MySqlDbType.VarChar), new MySqlParameter("?pwd",MySqlDbType.VarChar), new MySqlParameter("?joindate",MySqlDbType.Date), new MySqlParameter("?login_ip",MySqlDbType.VarChar), new MySqlParameter("?login_date",MySqlDbType.Date) }; parms[0].Value = myUser.UserName; parms[1].Value = myUser.Pwd; parms[2].Value = myUser.JoinDate; parms[3].Value = myUser.Login_IP; parms[4].Value = myUser.Login_Date; result = DbHelper.ExecuteNonQuery(sql,parms); return result > 0; }
public static User GetItem(int uid) { User myUser = new User(); myUser.UID = uid; return UserService.GetItem(myUser); }
public static User GetItem(string username) { User myUser = new User(); myUser.UserName = username; return UserService.GetItem(myUser); }
public static bool Update(int uid, string loginip, DateTime logindate) { User myUser = new User(); myUser.UID = uid; myUser.Login_IP = loginip; myUser.Login_Date = logindate; return UserService.Update(myUser); }
public static bool Update(int uid, string password) { User myUser = new User(); myUser.UID = uid; myUser.Pwd = password; return UserService.Update(myUser); }
public static bool Insert(User myUser) { if (string.IsNullOrEmpty(myUser.UserName) || string.IsNullOrEmpty(myUser.Pwd) || string.IsNullOrEmpty(myUser.Login_IP)) { return false; } else { myUser.JoinDate = DateTime.Now; myUser.Login_Date = DateTime.Now; return UserService.Insert(myUser); } }