简体   繁体   English

将XML数据保存到SQL Server表

[英]Save XML data to SQL Server table

I have this xml file, and I want to save value NUMBER (for example) to a SQL Server table. 我有这个xml文件,并且想将值NUMBER (例如)保存到SQL Server表中。

<ORDER>
  <ORDER_HEADER>
    <NUMBER>10945</NUMBER>
    <TIME>7.8.2013 12:45:20</TIME>
    <NOTE>this is Note</NOTE>   
  </ORDER_HEADER>
</ORDER> 

This is my code: 这是我的代码:

XDocument doc = XDocument.Load("C:\\Users\\L\\Desktop\\data.xml");
var NUMBER = doc.Descendants("NUMBER");
var TIME = doc.Descendants("TIME");
var NOTE = doc.Descendants("NOTE");

foreach (var cislo in NUMBER)
{
    SqlConnection conn = new SqlConnection("Data Source=***");
    conn.Open();

    using (SqlCommand cmd = conn.CreateCommand())
    {
       cmd.CommandText = "Update CISLO SET cislo = @cislo1;";
       cmd.Parameters.AddWithValue("@cislo1", doc);

       cmd.ExecuteNonQuery();
    }
 }

 MessageBox.Show("OK");

I get this error: 我收到此错误:

There is no mapping from object type System.Xml.Linq.XDocument to a known managed provider native type. 从对象类型System.Xml.Linq.XDocument到已知的托管提供程序本机类型没有映射。

On row: 在行上:

cmd.ExecuteNonQuery();

You are passing 'doc', which is your XDocument, into the parameter. 您正在将XDocument的'doc'传递给参数。 Try changing 尝试改变

cmd.Parameters.AddWithValue("@cislo1", doc);

to

cmd.Parameters.AddWithValue("@cislo1", cislo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM