/// <summary> /// 转换SqlDataReader类型数据记录为实体 /// </summary> private static Entity.CollectInfoEntity ConvertToEntityFromDataReader(SqlDataReader reader) { Entity.CollectInfoEntity entity = new Entity.CollectInfoEntity(); entity.Id = Convert.ToInt32(reader["Id"]); entity.StationId = Convert.ToInt32(reader["StationId"]); entity.CollectType = Convert.ToInt32(reader["CollectType"]); entity.UserName = reader["UserName"].ToString(); entity.Phone = reader["Phone"].ToString(); entity.Reason = reader["Reason"].ToString(); entity.AddTime = Convert.ToDateTime(reader["AddTime"]); return(entity); }
/// <summary> /// 更新数据库中一条记录(根据主键条件) /// </summary> /// <param name="entity">Entity.CollectInfoEntity实体类</param> public static void Update(Entity.CollectInfoEntity entity) { List <SqlParameter> commandParms = new List <SqlParameter>(); commandParms.Add(SqlHelper.CreateParam("@Id", SqlDbType.Int, 0, ParameterDirection.Input, entity.Id)); commandParms.Add(SqlHelper.CreateParam("@StationId", SqlDbType.Int, 0, ParameterDirection.Input, entity.StationId)); commandParms.Add(SqlHelper.CreateParam("@CollectType", SqlDbType.Int, 0, ParameterDirection.Input, entity.CollectType)); commandParms.Add(SqlHelper.CreateParam("@UserName", SqlDbType.NVarChar, 50, ParameterDirection.Input, entity.UserName)); commandParms.Add(SqlHelper.CreateParam("@Phone", SqlDbType.VarChar, 11, ParameterDirection.Input, entity.Phone)); commandParms.Add(SqlHelper.CreateParam("@Reason", SqlDbType.NVarChar, 200, ParameterDirection.Input, entity.Reason)); commandParms.Add(SqlHelper.CreateParam("@AddTime", SqlDbType.DateTime, 0, ParameterDirection.Input, entity.AddTime)); SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, C_SP_COLLECTINFO_UPDATE, commandParms); }
/// <summary> /// 向数据表中插入一条新记录 /// </summary> /// <param name="entity">Entity.CollectInfoEntity实体类</param> /// <remarks>如果表存在自增长字段,插入记录成功后自增长字段值会更新至实体</remarks> public static void Insert(Entity.CollectInfoEntity entity) { List <SqlParameter> commandParms = new List <SqlParameter>(); SqlParameter id_Id = SqlHelper.CreateParam("@Id", SqlDbType.Int, 0, ParameterDirection.Output, null); commandParms.Add(id_Id); commandParms.Add(SqlHelper.CreateParam("@StationId", SqlDbType.Int, 0, ParameterDirection.Input, entity.StationId)); commandParms.Add(SqlHelper.CreateParam("@CollectType", SqlDbType.Int, 0, ParameterDirection.Input, entity.CollectType)); commandParms.Add(SqlHelper.CreateParam("@UserName", SqlDbType.NVarChar, 50, ParameterDirection.Input, entity.UserName)); commandParms.Add(SqlHelper.CreateParam("@Phone", SqlDbType.VarChar, 11, ParameterDirection.Input, entity.Phone)); commandParms.Add(SqlHelper.CreateParam("@Reason", SqlDbType.NVarChar, 200, ParameterDirection.Input, entity.Reason)); commandParms.Add(SqlHelper.CreateParam("@AddTime", SqlDbType.DateTime, 0, ParameterDirection.Input, entity.AddTime)); SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, C_SP_COLLECTINFO_INSERT, commandParms); entity.Id = Convert.ToInt32(id_Id.Value); }
/// <summary> /// 获取数据库一条记录实体(根据主键条件) /// </summary> /// <param name="id">主键字段id</param> /// <returns>Entity.CollectInfoEntity实体类</returns> public static Entity.CollectInfoEntity GetById(int id) { Entity.CollectInfoEntity entity = null; List <SqlParameter> commandParms = new List <SqlParameter>(); commandParms.Add(SqlHelper.CreateParam("@Id", SqlDbType.Int, 0, ParameterDirection.Input, id)); using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, C_SP_COLLECTINFO_GET, commandParms)) { if (reader.Read()) { entity = ConvertToEntityFromDataReader(reader); } } return(entity); }