1.添加
/// <summary> /// Insert /// </summary> /// <param name="entity"></param> /// <returns></returns> public static int Insert(Person entity) { string sql = @"insert into ""Person"" (""IdCard"",""RealName"",""Gender"",""Address"") values (@IdCard,@RealName,@Gender,@Address)"; NpgsqlParameter[] cmdParams = { new NpgsqlParameter("@IdCard", NpgsqlDbType.Varchar) { Value = entity.IdCard ?? (object)DBNull.Value }, new NpgsqlParameter("@RealName", NpgsqlDbType.Varchar) { Value = entity.RealName ?? (object)DBNull.Value }, new NpgsqlParameter("@Gender", NpgsqlDbType.Bit) { Value = entity.Gender }, new NpgsqlParameter("@Address", NpgsqlDbType.Varchar) { Value = entity.Address ?? (object)DBNull.Value }, }; return PostgresqlBaseDal.ExecuteNonQuery(sql, cmdParams); }
2.修改
/// <summary> /// Update /// </summary> /// <param name="entity"></param> /// <returns></returns> public static int Update(Person entity) { string sql = @"update ""Person"" set ""IdCard""[email protected],""RealName""[email protected],""Gender""[email protected],""Address""[email protected] where ""PersonId""[email protected]"; NpgsqlParameter[] cmdParams = { new NpgsqlParameter("@PersonId", NpgsqlDbType.Integer) { Value = entity.PersonId }, new NpgsqlParameter("@IdCard", NpgsqlDbType.Varchar) { Value = entity.IdCard ?? (object)DBNull.Value }, new NpgsqlParameter("@RealName", NpgsqlDbType.Varchar) { Value = entity.RealName ?? (object)DBNull.Value }, new NpgsqlParameter("@Gender", NpgsqlDbType.Bit) { Value = entity.Gender }, new NpgsqlParameter("@Address", NpgsqlDbType.Varchar) { Value = entity.Address ?? (object)DBNull.Value }, }; return PostgresqlBaseDal.ExecuteNonQuery(sql, cmdParams); }
3.删除
/// <summary> /// Delete /// </summary> /// <param name="personId"></param> /// <returns></returns> public static int Delete(int personId) { string sql = @"delete from ""Person"" where ""PersonId""[email protected]"; NpgsqlParameter[] cmdParams = { new NpgsqlParameter("@PersonId", NpgsqlDbType.Integer) { Value = personId }, }; return PostgresqlBaseDal.ExecuteNonQuery(sql, cmdParams); }
4.读取
/// <summary> /// Reader /// </summary> /// <param name="reader"></param> /// <returns></returns> private static Person Reader(NpgsqlDataReader reader) { Person newEntity = new Person(); if (reader != null && !reader.IsClosed) { if (reader["PersonId"] != DBNull.Value) newEntity.PersonId = (int)reader["PersonId"]; if (reader["IdCard"] != DBNull.Value) newEntity.IdCard = (string)reader["IdCard"]; if (reader["RealName"] != DBNull.Value) newEntity.RealName = (string)reader["RealName"]; if (reader["Gender"] != DBNull.Value) newEntity.Gender = (bool)reader["Gender"]; if (reader["Address"] != DBNull.Value) newEntity.Address = (string)reader["Address"]; } return newEntity; }
5.查询1行
/// <summary> /// GetEntity /// </summary> /// <param name="personId"></param> /// <returns></returns> public static Person GetEntity(int personId) { string sql = @"select * from ""Person"" where ""PersonId""[email protected]"; NpgsqlParameter[] cmdParams = { new NpgsqlParameter("@PersonId", NpgsqlDbType.Integer) { Value = personId }, }; return PostgresqlBaseDal.ExecuteReader(Reader, sql, cmdParams); }
原文地址:https://www.cnblogs.com/yuyuefly/p/9662120.html
时间: 2024-10-12 02:39:15