Npgsql使用入門(三)【批量導入數據】

Program.cs代碼:

 class Program{static void Main(string[] args){var test = new PgBulkCopyHelper<SingleBuilding>("bld_amap_gzmain");foreach (string pName in test.PropNames){Console.WriteLine("name: {0},\t\ttype: {1}", pName, test.PropInfo[pName]);}//-----------------------------------------------------------------------------------------------//定義每次插入的最大數量限制int maxNum = 1; //100000;//初始化對應的數據表DataTable dataTable = test.InitDataTable();string connectionString = "Host=localhost;Username=king;Password=wu12345;Database=dellstore";List<List<SingleBuilding>> bldsList = new List<List<SingleBuilding>>();NpgsqlPolygon plg1 = new NpgsqlPolygon(10);plg1.Add(new NpgsqlPoint(0.0, 0.0));plg1.Add(new NpgsqlPoint(6.0, -1.0));plg1.Add(new NpgsqlPoint(5.0, 3.0));plg1.Add(new NpgsqlPoint(1.0, 2.0));NpgsqlPolygon plg2 = new NpgsqlPolygon(10);plg2.Add(new NpgsqlPoint(100.0, 10.0));plg2.Add(new NpgsqlPoint(40.0, 180.0));plg2.Add(new NpgsqlPoint(190.0, 60.0));plg2.Add(new NpgsqlPoint(10.0, 60.0));plg2.Add(new NpgsqlPoint(160.0, 180.0));List<SingleBuilding> sblist1 = new List<SingleBuilding>(){new SingleBuilding(){id=System.Guid.NewGuid(),tile_x=1,         tile_y=2,         bps_gc=plg1,bps_llc=plg2,cp_gc=new NpgsqlPoint(0,0),   cp_llc=new NpgsqlPoint(100,10),  name="測試文本1",bld_floor=111,     height=22           },new SingleBuilding(){id=System.Guid.NewGuid(),tile_x=1,         tile_y=2,         bps_gc=plg1,bps_llc=plg2,cp_gc=new NpgsqlPoint(0,0),   cp_llc=new NpgsqlPoint(100,10),  name="測試文本2",bld_floor=222,     height=444     }};bldsList.Add(sblist1);using (var conn = new NpgsqlConnection(connectionString)){conn.Open();foreach (List<SingleBuilding> blds in bldsList){if (blds != null && blds.Count > 0){//填充數據test.FillDataTable(blds, dataTable);}//判斷 dataTable 里面的數據量是否已經超過規定最大行數 maxNumif (dataTable.Rows.Count>maxNum){//如果是,則將 dataTable 里面的數據插入到數據庫中test.BulkInsert(conn, dataTable);//清空 dataTable 中的現有數據dataTable.Clear();}}}}}public class SingleBuilding{//創建數據表的SQL語句如下:/*CREATE TABLE bld_amap_gzmain (id uuid PRIMARY KEY NOT NULL,tile_x integer,             --x index of the map tile where the building is locatedtile_y integer,             --y index of the map tile where the building is locatedbps_gc polygon NOT NULL,    --the points of the bottom outline of the building, geodetic coordinatesbps_llc polygon NOT NULL,   --the points of the bottom outline of the building, Latitude and longitude coordinatescp_gc point NOT NULL,       --the center point of the building, geodetic coordinatescp_llc point NOT NULL,      --the center point of the building, Latitude and longitude coordinatesname text,bld_floor smallint,         --the number of floors of the buildingheight real                 --the height of building);*/public Guid id { get; set; }public int? tile_x { get; set; }public int? tile_y { get; set; }public NpgsqlPolygon bps_gc { get; set; }public NpgsqlPolygon bps_llc { get; set; }public NpgsqlPoint cp_gc { get; set; }public NpgsqlPoint cp_llc { get; set; }public string name { get; set; }public short? bld_floor { get; set; }public float? height { get; set; }}

PgBulkCopyHelper.cs代碼:

using Npgsql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Reflection;namespace PgBulkCopyHelper
{/// <summary>/// 用以快速將大批量數據插入到postgresql中/// </summary>/// <typeparam name="TEntity"></typeparam>public class PgBulkCopyHelper<TEntity>{/// <summary>/// TEntity的屬性信息/// Dictionary(string "property_name", Type property_type)/// </summary>public Dictionary<string, Type> PropInfo { get; set; }/// <summary>/// TEntity的屬性名稱列表/// </summary>public List<string> PropNames { get; set; }/// <summary>/// 數據表全名:schema.tableName or tableName/// </summary>public string FullTableName { get; set; }/// <summary>/// 構造函數/// </summary>/// <param name="schema">數據表的schema,一般為public</param>/// <param name="tableName">數據表的名稱</param>public PgBulkCopyHelper(string schema, string tableName){PropNames = new List<string>();PropInfo = new Dictionary<string, Type>();PropertyInfo[] typeArgs = GetPropertyFromTEntity();foreach (PropertyInfo tParam in typeArgs){PropNames.Add(tParam.Name);PropInfo[tParam.Name] = tParam.PropertyType;}if (!string.IsNullOrWhiteSpace(tableName)){if (string.IsNullOrWhiteSpace(schema)){FullTableName = tableName;}elseFullTableName = string.Format("{0}.{1}", schema, tableName);}}/// <summary>/// 構造函數/// </summary>/// <param name="tableName">數據表的名稱</param>public PgBulkCopyHelper(string tableName):this(null, tableName){ }/// <summary>/// 獲取TEntity的屬性信息/// </summary>/// <returns>TEntity的屬性信息的列表</returns>private PropertyInfo[] GetPropertyFromTEntity(){Type t = typeof(TEntity);PropertyInfo[] typeArgs = t.GetProperties();return typeArgs;}/// <summary>/// 根據TEntity的屬性信息構造對應數據表/// </summary>/// <returns>只有字段信息的數據表</returns>public DataTable InitDataTable(){DataTable dataTable = new DataTable();foreach(PropertyInfo tParam in GetPropertyFromTEntity()){Type propType = tParam.PropertyType;//由于 DataSet 不支持 System.Nullable<> 類型,因此要先做判斷if ((propType.IsGenericType) && (propType.GetGenericTypeDefinition() == typeof(Nullable<>)))propType = propType.GetGenericArguments()[0];dataTable.Columns.Add(tParam.Name, propType);}return dataTable;}/// <summary>/// 根據TEntity可枚舉列表填充給定的數據表/// </summary>/// <param name="entities">TEntity類型的可枚舉列表</param>/// <param name="dataTable">數據表</param>public void FillDataTable(IEnumerable<TEntity> entities, DataTable dataTable){if (entities != null && entities.Count() > 0){foreach (TEntity entity in entities){FillDataTable(entity, dataTable);}}}/// <summary>/// 在DataTable中插入單條數據/// </summary>/// <param name="entity">具體數據</param>/// <param name="dataTable">數據表</param>public void FillDataTable(TEntity entity, DataTable dataTable){var dataRow = dataTable.NewRow();int colNum = dataTable.Columns.Count;PropertyInfo[] typeArgs = GetPropertyFromTEntity();for (int i = 0; i < colNum; i++){dataRow[i] = typeArgs[i].GetValue(entity);}dataTable.Rows.Add(dataRow);}/// <summary>/// 通過PostgreSQL連接把dataTable中的數據整塊填充到數據庫對應的數據表中/// 注意,該函數不負責NpgsqlConnection的創建、打開以及關閉/// </summary>/// <param name="conn">PostgreSQL連接</param>/// <param name="dataTable">數據表</param>public void BulkInsert(NpgsqlConnection conn, DataTable dataTable){var commandFormat = string.Format(CultureInfo.InvariantCulture, "COPY {0} FROM STDIN BINARY", FullTableName);using (var writer = conn.BeginBinaryImport(commandFormat)){foreach (DataRow item in dataTable.Rows)writer.WriteRow(item.ItemArray);}}}
}

運行結果如圖:

這里寫圖片描述


這里寫圖片描述

轉載于:https://www.cnblogs.com/Wulex/p/6953527.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/455510.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/455510.shtml
英文地址,請注明出處:http://en.pswp.cn/news/455510.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

遠程網絡視頻監視技術

目前要實現廣域網視頻監視&#xff0c;主要通過三種方式實現&#xff1a;1.硬盤錄像機&#xff1b;2.網絡視頻服務器&#xff1b;3.網絡攝像機。 硬盤錄像機是一個以錄像為主的設備&#xff0c;有的可以支持IE瀏覽。網絡視頻服務器一般前端不錄像&#xff0c;直接將影像傳輸到…

Deepin下java開發環境部署

簡介 本經驗由深度論壇用戶(zhang12345shun)分享&#xff0c;原文地址 正文 SUN JDK&#xff08;現已改名Oracle JDK&#xff09; 1.下載Sun版JDK壓縮包&#xff08;.tar.gz&#xff09;&#xff0c;選擇其中的32/64位Linux版本。 2.將其解壓縮&#xff1a; sudo tar -zx…

判斷ic卡類型

傳15讀卡片數據 判斷data8的status是否為0&#xff0c;是則是ic 否則就是磁條卡 根據二磁道數據識別是IC卡還是磁條卡 根據二磁道符號后面第一位識別。規則是 該數字 6 或者 2 則該卡是IC卡&#xff0c;否則是普通磁條卡 轉載于:https://www.cnblogs.com/wubenhui/p/6956256.h…

python常用代碼_Python常用算法學習(3)(原理+代碼)——最全總結

1&#xff0c;什么是算法的時間和空間復雜度算法(Algorithm)是指用來操作數據&#xff0c;解決程序問題的一組方法&#xff0c;對于同一個問題&#xff0c;使用不同的算法&#xff0c;也許最終得到的結果是一樣的&#xff0c;但是在過程中消耗的資源和時間卻會有很大的區別。那…

數據監測驅動下的信息流廣告優化

信息流廣告是什么 “今日頭條和百度必有一戰”&#xff0c;相信不少的互聯網人在過去幾個月都聽到過類似的斷言。定位于信息分發平臺的今日頭條和主營搜索業務的百度會產生如此大的利益沖突&#xff0c;最核心的點其實就是信息流廣告。 信息流廣告指的是在用戶使用互聯網產品或…

在idea中使用git管理你的項目

起步 idea是十分智能的Java集成開發環境 而我們在用idea寫項目的時候經常遇到版本控制的問題,而git工具如果你只會在終端中的git命令來進行控制,可能會使得效率低下 今天小編就教大家在idea中使用git來管理你的項目 首先創建一個項目 點擊create new projects 這里選擇默認…

react-native熱更新插件react-native-code-push

使用react-native-code-push插件來實現熱更新的時候&#xff0c;會遇到一些問題。下面這個問題就讓我差點崩潰了。 在測試 Production 和 Staging 是否會去檢查各自環境下的bundle文件&#xff0c;我就遇到了混亂的問題。 有時候用 Release 打包出來的app會去檢查 Staging 下的…

《工業控制網絡安全技術與實踐》一一第3章 工業控制網絡安全威脅

第3章 工業控制網絡安全威脅 第2章介紹了工業控制系統的相關知識。本章主要介紹工業控制網絡的基本知識&#xff0c;并詳細介紹工業控制網絡的常見安全威脅。之后&#xff0c;分析工業控制系統的脆弱性。

多媒體視頻知識入門貼zt(二)

2.2 音視頻基本概念介紹 2.2.1 視頻的基本概念 RGB和YUV RGB指的是紅綠藍&#xff0c;應用還是很廣泛的&#xff0c;比如顯示器顯示&#xff0c;BMP文件格式中的像素值等&#xff1b;而YUV主要指亮度和兩個色差信號&#xff0c;被稱為luminance和chrominance他們的轉化關系可以…

Java筆記01-數組相關

數組相關 數組的創建 數據類型[] 數組名稱 new 數據類型[數組的長度]其中數據類型可以為任意類型 數組的訪問 ? 調用數組的length屬性可以獲取數組的長度&#xff1a; int len arr.length;? 可以通過下標的方式訪問數組中的每一個元素。 需要注意的是&#xff1a;數組…

偏好設置

轉載于:https://www.cnblogs.com/xufengyuan/p/6959424.html

keyshot環境素材文件_快速學會keyshot基礎渲染的步驟

KeyShot是基于CPU為三維數據進行渲染和動畫操作的獨立渲染器。意為“The Key to Amazing Shots”&#xff0c;是一個互動性的光線追蹤與全域光渲染程序&#xff0c;無需復雜的設定即可產生相片般真實的 3D 渲染影像。KeyShot超強的渲染能力廣泛的應用于工業產品、機械工程、CG行…

傳統數據中心如何實現向云的平滑升級

1.引言 眾所周知&#xff0c;云計算是近年來發展最快的互聯網技術&#xff0c;被稱為第四次IT革命。據權威機構預測&#xff0c;到2016年&#xff0c;2/3的IT應用服務將建立在云架構上 [1]。作為云計算核心的基礎承載設施&#xff0c;數據中心在網絡中所扮演的角色也愈加重要。…

UGUI滾動列表ScrollView使用注意點

ScrollView的Viewport不能引用其子節點Grid&#xff0c;不然會導致ScrollView滾到頭時還能繼續滾動&#xff0c;無法回滾 轉載于:https://www.cnblogs.com/lovesharing/p/6963062.html

Java筆記02-OOP

面向對象編程 萬物皆對象 面向對象指以屬性和行為的觀點去分析現實生活中的事物 面向對象編程指先以面向對象的思想進行分析,然后使用面向對象的編程語言進行表達的過程 面向對象編程是軟件產業化發展的需求 理解面向對象的思想精髓才行 面想對象的三大特性 封裝 繼承 …

上位機與基恩士plc以太網通訊_2020湛江AB羅克韋爾PLC主機回收二手或全新

2020湛江AB羅克韋爾PLC主機回收二手或全新專業回收基恩士光電傳感器回收&#xff0c;基恩士安全光柵回收&#xff0c;基恩士對射開關回收&#xff0c;基恩士工控配件回收&#xff0c;基恩士視覺相機回收&#xff0c;發那科伺服驅動電機回收&#xff0c;發那科控制系統回收&…

經常使用的 WEB server

對于剛開始學習的人來說&#xff0c;或許自己能夠寫出一些簡單的演示樣例DEMO&#xff0c;但卻糾結于不知道應該怎樣才干訪問自己的頁面&#xff0c;這里進行了一些簡單的WEBserver總結。便于新朋友配置使用 靜態HTML頁面 對于靜態HTML頁面不須要webserver&#xff0c;直接右鍵…

多媒體視頻知識入門貼zt(一)

一 基礎篇 1.1 圖形、圖像和視頻圖形&#xff08;graphic&#xff09;&#xff1a;和圖像與視頻不同&#xff0c;有一種說法是圖形就是自然界的客觀世界不存在的圖案。對于計算機中的圖形研究&#xff0c;有專門的計算機圖形學&#xff0c;主要的 研究對象是點、線、面等抽象事…

Java筆記03-Constructor Override

Java筆記03-Constructor & Override 構造方法基本概念 構造方法是類中的一種特殊方法 它是在類創建對象(實例化)的時候自動調用的方法 這個和python中的__init__初始化魔術方法類似 可以在創建對象的時候進行參數的傳遞 默認送您的構造 一個類編譯過后都需要有一個構…

生物信息學概論_大學專業詳解系列83——生物信息學(理學學士)

生物信息學(理學學士)畢業生應具備的知識和能力(1)掌握扎實的數學、物理、化學基礎理論和基本知識&#xff1b;(2)掌握生物學專業基礎知識和信息處理的專門知識&#xff1b;(3)掌握普通生物學、細胞生物學、遺傳學、分子生物學、生物數據庫管理系統、生物信息學、基因組學、蛋白…