(精华)2020年6月27日 C#类库 PostgreSqlHelper(Ado.net数据库封装)

news/2024/7/9 21:20:06 标签: GIS, postgis, postgresql

本文转载自《https://blog.csdn.net/aa2528877987/article/details/106980804》

using EFCore.Sharding;
using Npgsql;
using NpgsqlTypes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Net;
using System.Net.NetworkInformation;

namespace Core.Util
{
    /// <summary>
    /// PostgreSql数据库操作帮助类
    /// </summary>
    public class PostgreSqlHelper : DbHelper
    {
        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="conString">完整连接字符串</param>
        public PostgreSqlHelper(string conString)
            : base(DatabaseType.PostgreSql, conString)
        {

        }

        #endregion

        #region 私有成员

        protected override Dictionary<string, Type> DbTypeDic { get; } = new Dictionary<string, Type>()
        {
            { "bool", typeof(bool) },
            { "int4", typeof(int) },
            { "int8", typeof(long) },
            { "float4", typeof(float) },
            { "float8", typeof(decimal) },
            { "numeric", typeof(decimal) },
            { "money", typeof(decimal) },
            { "text", typeof(string) },
            { "varchar", typeof(string) },
            { "bpchar", typeof(string) },
            { "citext", typeof(string) },
            { "json", typeof(string) },
            { "jsonb", typeof(string) },
            { "xml", typeof(string) },
            //{ "point", typeof(NpgsqlPoint) },
            //{ "lseg", typeof(NpgsqlLSeg) },
            //{ "path", typeof(NpgsqlPath) },
            //{ "polygon", typeof(NpgsqlPolygon) },
            //{ "line", typeof(NpgsqlLine) },
            //{ "circle", typeof(NpgsqlCircle) },
            //{ "box", typeof(NpgsqlBox) },
            { "bit(1)", typeof(bool) },
            { "bit(n)", typeof(BitArray) },
            { "varbit", typeof(BitArray) },
            { "hstore", typeof(IDictionary) },
            { "uuid", typeof(Guid) },
            { "cidr", typeof(ValueTuple<IPAddress,int>) },
            { "inet", typeof(IPAddress) },
            { "macaddr", typeof(PhysicalAddress) },
            { "tsquery", typeof(NpgsqlTsQuery) },
            { "tsvector", typeof(NpgsqlTsVector) },
            { "date", typeof(DateTime) },
            { "interval", typeof(TimeSpan) },
            { "timestamp", typeof(DateTime) },
            { "timestamptz", typeof(DateTime) },
            { "time", typeof(TimeSpan) },
            { "timetz", typeof(DateTimeOffset) },
            { "bytea", typeof(byte[]) },
            { "oid", typeof(uint) },
            { "xid", typeof(uint) },
            { "cid", typeof(uint) },
            { "oidvector", typeof(uint[]) },
            { "name", typeof(string) },
            { "(internal) char", typeof(char) },
            //{ "geometry (PostGIS)", typeof(PostgisGeometry) },
            { "record", typeof(object[]) },
            { "composite types", typeof(object) },
            { "range subtypes", typeof(object) },
            { "enum types", typeof(Enum) },
            { "array types", typeof(Array) },
        };

        #endregion

        #region 外部接口

        /// <summary>
        /// 获取数据库中的所有表
        /// </summary>
        /// <param name="schemaName">模式(架构)</param>
        /// <returns></returns>
        public override List<DbTableInfo> GetDbAllTables(string schemaName=null)
        {
            if (schemaName.IsNullOrEmpty())
                schemaName = "public";
            string sql = @"(select 
	relname as ""TableName"",
	cast(obj_description(relfilenode,'pg_class') as varchar) as ""Description""
from pg_class c 
where  relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%' and relchecks=0
order by relname)

UNION ALL

(SELECT viewname as ""TableName"",NULL as ""Description""
FROM pg_views
WHERE schemaname = @schemaName)";
            return GetListBySql<DbTableInfo>(sql, new List<DbParameter> { new NpgsqlParameter("@schemaName", schemaName) });
        }

        /// <summary>
        /// 通过连接字符串和表名获取数据库表的信息
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public override List<TableInfo> GetDbTableInfo(string tableName)
        {
            string sql = @"SELECT 
	a.attname as ""Name"",

    pg_type.typname as ""Type"",
(SELECT ""count""(*) from
(SELECT
ic.column_name as ""ColumnName""
FROM
information_schema.table_constraints tc
JOIN information_schema.constraint_column_usage AS ccu USING(constraint_schema, constraint_name)
JOIN information_schema.columns AS ic ON ic.table_schema = tc.constraint_schema AND tc.table_name = ic.table_name AND ccu.column_name = ic.column_name
where constraint_type = 'PRIMARY KEY' and tc.""table_name"" = @table_name) KeyA WHERE KeyA.""ColumnName"" = a.attname)> 0 as ""IsKey"",
a.attnotnull<> True as ""IsNullable"",
	col_description(a.attrelid, a.attnum) as ""Description""


FROM pg_class as c,pg_attribute as a inner join pg_type on pg_type.oid = a.atttypid
where c.relname = @table_name and a.attrelid = c.oid and a.attnum > 0;";
            return GetListBySql<TableInfo>(sql, new List<DbParameter> { new NpgsqlParameter("@table_name", tableName) });
        }

        /// <summary>
        /// 生成实体文件
        /// </summary>
        /// <param name="infos">表字段信息</param>
        /// <param name="tableName">表名</param>
        /// <param name="tableDescription">表描述信息</param>
        /// <param name="filePath">文件路径(包含文件名)</param>
        /// <param name="nameSpace">实体命名空间</param>
        /// <param name="schemaName">架构(模式)名</param>
        public override void SaveEntityToFile(List<TableInfo> infos, string tableName, string tableDescription, string filePath, string nameSpace, string schemaName = "public")
        {
            base.SaveEntityToFile(infos, tableName, tableDescription, filePath, nameSpace, schemaName);
        }

        #endregion
    }
}


http://www.niftyadmin.cn/n/1372049.html

相关文章

利用ArcGIS Python批量拼接裁剪遥感影像(arcpy batch processing)

本文转载在《https://blog.csdn.net/gisboygogogo/article/details/75195760》 本篇文章将说明如何利用ArcGIS 10.1自带的Python IDLE进行遥感影像的批量拼接与裁剪。 1.运行环境&#xff1a;ArcGIS10.1 (安装传送门)、Python IDLE 2.数据来源&#xff1a;地理空间数据云 GDE…

GIS中的矢量数据、栅格数据

GIS中的数据分为&#xff1a; 1.矢量数据 2.栅格数据 参考【Types of GIS Data Explored: Vector and Raster】 GIS数据与普通数据的最主要差别是包含了空间位置信息数据。 GIS数据内部可分为两类&#xff1a;以矢量和栅格形式&#xff08;包括图像&#xff09;表示的空间数据…

C#-Word内容全部显示在RichTextBox中

引用 using Microsoft.Office.Interop.Word;实现 /// ///filePath :要显示的word路径 ///rtb:要显示的目标RichTextBox 控件 /// public void ReadWordFile(string filePath, RichTextBox rtb){Microsoft.Office.Interop.Word.Application wordObject new Microsoft.Office.…

一个垂直的步骤组件

控件非完全自主实现&#xff0c;基于网络上水平的步骤组件开发。最终效果如下&#xff1a; 每个步骤可以点击&#xff0c;点击后触发事件&#xff0c;后续步骤的图标变为灰色。 核心源代码如下&#xff1a; using System; using System.Collections.Generic; using System.C…

管线中的净距分析

完整系统源码地址&#xff1a;https://gitee.com/rzcgis/pipeline 管网系统中涉及到的净距分析&#xff0c;计算两条管线的最小水平、垂直净距。 用鼠标左键在图形窗口点击选择两条管线&#xff0c;系统将自动计算水平距离或垂直距离。 选择两条管线&#xff0c;计算出两条关系…

通过QGIS将带有高程的点数据转换为DEM

Shapefile格式的点数据带有高程字段Height&#xff0c;将此数据转换为DEM。 数据如下&#xff1a; 在QGIS的Processing ToolBox中新建搜索“IDW”: 打开【IDW interpolation】工具并填入参数&#xff1a; 注意&#xff1a;Output raster size只需要填入Rows参数即可,相关参…

C#中获取枚举值的介绍

C#中获取枚举值的介绍 public static string GetEnumDescription(Enum enumValue){string value enumValue.ToString();FieldInfo field enumValue.GetType().GetField(value);object[] objs field.GetCustomAttributes(typeof(DescriptionAttribute), false); //获取描…

C#克隆任意对象

/// <summary>/// 类的扩展属性/// </summary>public class CloneExtended : ICloneable{public static object Clone(object obj){BinaryFormatter Formatter new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));MemoryStream stre…