使用PostgreSQLCopyHelper进行记录的XML大容量导入-Npgsql.PostgresException:42601
在将包含两条记录的XML文件中的记录导入数据库表时,我遇到了一个问题,第一条记录被插入了两次,而第二条记录没有保存到表中使用PostgreSQLCopyHelper进行记录的XML大容量导入-Npgsql.PostgresException:42601,xml,postgresql,import,Xml,Postgresql,Import,在将包含两条记录的XML文件中的记录导入数据库表时,我遇到了一个问题,第一条记录被插入了两次,而第二条记录没有保存到表中 insert = insert ?? new PostgreSQLCopyHelper<ProductViewModel>("public", "q_product") .MapUUID("q_guid", x => Guid.NewGuid()) .MapText("q_description", x => node.SelectSing
insert = insert ?? new PostgreSQLCopyHelper<ProductViewModel>("public", "q_product")
.MapUUID("q_guid", x => Guid.NewGuid())
.MapText("q_description", x => node.SelectSingleNode("ProductID/LongDescription").InnerText)
.MapText("q_barcode", x => node.SelectSingleNode("Barcode/Eancode").InnerText)
etc........
;
entities.Add(p);
这是包含两个记录/产品的XML文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<ProdExtract>
<ExtractHeader>
<Schema>
<id>Accord ePOS Product Extract</id>
<Version>1.00</Version>
</Schema>
</ExtractHeader>
<Product>
<Mode>Modify</Mode>
<ProductID>
<LongDescription>ZOLMANS MINT SAUCE #</LongDescription>
</ProductID>
<BasicFields>
<Units>6</Units>
<VAT VatCode="A" VatRate="0" />
</BasicFields>
<Price>
<CurrentWSP>9.55</CurrentWSP>
<CurrentRSP>2.01</CurrentRSP>
</Price>
<Barcode>
<Eancode>5000147032921</Eancode>
</Barcode>
</Product>
<Product>
<Mode>Modify</Mode>
<ProductID>
<LongDescription>TEST XML IMPORT PRODUCT </LongDescription>
</ProductID>
<BasicFields>
<Units>10</Units>
<VAT VatCode="A" VatRate="0" />
</BasicFields>
<Price>
<CurrentWSP>8.88</CurrentWSP>
<CurrentRSP>2.22</CurrentRSP>
</Price>
<Barcode>
<Eancode>5000147032923</Eancode>
</Barcode>
</Product>
</ProdExtract>
更新解决方案
删除线路
插入=新的PostgreSQLCopyHelperpublic,q_产品
使用下面的方法,可以满足我当前的需要。我知道可能有比这个解决方案更快的方法,但这适合我目前的需要
NpgsqlCommand cmd = new NpgsqlCommand
("INSERT INTO q_product (q_guid, q_description, q_barcode, q_casesize, q_sellprice, q_casecost, q_import_vatcode)" +
"VALUES (@pk, @des, @bcode, @csize, @sprice, @ccost, @vcode )",
con);
cmd.Parameters.AddWithValue("@pk", p.q_guid);
cmd.Parameters.AddWithValue("@des", p.q_description);
cmd.Parameters.AddWithValue("@bcode", p.q_barcode);
cmd.Parameters.AddWithValue("@csize", p.q_casesize);
cmd.Parameters.AddWithValue("@sprice", p.q_sellprice);
cmd.Parameters.AddWithValue("@ccost", p.q_casecost);
cmd.Parameters.AddWithValue("@vcode", p.q_import_vatcode);
cmd.ExecuteNonQuery();
您的问题是重复添加相同的元素:
entities.Add(p);
在循环的每次迭代中,您需要创建一个新对象并设置其属性:
p = new WhateverYourTypeIs();
p.Property1 = node.Something;
p.Property2 = node.SomethingElse;
entities.Add(p);
等等。q_产品中的列名是什么?我还怀疑您需要一些映射代码-类似于中的代码。我在q_产品中的列是q_guid、q_description、,q_条形码等。当我遇到填充p@mjwills的问题时,我使用的映射代码与之前的链接类似。我更新了post bottom,其中一种类型的mapping@mjwillis在我决定将insert语句与sqlcommand一起使用,而不是大容量insert之后,您的解决方案对我很有效。它适合我当前的需要,但仍将研究如何使其像以前的fised width文件解决方案一样工作。谢谢
entities.Add(p);
p = new WhateverYourTypeIs();
p.Property1 = node.Something;
p.Property2 = node.SomethingElse;
entities.Add(p);