/// <summary> /// 更新一条数据 /// </summary> public bool Update(cn.itcast.bookshop.Model.Users model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update Users set "); strSql.Append("LoginPwd=@LoginPwd,"); strSql.Append("Name=@Name,"); strSql.Append("Address=@Address,"); strSql.Append("Phone=@Phone,"); strSql.Append("UserStateId=@UserStateId"); strSql.Append(" where Id=@Id"); SqlParameter[] parameters = { new SqlParameter("@LoginPwd", SqlDbType.NVarChar, 50), new SqlParameter("@Name", SqlDbType.NVarChar, 50), new SqlParameter("@Address", SqlDbType.NVarChar, 200), new SqlParameter("@Phone", SqlDbType.NVarChar, 100), new SqlParameter("@UserStateId", SqlDbType.Int, 4), new SqlParameter("@Id", SqlDbType.Int, 4), new SqlParameter("@LoginId", SqlDbType.NVarChar, 50), new SqlParameter("@Mail", SqlDbType.NVarChar, 100) }; parameters[0].Value = model.LoginPwd; parameters[1].Value = model.Name; parameters[2].Value = model.Address; parameters[3].Value = model.Phone; parameters[4].Value = model.UserStateId; parameters[5].Value = model.Id; parameters[6].Value = model.LoginId; parameters[7].Value = model.Mail; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 得到一个对象实体 /// </summary> public cn.itcast.bookshop.Model.Users DataRowToModel(DataRow row) { cn.itcast.bookshop.Model.Users model = new cn.itcast.bookshop.Model.Users(); if (row != null) { if (row["Id"] != null && row["Id"].ToString() != "") { model.Id = int.Parse(row["Id"].ToString()); } if (row["LoginId"] != null) { model.LoginId = row["LoginId"].ToString(); } if (row["LoginPwd"] != null) { model.LoginPwd = row["LoginPwd"].ToString(); } if (row["Name"] != null) { model.Name = row["Name"].ToString(); } if (row["Address"] != null) { model.Address = row["Address"].ToString(); } if (row["Phone"] != null) { model.Phone = row["Phone"].ToString(); } if (row["Mail"] != null) { model.Mail = row["Mail"].ToString(); } if (row["UserStateId"] != null && row["UserStateId"].ToString() != "") { model.UserStateId = int.Parse(row["UserStateId"].ToString()); } } return(model); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(cn.itcast.bookshop.Model.Users model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into Users("); strSql.Append("LoginId,LoginPwd,Name,Address,Phone,Mail,UserStateId)"); strSql.Append(" values ("); strSql.Append("@LoginId,@LoginPwd,@Name,@Address,@Phone,@Mail,@UserStateId)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@LoginId", SqlDbType.NVarChar, 50), new SqlParameter("@LoginPwd", SqlDbType.NVarChar, 50), new SqlParameter("@Name", SqlDbType.NVarChar, 50), new SqlParameter("@Address", SqlDbType.NVarChar, 200), new SqlParameter("@Phone", SqlDbType.NVarChar, 100), new SqlParameter("@Mail", SqlDbType.NVarChar, 100), new SqlParameter("@UserStateId", SqlDbType.Int, 4) }; parameters[0].Value = model.LoginId; parameters[1].Value = model.LoginPwd; parameters[2].Value = model.Name; parameters[3].Value = model.Address; parameters[4].Value = model.Phone; parameters[5].Value = model.Mail; parameters[6].Value = model.UserStateId; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 得到一个对象实体 /// </summary> public cn.itcast.bookshop.Model.Users GetModel(int Id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 Id,LoginId,LoginPwd,Name,Address,Phone,Mail,UserStateId from Users "); strSql.Append(" where Id=@Id"); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Int, 4) }; parameters[0].Value = Id; cn.itcast.bookshop.Model.Users model = new cn.itcast.bookshop.Model.Users(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }