Npgsql使用入门(三)【批量导入数据】
生活随笔
收集整理的這篇文章主要介紹了
Npgsql使用入门(三)【批量导入数据】
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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]);}//-----------------------------------------------------------------------------------------------//定義每次插入的最大數(shù)量限制int maxNum = 1; //100000;//初始化對(duì)應(yīng)的數(shù)據(jù)表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="測(cè)試文本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="測(cè)試文本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){//填充數(shù)據(jù)test.FillDataTable(blds, dataTable);}//判斷 dataTable 里面的數(shù)據(jù)量是否已經(jīng)超過(guò)規(guī)定最大行數(shù) maxNumif (dataTable.Rows.Count>maxNum){//如果是,則將 dataTable 里面的數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中test.BulkInsert(conn, dataTable);//清空 dataTable 中的現(xiàn)有數(shù)據(jù)dataTable.Clear();}}}}}public class SingleBuilding{//創(chuàng)建數(shù)據(jù)表的SQL語(yǔ)句如下:/*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>/// 用以快速將大批量數(shù)據(jù)插入到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的屬性名稱(chēng)列表/// </summary>public List<string> PropNames { get; set; }/// <summary>/// 數(shù)據(jù)表全名:schema.tableName or tableName/// </summary>public string FullTableName { get; set; }/// <summary>/// 構(gòu)造函數(shù)/// </summary>/// <param name="schema">數(shù)據(jù)表的schema,一般為public</param>/// <param name="tableName">數(shù)據(jù)表的名稱(chēng)</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>/// 構(gòu)造函數(shù)/// </summary>/// <param name="tableName">數(shù)據(jù)表的名稱(chēng)</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>/// 根據(jù)TEntity的屬性信息構(gòu)造對(duì)應(yīng)數(shù)據(jù)表/// </summary>/// <returns>只有字段信息的數(shù)據(jù)表</returns>public DataTable InitDataTable(){DataTable dataTable = new DataTable();foreach(PropertyInfo tParam in GetPropertyFromTEntity()){Type propType = tParam.PropertyType;//由于 DataSet 不支持 System.Nullable<> 類(lèi)型,因此要先做判斷if ((propType.IsGenericType) && (propType.GetGenericTypeDefinition() == typeof(Nullable<>)))propType = propType.GetGenericArguments()[0];dataTable.Columns.Add(tParam.Name, propType);}return dataTable;}/// <summary>/// 根據(jù)TEntity可枚舉列表填充給定的數(shù)據(jù)表/// </summary>/// <param name="entities">TEntity類(lèi)型的可枚舉列表</param>/// <param name="dataTable">數(shù)據(jù)表</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中插入單條數(shù)據(jù)/// </summary>/// <param name="entity">具體數(shù)據(jù)</param>/// <param name="dataTable">數(shù)據(jù)表</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>/// 通過(guò)PostgreSQL連接把dataTable中的數(shù)據(jù)整塊填充到數(shù)據(jù)庫(kù)對(duì)應(yīng)的數(shù)據(jù)表中/// 注意,該函數(shù)不負(fù)責(zé)NpgsqlConnection的創(chuàng)建、打開(kāi)以及關(guān)閉/// </summary>/// <param name="conn">PostgreSQL連接</param>/// <param name="dataTable">數(shù)據(jù)表</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);}}} }運(yùn)行結(jié)果如圖:
轉(zhuǎn)載于:https://www.cnblogs.com/Wulex/p/6953527.html
總結(jié)
以上是生活随笔為你收集整理的Npgsql使用入门(三)【批量导入数据】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: JavaScript实现输入验证(简单的
- 下一篇: 结对项目的感受