using System; using System.Collections.Generic; using System.Data; using System.Text; using System.Data.SqlClient; /// /// 管理员 /// namespace Basic.DAL { public class manager { /// /// 获得查询分页数据 /// public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount) { StringBuilder strSql = new StringBuilder(); strSql.Append("select * from tb_manager"); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString()))); return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder)); } /// /// 增加一条数据 /// public int Add(Model.manager model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into tb_manager("); strSql.Append("role_id,role_type,shop_id,user_name,user_pwd,real_name,telephone,email,is_lock,add_time)"); strSql.Append(" values ("); strSql.Append("@role_id,@role_type,@shop_id,@user_name,@user_pwd,@real_name,@telephone,@email,@is_lock,@add_time)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@role_id", SqlDbType.Int,10), new SqlParameter("@role_type", SqlDbType.Int,10), new SqlParameter("@shop_id", SqlDbType.Int,10), new SqlParameter("@user_name", SqlDbType.NVarChar,100), new SqlParameter("@user_pwd", SqlDbType.NVarChar,100), new SqlParameter("@real_name", SqlDbType.NVarChar,50), new SqlParameter("@telephone", SqlDbType.NVarChar,30), new SqlParameter("@email", SqlDbType.NVarChar,30), new SqlParameter("@is_lock", SqlDbType.Int,10), new SqlParameter("@add_time", SqlDbType.DateTime,23)}; parameters[0].Value = model.role_id; parameters[1].Value = model.role_type; parameters[2].Value = model.shop_id; parameters[3].Value = model.user_name; parameters[4].Value = model.user_pwd; parameters[5].Value = model.real_name; parameters[6].Value = model.telephone; parameters[7].Value = model.email; parameters[8].Value = model.is_lock; parameters[9].Value = model.add_time; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return 0; } else { return Convert.ToInt32(obj); } } /// /// 得到一个对象实体 /// public Model.manager GetModel(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,role_id,role_type,shop_id,user_name,user_pwd,real_name,telephone,email,is_lock,add_time from tb_manager"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = id; Model.manager model = new Model.manager(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["id"].ToString())) { model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["role_id"].ToString())) { model.role_id = int.Parse(ds.Tables[0].Rows[0]["role_id"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["role_type"].ToString())) { model.role_type = int.Parse(ds.Tables[0].Rows[0]["role_type"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["shop_id"].ToString())) { model.shop_id = int.Parse(ds.Tables[0].Rows[0]["shop_id"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["user_name"].ToString())) { model.user_name = ds.Tables[0].Rows[0]["user_name"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["user_pwd"].ToString())) { model.user_pwd = ds.Tables[0].Rows[0]["user_pwd"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["real_name"].ToString())) { model.real_name = ds.Tables[0].Rows[0]["real_name"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["telephone"].ToString())) { model.telephone = ds.Tables[0].Rows[0]["telephone"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["email"].ToString())) { model.email = ds.Tables[0].Rows[0]["email"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["is_lock"].ToString())) { model.is_lock = int.Parse(ds.Tables[0].Rows[0]["is_lock"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["add_time"].ToString())) { model.add_time = DateTime.Parse(ds.Tables[0].Rows[0]["add_time"].ToString()); } return model; } else { return null; } } /// /// 是否存在该记录 /// public bool Exists(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from tb_manager"); strSql.Append(" where id=@id "); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = id; return DbHelperSQL.Exists(strSql.ToString(), parameters); } /// /// 更新一条数据 /// public bool Update(Model.manager model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tb_manager set "); strSql.Append("role_id=@role_id,"); strSql.Append("role_type=@role_type,"); strSql.Append("shop_id=@shop_id,"); strSql.Append("user_name=@user_name,"); strSql.Append("user_pwd=@user_pwd,"); strSql.Append("real_name=@real_name,"); strSql.Append("telephone=@telephone,"); strSql.Append("email=@email,"); strSql.Append("is_lock=@is_lock,"); strSql.Append("add_time=@add_time"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@role_id", SqlDbType.Int,10), new SqlParameter("@role_type", SqlDbType.Int,10), new SqlParameter("@shop_id", SqlDbType.Int,10), new SqlParameter("@user_name", SqlDbType.NVarChar,100), new SqlParameter("@user_pwd", SqlDbType.NVarChar,100), new SqlParameter("@real_name", SqlDbType.NVarChar,50), new SqlParameter("@telephone", SqlDbType.NVarChar,30), new SqlParameter("@email", SqlDbType.NVarChar,30), new SqlParameter("@is_lock", SqlDbType.Int,10), new SqlParameter("@add_time", SqlDbType.DateTime,23), new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = model.role_id; parameters[1].Value = model.role_type; parameters[2].Value = model.shop_id; parameters[3].Value = model.user_name; parameters[4].Value = model.user_pwd; parameters[5].Value = model.real_name; parameters[6].Value = model.telephone; parameters[7].Value = model.email; parameters[8].Value = model.is_lock; parameters[9].Value = model.add_time; parameters[10].Value = model.id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } /// /// 删除一条数据 /// public bool Delete(int id) { List sqllist = new List(); StringBuilder strSql = new StringBuilder(); strSql.Append("delete from tb_manager_log "); strSql.Append(" where user_id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = id; CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters); sqllist.Add(cmd); StringBuilder strSql1 = new StringBuilder(); strSql1.Append("delete from tb_manager "); strSql1.Append(" where id=@id"); SqlParameter[] parameters1 = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters1[0].Value = id; cmd = new CommandInfo(strSql1.ToString(), parameters1); sqllist.Add(cmd); int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist); if (rowsAffected > 0) { return true; } else { return false; } } /// /// 根据用户名密码返回一个实体 /// public Model.manager GetModel(string user_name, string user_pwd) { StringBuilder strSql = new StringBuilder(); strSql.Append("select id from tb_manager"); strSql.Append(" where user_name=@user_name and user_pwd=@user_pwd and is_lock=0"); SqlParameter[] parameters = { new SqlParameter("@user_name", SqlDbType.NVarChar,100), new SqlParameter("@user_pwd", SqlDbType.NVarChar,100)}; parameters[0].Value = user_name; parameters[1].Value = user_pwd; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj != null) { return GetModel(Convert.ToInt32(obj)); } return null; } /// /// 得到一个对象实体 /// public Model.manager GetModel(string username) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,role_id,role_type,shop_id,user_name,user_pwd,real_name,telephone,email,is_lock,add_time from tb_manager"); strSql.Append(" where user_name=@user_name"); SqlParameter[] parameters = { new SqlParameter("@user_name", SqlDbType.NVarChar,255)}; parameters[0].Value = username; Model.manager model = new Model.manager(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["id"].ToString())) { model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["role_id"].ToString())) { model.role_id = int.Parse(ds.Tables[0].Rows[0]["role_id"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["role_type"].ToString())) { model.role_type = int.Parse(ds.Tables[0].Rows[0]["role_type"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["shop_id"].ToString())) { model.shop_id = int.Parse(ds.Tables[0].Rows[0]["shop_id"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["user_name"].ToString())) { model.user_name = ds.Tables[0].Rows[0]["user_name"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["user_pwd"].ToString())) { model.user_pwd = ds.Tables[0].Rows[0]["user_pwd"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["real_name"].ToString())) { model.real_name = ds.Tables[0].Rows[0]["real_name"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["telephone"].ToString())) { model.telephone = ds.Tables[0].Rows[0]["telephone"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["email"].ToString())) { model.email = ds.Tables[0].Rows[0]["email"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["is_lock"].ToString())) { model.is_lock = int.Parse(ds.Tables[0].Rows[0]["is_lock"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["add_time"].ToString())) { model.add_time = DateTime.Parse(ds.Tables[0].Rows[0]["add_time"].ToString()); } return model; } else { return null; } } #region /// /// 获得查询分页数据 /// public DataSet GetList(string _strWhere, string _orderby, int _pageSize, int _page, out int recordCount) { BasicPage bp = new BasicPage(); StringBuilder str_Sql = new StringBuilder(); str_Sql.Append("select top " + _pageSize + " id,user_name,role_id,real_name,shop_id,telephone,email,add_time,is_lock from tb_manager"); str_Sql.Append(" where "); str_Sql.Append(_strWhere); str_Sql.Append(" and id not in "); str_Sql.Append(" ( "); str_Sql.Append(" select top " + (_page - 1) * _pageSize + " id from tb_manager "); str_Sql.Append(" where "); str_Sql.Append(_strWhere); str_Sql.Append(" order by "); str_Sql.Append(_orderby); str_Sql.Append(" ) "); str_Sql.Append(" order by "); str_Sql.Append(_orderby); DataSet dst = bp.SelectDataBase("tb_manager", str_Sql.ToString()); // recordCount = totlePage(showTotal(_strWhere), _pageSize); return dst; } //总页数 public int totlePage(int Total, int PageSize) { if (Total % PageSize == 0) { return Total / PageSize; } else { return Total / PageSize + 1; } } //总条数 protected int showTotal(string strSql) { BasicPage bp = new BasicPage(); int intTotal = 0; SqlDataReader myread = bp.getRead("select count(id) as CountId from tb_manager where " + strSql); if (myread.Read()) { intTotal = Convert.ToInt32(myread["CountId"].ToString()); } myread.Close(); return intTotal; } #endregion } }