我将数据从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();
但是前两个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();
}
}
答案 0 :(得分:3)
foreach (var katalo_cislo in CATALOG_NUMBER)
foreach (var vat1 in VAT){}
上面的代码会将(no of CATALOG_NUMBER)* (no of VAT)
记录插入到数据库中,我认为你需要将项目插入项目表。然后,您只需选择ITEM
节点并将其插入到表中,如下所示
var Items = doc.Descendants("ITEM")
.Select(x=> new {CATALOG_NUMBER= (string)x.Element("CATALOG_NUMBER"),
VAT = (int?)x.Element("VAT")} ).ToList();
foreach( var item in Items)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Insert INTO ITEM (CATALOG_NUMBER,VAT) VALUES (@katalo_cislo,@vat1);";
cmd.Parameters.AddWithValue("@katalo_cislo", item.CATALOG_NUMBER);
cmd.Parameters.AddWithValue("@vat1", (object)item.VAT??DBNull.Value);
cmd.ExecuteNonQuery();
}
}