using System; using System.Data; using System.Text; using System.Data.SqlClient; namespace Basic.DAL { /// /// 数据访问层 /// public partial class coupon { #region 此代码由工具生成 /// /// 增加一条数据 /// public int Add(Basic.Model.coupon model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into tb_coupon("); strSql.Append("paixu,name,start_time,end_time,number,purchase_amount,coupon_amount,status,add_time)"); strSql.Append(" values ("); strSql.Append("@paixu,@name,@start_time,@end_time,@number,@purchase_amount,@coupon_amount,@status,@add_time)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@paixu", SqlDbType.Int,10), new SqlParameter("@name", SqlDbType.NVarChar,255), new SqlParameter("@start_time", SqlDbType.DateTime,23), new SqlParameter("@end_time", SqlDbType.DateTime,23), new SqlParameter("@number", SqlDbType.Int,10), new SqlParameter("@purchase_amount", SqlDbType.Decimal,18), new SqlParameter("@coupon_amount", SqlDbType.Decimal,18), new SqlParameter("@status", SqlDbType.Int,10), new SqlParameter("@add_time", SqlDbType.DateTime,23)}; parameters[0].Value = model.paixu; parameters[1].Value = model.name; parameters[2].Value = model.start_time; parameters[3].Value = model.end_time; parameters[4].Value = model.number; parameters[5].Value = model.purchase_amount; parameters[6].Value = model.coupon_amount; parameters[7].Value = model.status; parameters[8].Value = model.add_time; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return 0; } else { return Convert.ToInt32(obj); } } /// /// 更新一条数据 /// public bool Update(Basic.Model.coupon model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tb_coupon set "); strSql.Append("paixu=@paixu,"); strSql.Append("name=@name,"); strSql.Append("start_time=@start_time,"); strSql.Append("end_time=@end_time,"); strSql.Append("number=@number,"); strSql.Append("purchase_amount=@purchase_amount,"); strSql.Append("coupon_amount=@coupon_amount,"); strSql.Append("status=@status,"); strSql.Append("add_time=@add_time"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@paixu", SqlDbType.Int,10), new SqlParameter("@name", SqlDbType.NVarChar,255), new SqlParameter("@start_time", SqlDbType.DateTime,23), new SqlParameter("@end_time", SqlDbType.DateTime,23), new SqlParameter("@number", SqlDbType.Int,10), new SqlParameter("@purchase_amount", SqlDbType.Decimal,18), new SqlParameter("@coupon_amount", SqlDbType.Decimal,18), new SqlParameter("@status", SqlDbType.Int,10), new SqlParameter("@add_time", SqlDbType.DateTime,23), new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = model.paixu; parameters[1].Value = model.name; parameters[2].Value = model.start_time; parameters[3].Value = model.end_time; parameters[4].Value = model.number; parameters[5].Value = model.purchase_amount; parameters[6].Value = model.coupon_amount; parameters[7].Value = model.status; parameters[8].Value = model.add_time; parameters[9].Value = model.id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } /// /// 删除一条数据 /// public bool Delete(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("delete from tb_coupon"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } /// /// 得到一个对象实体 /// public Basic.Model.coupon GetModel(int id) { StringBuilder strSql=new StringBuilder(); strSql.Append("select top 1 id,paixu,name,start_time,end_time,number,purchase_amount,coupon_amount,status,add_time from tb_coupon"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = id; Basic.Model.coupon model = new Basic.Model.coupon(); 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]["paixu"].ToString())) { model.paixu = int.Parse(ds.Tables[0].Rows[0]["paixu"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["name"].ToString())) { model.name = ds.Tables[0].Rows[0]["name"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["start_time"].ToString())) { model.start_time = DateTime.Parse(ds.Tables[0].Rows[0]["start_time"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["end_time"].ToString())) { model.end_time = DateTime.Parse(ds.Tables[0].Rows[0]["end_time"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["number"].ToString())) { model.number = int.Parse(ds.Tables[0].Rows[0]["number"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["purchase_amount"].ToString())) { model.purchase_amount = decimal.Parse(ds.Tables[0].Rows[0]["purchase_amount"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["coupon_amount"].ToString())) { model.coupon_amount = decimal.Parse(ds.Tables[0].Rows[0]["coupon_amount"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["status"].ToString())) { model.status = int.Parse(ds.Tables[0].Rows[0]["status"].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 DataSet GetList(int Top, string strWhere, string filedOrder) { StringBuilder strSql = new StringBuilder(); strSql.Append("select "); if (Top > 0) { strSql.Append(" top " + Top.ToString()); } strSql.Append("id,paixu,name,start_time,end_time,number,purchase_amount,coupon_amount,status,add_time"); strSql.Append(" from tb_coupon"); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } strSql.Append(" order by " + filedOrder); return DbHelperSQL.Query(strSql.ToString()); } /// /// 获得查询分页数据 /// 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,paixu,name,start_time,end_time,number,purchase_amount,coupon_amount,status,add_time from tb_coupon"); 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_coupon "); 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_coupon", 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_coupon where " + strSql); if (myread.Read()) { intTotal = Convert.ToInt32(myread["CountId"].ToString()); } myread.Close(); return intTotal; } /// /// 是否存在该记录 /// public bool Exists(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from tb_coupon"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = id; return DbHelperSQL.Exists(strSql.ToString(), parameters); } /// /// 修改一列数据 /// public void UpdateField(int id, string strValue) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tb_coupon set " + strValue); strSql.Append(" where id=" + id); DbHelperSQL.ExecuteSql(strSql.ToString()); } #endregion } }