/// <summary> /// 新增一条记录 /// </summary> /// <param name="model"></param> /// <returns></returns> public int Add(score model) { StringBuilder sql = new StringBuilder(); sql.Append("insert into [dbo].[score] ([userid],[dimensionid],[score],[num])"); sql.Append(" values (@userid,@dimensionid,@userscore,@num)"); sql.Append(" set @id=@@IDENTITY"); SqlParameter[] cmdParams = new SqlParameter[]{ new SqlParameter("@id", SqlDbType.Int, 4) { Value = model.id, Direction = ParameterDirection.Output }, new SqlParameter("@userid", SqlDbType.Int, 4) { Value = model.userid }, new SqlParameter("@dimensionid", SqlDbType.Int, 4) { Value = model.dimensionid }, new SqlParameter("@userscore", SqlDbType.VarChar, 53) { Value = model.userscore }, new SqlParameter("@num", SqlDbType.Int, 4) { Value = model.num } }; int result = Convert.ToInt32(MSEntLibSqlHelper.ExecuteNonQueryBySql(sql.ToString(), cmdParams)); model.id = Convert.ToInt32(cmdParams[0].Value); return result; }
/// <summary> /// 获取数据集 /// </summary> /// <param name="where"></param> /// <param name="orderBy"></param> /// <returns></returns> public List<score> GetList(string where, string orderBy) { StringBuilder sql = new StringBuilder(); sql.Append("select * from [dbo].[score]"); if (!string.IsNullOrEmpty(where)) sql.Append(" where " + where); if (!string.IsNullOrEmpty(orderBy)) sql.Append(" order by " + orderBy); List<score> list = new List<score>(); using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql.ToString())) { while (reader.Read()) { score model = new score(); ConvertToModel(reader, model); list.Add(model); } } return list; }
/// <summary> /// 获取分页数据集 /// </summary> /// <param name="pageSize"></param> /// <param name="pageIndex"></param> /// <param name="where"></param> /// <param name="orderBy"></param> /// <param name="recordCount"></param> /// <returns></returns> public List<score> GetList(int pageSize, int pageIndex, string where, string orderBy, out int recordCount) { if (string.IsNullOrEmpty(orderBy)) throw new ArgumentNullException(); StringBuilder sb = new StringBuilder(); sb.Append("select count(1) from [dbo].[score]"); if (!string.IsNullOrEmpty(where)) sb.Append(" where " + where); recordCount = Convert.ToInt32(MSEntLibSqlHelper.ExecuteScalarBySql(sb.ToString())); int start = (pageIndex - 1) * pageSize + 1; int end = pageIndex * pageSize; StringBuilder sql = new StringBuilder(); sql.Append("select * from (select *,ROW_NUMBER() over (order by " + orderBy + ") as [RowNum] from [dbo].[score]"); if (!string.IsNullOrEmpty(where)) sql.Append(" where " + where); sql.Append(") as T where [RowNum] between " + start + " and " + end); List<score> list = new List<score>(); using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql.ToString())) { while (reader.Read()) { score model = new score(); ConvertToModel(reader, model); list.Add(model); } } return list; }
private void ConvertToModel(IDataReader reader, score model) { if (reader["id"] != DBNull.Value) model.id = Convert.ToInt32(reader["id"]); if (reader["userid"] != DBNull.Value) model.userid = Convert.ToInt32(reader["userid"]); if (reader["dimensionid"] != DBNull.Value) model.dimensionid = Convert.ToInt32(reader["dimensionid"]); if (reader["score"] != DBNull.Value) model.userscore = Convert.ToDecimal(reader["score"]); if (reader["num"] != DBNull.Value) model.num = Convert.ToInt32(reader["num"]); }
/// <summary> /// 更新一条记录 /// </summary> /// <param name="model"></param> /// <returns></returns> public int Update(score model) { StringBuilder sql = new StringBuilder(); sql.Append("update [dbo].[score] set "); sql.Append("[userid]=@userid,[dimensionid]=@dimensionid,[score]=@userscore,[num]=@num"); sql.Append(" where [id]=@id"); SqlParameter[] cmdParams = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) { Value = model.id }, new SqlParameter("@userid", SqlDbType.Int, 4) { Value = model.userid }, new SqlParameter("@dimensionid", SqlDbType.Int, 4) { Value = model.dimensionid }, new SqlParameter("@userscore", SqlDbType.VarChar, 53) { Value = model.userscore }, new SqlParameter("@num", SqlDbType.Int, 4) { Value = model.num } }; return MSEntLibSqlHelper.ExecuteNonQueryBySql(sql.ToString(), cmdParams); }
/// <summary> /// 取得一条记录 /// </summary> /// <param name="id"></param> /// <param name="where"></param> /// <returns></returns> public score GetModel(int id, string where) { string sql = "select * from [dbo].[score] where [id]=@Id"; if (!string.IsNullOrEmpty(where)) sql += " and " + where; SqlParameter[] cmdParams = new SqlParameter[]{ new SqlParameter("@Id", SqlDbType.Int, 4) { Value = id } }; using (IDataReader reader = MSEntLibSqlHelper.ExecuteDataReaderBySql(sql, cmdParams)) { if (reader.Read()) { score model = new score(); ConvertToModel(reader, model); return model; } else { return null; } } }
/// <summary> /// 新增一条记录 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool Add(score model) { return dal.Add(model) > 0; }
/// <summary> /// 更新一条记录 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool Update(score model) { return dal.Update(model) > 0; }