在 .NET 核心中使用 dapper 批量插入到 PostgreSQL
Bulk insert into PostgreSQL using dapper in .NET core
我刚开始使用 Dapper,只是不知道如何将 json 文件中的属性列表插入到我的 postgres 数据库中。
所以我们开始我的对象 class:
public class UniqueIdField
{
public string name { get; set; }
public bool isSystemMaintained { get; set; }
}
public class SpatialReference
{
public int wkid { get; set; }
public int latestWkid { get; set; }
}
public class Field
{
public string name { get; set; }
public string type { get; set; }
public string alias { get; set; }
public string sqlType { get; set; }
public int length { get; set; }
public object domain { get; set; }
public object defaultValue { get; set; }
}
public class Attributes
{
public string GEN { get; set; }
public string BL { get; set; }
public string BL_ID { get; set; }
public string county { get; set; }
public string last_update { get; set; }
public double cases7_per_100k { get; set; }
public double cases7_bl_per_100k { get; set; }
public string cases7_per_100k_txt { get; set; }
public string BEZ { get; set; }
}
public class Feature
{
public Attributes attributes { get; set; }
}
public class Root
{
public string objectIdFieldName { get; set; }
public UniqueIdField uniqueIdField { get; set; }
public string globalIdFieldName { get; set; }
public string geometryType { get; set; }
public SpatialReference spatialReference { get; set; }
public List<Field> fields { get; set; }
public List<Feature> features { get; set; }
}
这是我插入数据的方法:
public async Task SeedCoronaInformationTable(Root CoronaData)
{
var constr = _configuration.GetConnectionString("PostgresConnection");
using var connection = new NpgsqlConnection(constr);
connection.Open();
var deleteStatement = @"DELETE FROM public.rki_corona_information";
var insertStatement = @"INSERT INTO public.rki_corona_information (id, landkreis, bl, bl_id, county, last_update, cases7_per_100k, cases7_bl_per_100k, cases7_per_100k_txt, BEZ)
Values (@id, @landkreis, @bl, @bl_id, @county, @last_update, @cases7_per_100k, @cases7_bl_per_100k, @cases7_per_100k_txt, @BEZ)";
connection.Query(deleteStatement);
int index = 1;
foreach (var feature in CoronaData.features)
{
var affectedRows = await connection.ExecuteAsync(insertStatement, new
{
id = index,
landkreis = feature.attributes.GEN,
bl = feature.attributes.BL,
bl_id = feature.attributes.BL_ID,
feature.attributes.county,
feature.attributes.last_update,
feature.attributes.cases7_per_100k,
feature.attributes.cases7_bl_per_100k,
feature.attributes.cases7_per_100k_txt,
feature.attributes.BEZ
}); ;
index++;
}
connection.Close();
}
后者有效,但是,我认为我不必自己循环遍历对象。我宁愿插入它并让 dapper 负责循环。我只是找不到如何传递属性列表的方法,因为它是 features
.
的嵌套元素
如何抓取属性并像 var affectedRows = await connection.ExecuteAsync(insertStatement, attributes)
一样插入它们?
没有支持的语法可以做到这一点。您所拥有的应该可以正常工作 - 请注意删除步骤 could/should 使用 Execute 而不是 Query。
您可以将要素数组序列化为 json 数组或复合数组,如下所述:Table valued Parameter Equivalent in Postgresql.
我使用 table 值参数在 SQL 服务器上实现了这一点。
根据您的记录有多大,您可能希望对请求进行批处理。
我刚开始使用 Dapper,只是不知道如何将 json 文件中的属性列表插入到我的 postgres 数据库中。
所以我们开始我的对象 class:
public class UniqueIdField
{
public string name { get; set; }
public bool isSystemMaintained { get; set; }
}
public class SpatialReference
{
public int wkid { get; set; }
public int latestWkid { get; set; }
}
public class Field
{
public string name { get; set; }
public string type { get; set; }
public string alias { get; set; }
public string sqlType { get; set; }
public int length { get; set; }
public object domain { get; set; }
public object defaultValue { get; set; }
}
public class Attributes
{
public string GEN { get; set; }
public string BL { get; set; }
public string BL_ID { get; set; }
public string county { get; set; }
public string last_update { get; set; }
public double cases7_per_100k { get; set; }
public double cases7_bl_per_100k { get; set; }
public string cases7_per_100k_txt { get; set; }
public string BEZ { get; set; }
}
public class Feature
{
public Attributes attributes { get; set; }
}
public class Root
{
public string objectIdFieldName { get; set; }
public UniqueIdField uniqueIdField { get; set; }
public string globalIdFieldName { get; set; }
public string geometryType { get; set; }
public SpatialReference spatialReference { get; set; }
public List<Field> fields { get; set; }
public List<Feature> features { get; set; }
}
这是我插入数据的方法:
public async Task SeedCoronaInformationTable(Root CoronaData)
{
var constr = _configuration.GetConnectionString("PostgresConnection");
using var connection = new NpgsqlConnection(constr);
connection.Open();
var deleteStatement = @"DELETE FROM public.rki_corona_information";
var insertStatement = @"INSERT INTO public.rki_corona_information (id, landkreis, bl, bl_id, county, last_update, cases7_per_100k, cases7_bl_per_100k, cases7_per_100k_txt, BEZ)
Values (@id, @landkreis, @bl, @bl_id, @county, @last_update, @cases7_per_100k, @cases7_bl_per_100k, @cases7_per_100k_txt, @BEZ)";
connection.Query(deleteStatement);
int index = 1;
foreach (var feature in CoronaData.features)
{
var affectedRows = await connection.ExecuteAsync(insertStatement, new
{
id = index,
landkreis = feature.attributes.GEN,
bl = feature.attributes.BL,
bl_id = feature.attributes.BL_ID,
feature.attributes.county,
feature.attributes.last_update,
feature.attributes.cases7_per_100k,
feature.attributes.cases7_bl_per_100k,
feature.attributes.cases7_per_100k_txt,
feature.attributes.BEZ
}); ;
index++;
}
connection.Close();
}
后者有效,但是,我认为我不必自己循环遍历对象。我宁愿插入它并让 dapper 负责循环。我只是找不到如何传递属性列表的方法,因为它是 features
.
如何抓取属性并像 var affectedRows = await connection.ExecuteAsync(insertStatement, attributes)
一样插入它们?
没有支持的语法可以做到这一点。您所拥有的应该可以正常工作 - 请注意删除步骤 could/should 使用 Execute 而不是 Query。
您可以将要素数组序列化为 json 数组或复合数组,如下所述:Table valued Parameter Equivalent in Postgresql.
我使用 table 值参数在 SQL 服务器上实现了这一点。 根据您的记录有多大,您可能希望对请求进行批处理。