asp.net XML导入SQL数据库使用foreach方法
protected void btnSumit_Click(object sender, EventArgs e)
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
string path = Server.MapPath("db.xml");
XmlDocument xml = new XmlDocument();
xml.Load(path);
XmlNodeList node = xml.GetElementsByTagName("Item");
foreach (XmlElement n in node)
{
cmd.CommandText = string.Format("insert into img2(title,content,creatTime) values('"+n.GetAttribute("title")+"','"+n.GetAttribute("Content")+"','"+n.GetAttribute("CreatTime")+"')");
cmd.ExecuteNonQuery();
}
}
conn.Close();
}
protected void btnXML_Click(object sender, EventArgs e)
{
string xml = "select id,img,imgname,title,content,creattime from img ";
conn.Open();
SqlCommand cmd = new SqlCommand(xml,conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ds.WriteXml(Server.MapPath("XML3.xml"));
conn.Close();
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('导入成功')</script>");
}