本文介绍了将数据从xml doc保存到sqlserver 2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做一个wpf C#应用程序,我想通过storeprocedure保存到数据库中.
该过程接受xml文档的位置.我不知道如何从xml保存数据并保存它.
这是我的代码,
Hi, I am doing a wpf C# app where i want to save into database through storeprocedure.
where that procedure accepts a xml document. I do not know how to save data from xml and save it.
Here is my code,
public partial class MainWindow : Window
{
private XmlDocument saveXml;
List<string> attribute;
private String id, name, dep;
private SqlConnection con;
private SqlCommand com;
public MainWindow()
{
con = new SqlConnection();
con.ConnectionString = "Data Source=192.168.2.88; database=Student; User id=sa; Password=123456;";
saveXml = new XmlDocument();
attribute = new List<string>();
InitializeComponent();
saveXml.LoadXml("<root></root>");
for (int i = 0; i < 4; i++)
{
id = i.ToString();
name = "N" + i.ToString();
dep = "D" + i.ToString();
attribute.Add("id");
attribute.Add("name");
attribute.Add("dep");
XmlParsingLibrary.XmlEngine.getInstance().writeNode(saveXml, "Student", "//Root", attribute,id,name, dep);
attribute.Clear();
}
//MessageBox.Show(saveXml.OuterXml);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
con.Open();
com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "ProcSave";
SqlParameter inputUser2 = com.Parameters.AddWithValue("@xmlfile", saveXml);
inputUser2.Direction = ParameterDirection.Input;
com.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
}</string></string>
我的xml像
My xml like
<root>
<student id="1" name="N1" dep="D1" />
<student id="2" name="N2" dep="D2" />
<student id="3" name="N3" dep="D3" />
</root>
推荐答案
SELECT
XMLData.value('@id[1]','INT'), XMLData.value('@name[1]','NVARCHAR(500)'),XMLData.value('@dep[1]','NVARCHAR(max)')
FROM @XMLData.nodes('/root[1]') e(XMLData)
这篇关于将数据从xml doc保存到sqlserver 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!