2
我有這個xml文件,我想將值NUMBER
(例如)保存到SQL Server表。將XML數據保存到SQL Server表
<ORDER>
<ORDER_HEADER>
<NUMBER>10945</NUMBER>
<TIME>7.8.2013 12:45:20</TIME>
<NOTE>this is Note</NOTE>
</ORDER_HEADER>
</ORDER>
這是我的代碼:
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");
我得到這個錯誤:
There is no mapping from object type System.Xml.Linq.XDocument to a known managed provider native type.
在行:
cmd.ExecuteNonQuery();
大概應該是'cislo.Value' – MarcinJuraszek
YES! cmd.Parameters.AddWithValue(「@ cislo1」,cislo.Value); – Kate
好通話,@MarcinJuraszek – jle