• ADO.NET操作PostgreSQL:数据库操作类(未封装)


    1.添加

            /// <summary>
            /// 添加
            /// </summary>
            /// <param name="newEntity"></param>
            /// <returns></returns>
            public static int Insert(Person newEntity)
            {
                string sql = @"insert into ""Person"" (""PersonId"",""IdCard"",""RealName"",""Gender"",""Address"") values (@PersonId,@IdCard,@RealName,@Gender,@Address)";
                using (NpgsqlConnection conn = new NpgsqlConnection(postgresqlconn.connectionString))
                {
                    using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
                    {
                        #region MyRegion
                        cmd.Parameters.Add("@PersonId", NpgsqlDbType.Integer).Value = newEntity.PersonId;
                        cmd.Parameters.Add("@IdCard", NpgsqlDbType.Varchar).Value = newEntity.IdCard ?? (object)DBNull.Value;
                        cmd.Parameters.Add("@RealName", NpgsqlDbType.Varchar).Value = newEntity.RealName ?? (object)DBNull.Value;
                        cmd.Parameters.Add("@Gender", NpgsqlDbType.Bit).Value = newEntity.Gender;
                        cmd.Parameters.Add("@Address", NpgsqlDbType.Varchar).Value = newEntity.Address ?? (object)DBNull.Value;
                        #endregion
    
                        conn.Open();
                        return cmd.ExecuteNonQuery();
                    }
                }
            }
    View Code

    2.修改

            /// <summary>
            /// 修改
            /// </summary>
            /// <param name="newEntity"></param>
            /// <returns></returns>
            public static int Update(Person newEntity)
            {
                string sql = @"update ""Person"" set ""IdCard""=@IdCard,""RealName""=@RealName,""Gender""=@Gender,""Address""=@Address where ""PersonId""=@PersonId";
                using (NpgsqlConnection conn = new NpgsqlConnection(postgresqlconn.connectionString))
                {
                    using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
                    {
                        #region MyRegion
                        cmd.Parameters.Add("@PersonId", NpgsqlDbType.Integer).Value = newEntity.PersonId;
                        cmd.Parameters.Add("@IdCard", NpgsqlDbType.Varchar).Value = newEntity.IdCard ?? (object)DBNull.Value;
                        cmd.Parameters.Add("@RealName", NpgsqlDbType.Varchar).Value = newEntity.RealName ?? (object)DBNull.Value;
                        cmd.Parameters.Add("@Gender", NpgsqlDbType.Bit).Value = newEntity.Gender;
                        cmd.Parameters.Add("@Address", NpgsqlDbType.Varchar).Value = newEntity.Address ?? (object)DBNull.Value;
                        #endregion
    
                        conn.Open();
                        return cmd.ExecuteNonQuery();
                    }
                }
            }
    View Code

    3.删除

            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="personId"></param>
            /// <returns></returns>
            public static int Delete(int personId)
            {
                string sql = @"delete from ""Person"" where  ""PersonId""=@PersonId";
                using (NpgsqlConnection conn = new NpgsqlConnection(postgresqlconn.connectionString))
                {
                    using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
                    {
                        cmd.Parameters.Add("@PersonId", NpgsqlDbType.Integer).Value = personId;
                        conn.Open();
                        return cmd.ExecuteNonQuery();
                    }
                }
            }
    View Code

    4.读取

            /// <summary>
            /// 读取
            /// </summary>
            /// <param name="reader"></param>
            /// <returns></returns>
            private static Person GetByReader(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;
            }
    View Code

    5.查询1行

            /// <summary>
            /// 查询1行
            /// </summary>
            /// <param name="personId"></param>
            /// <returns></returns>
            public static Person GetEntity(int personId)
            {
                Person entity = null;
                string sql = @"select * from ""Person"" where ""PersonId""=@PersonId";
                using (NpgsqlConnection conn = new NpgsqlConnection(postgresqlconn.connectionString))
                {
                    using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
                    {
                        cmd.Parameters.Add("@PersonId", NpgsqlDbType.Integer).Value = personId;
                        conn.Open();
                        using (NpgsqlDataReader sr = cmd.ExecuteReader())
                        {
                            if (sr != null)
                            {
                                while (sr.Read())
                                {
                                    entity = GetByReader(sr);
                                }
                            }
                        }
                    }
                }
                return entity;
            }
    View Code
  • 相关阅读:
    TVM量化代码解析
    如何在 GPU 上优化卷积
    全文翻译(全文合集):TVM: An Automated End-to-End Optimizing Compiler for Deep Learning
    全文翻译(四) TVM An Automated End-to-End Optimizing Compiler
    全文翻译(三) TVM An Automated End-to-End Optimizing Compiler
    全文翻译(二): TVM: An Automated End-to-End Optimizing Compiler for Deep Learning
    全文翻译(一):TVM: An Automated End-to-End Optimizing Compiler for Deep Learning
    pytorch使用gpu加速的方法
    springboot的实践1
    带你玩转stackOverflow-3
  • 原文地址:https://www.cnblogs.com/yuyuefly/p/9662074.html
Copyright © 2020-2023  润新知