我在将数据从 xml 导入 SQL Server 表时遇到问题。
我的 XML 文件的这个例子:
<ORDER>
<ORDER_HEADER>
<NUMBER>109</NUMBER>
</ORDER_HEADER>
<CUSTOMER_HEADER>
<CUSTOMER>Michael</CUSTOMER>
</CUSTOMER_HEADER>
<ORDER_ITEMS>
<ITEM>
<CATALOG_NUMBER>2</CATALOG_NUMBER>
<VAT>21</VAT>
</ITEM>
<ITEM>
<CATALOG_NUMBER>5</CATALOG_NUMBER>
<VAT>21</VAT>
</ITEM>
<ITEM>
<CATALOG_NUMBER>7</CATALOG_NUMBER>
<VAT>21</VAT>
</ITEM>
<ITEM>
<CATALOG_NUMBER>9</CATALOG_NUMBER>
<VAT>21</VAT>
</ITEM>
</ORDER_ITEMS>
</ORDER>
这是我的 C# 代码:
XDocument doc = XDocument.Load("C:\\Users\\L\\order.xml");
var NUMBER = doc.Descendants("NUMBER");
var CUSTOMER = doc.Descendants("CUSTOMER");
var CATALOG_NUMBER = doc.Descendants("CATALOG_NUMBER");
var VAT = doc.Descendants("VAT");
SqlConnection conn = new SqlConnection("*****");
conn.Open();
foreach (var cislo in NUMBER)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText="Insert INTO ORDER_HEADER(NUMBER) VALUES (@cislo);";
cmd.Parameters.AddWithValue("@cislo",cislo.Value);
cmd.ExecuteNonQuery();
cmd.Clone();
}
}
foreach (var zakaznik in CUSTOMER)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText="Insert INTO CUSTOMER_HEAD(CUSTOMER)VALUES(@zakaznik);";
cmd.Parameters.AddWithValue("@zakaznik", zakaznik.Value);
cmd.ExecuteNonQuery();
cmd.Clone();
}
}
foreach (var katalo_cislo in CATALOG_NUMBER)
foreach (var vat1 in VAT)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Insert INTO ITEM (CATALOG_NUMBER,VAT) VALUES (@katalo_cislo,@vat1);";
cmd.Parameters.AddWithValue("@katalo_cislo", katalo_cislo.Value);
cmd.Parameters.AddWithValue("@vat1", vat1.Value);
cmd.ExecuteNonQuery();
cmd.Clone();
}
}
conn.Close();
但是前 2 个 sql 表是可以的,但是在最后一个 sql 表中,ITEM 超过 4 条记录...问题可能在两个 FOREACH 中...但是我怎么能从ORDER_ITEMS
右边加载记录?
谢谢
这是我现在的所有价值观的代码:¨
var Items = doc.Descendants("ITEM").Select(x => new { NUMBER = (int?)x.Element("NUMBER"), CATALOG_NUMBER = (string)x.Element("CATALOG_NUMBER"), ITEM_NAME = (string)x.Element("ITEM_NAME"), UNIT = (string)x.Element("UNIT"), AMOUNT = (int?)x.Element("AMOUNT"), PRICE_WITHOUT_VAT = (string)x.Element("PRICE_WITHOUT_VAT"), VAT = (string)x.Element("VAT"), PRICE_VAT = (string)x.Element("PRICE_VAT"), EAN = (string)x.Element("EAN"), SUPPLIER_ITEM_NUMBER = (string)x.Element("SUPPLIER_ITEM_NUMBER"), SUPPLIER_ID = (string)x.Element("SUPPLIER_ID"), SUPPLIER_NAME = (string)x.Element("SUPPLIER_NAME"), ORDER_ITEMS_Id = (string)x.Element("ORDER_ITEMS_Id"), }).ToList();
foreach (var item in Items)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Insert INTO ITEM (NUMBER,CATALOG_NUMBER,ITEM_NAME,UNIT,AMOUNT,PRICE_WITHOUT_VAT,VAT,PRICE_VAT,EAN,SUPPLIER_ITEM_NUMBER,SUPPLIER_ID,SUPPLIER_NAME,ORDER_ITEMS_Id) VALUES (@cislo,@katalo_cislo,@nazev_zbozi,@jednotka,@mnozstvi,@cenabezvat,@vat1,@cenavat,@ean1,@dodav_cislo,@dodav_id,@dodav_jmeno,@objednavkaid);";
cmd.Parameters.AddWithValue("@cislo", item.NUMBER);
cmd.Parameters.AddWithValue("@katalo_cislo", item.CATALOG_NUMBER);
cmd.Parameters.AddWithValue("@nazev_zbozi", item.ITEM_NAME);
cmd.Parameters.AddWithValue("@jednotka", item.UNIT);
cmd.Parameters.AddWithValue("@mnozstvi", item.AMOUNT);
cmd.Parameters.AddWithValue("@cenabezvat", item.PRICE_WITHOUT_VAT);
cmd.Parameters.AddWithValue("@vat1", item.VAT);
cmd.Parameters.AddWithValue("@cenavat", item.PRICE_VAT);
cmd.Parameters.AddWithValue("@ean1", item.EAN);
cmd.Parameters.AddWithValue("@dodav_cislo", item.SUPPLIER_ITEM_NUMBER);
cmd.Parameters.AddWithValue("@dodav_id", item.SUPPLIER_ID);
cmd.Parameters.AddWithValue("@dodav_jmeno", item.SUPPLIER_NAME);
cmd.Parameters.AddWithValue("@objednavkaid", item.ORDER_ITEMS_Id);
cmd.ExecuteNonQuery();
//cmd.Clone();
}
}