版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/funkstill/article/details/88200245
问题描述:
项目需要,对提取的数据以json格式插入到PostgreSQL数据库
解决方法:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Npgsql;
namespace learnGDAL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string pgisConnectString = "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=test_db";
NpgsqlConnection conn1 = new NpgsqlConnection(pgisConnectString);
conn1.Open();
using (NpgsqlCommand command1 = new NpgsqlCommand())
{
string ss = "PG:dbname='test_db' host='localhost' port='5432' user='postgres' password = 'postgres'";
OSGeo.OGR.Driver dr1 = Ogr.GetDriverByName("PostgreSQL");
DataSource ds1 = Ogr.Open(ss, 0);
string sql = "insert into test(ProvData) values('[{\"regionid\":\"110000\",\"name\":\"北京市\",\"number\":6331,\"area\":277898,\"materialRatio\":{\"彩色\":813,\"塑料\":273488,\"玻璃\":129,\"光伏\":0,\"连栋\":1309,\"其他\":2081,\"混凝土\":74},\"types\":{\"N\":6185,\"F\":83,\"KH\":63},\"areaRatio\":{\"N\":126000,\"F\":101800,\"KH\":50094},\"timeid\":\"201901\",\"changenum\":{\"KH\":{\"K\":3,\"N\":3,\"QC\":25,\"BC\":1},\"F\":{\"K\":3,\"N\":3,\"QC\":8,\"BC\":8}},\"changearea\":{\"KH\":{\"K\":4,\"N\":27,\"QC\":12,\"BC\":0.1},\"F\":{\"K\":23,\"N\":155,\"QC\":559,\"BC\":187}}}]');";
NpgsqlDataAdapter sda = new NpgsqlDataAdapter(sql, pgisConnectString);
}
}
catch
{
}
}
}
}