XML 导入导出数据库

XML导入数据库:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
using System.IO;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    string sqlstr=ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnDaoru_Click(object sender, EventArgs e)
    {

        Stream stream = File.OpenRead(Server.MapPath("book.xml"));
        XDocument document = XDocument.Load(stream);
        stream.Dispose();
        foreach (XElement element in document.Root.Descendants("book"))
        {
            string title = element.Element("title").Value;
            string author = element.Element("author").Value;
            string gender = element.Element("author").Attribute("gender").Value;
            string age = element.Element("author").Attribute("age").Value;
            string year = element.Element("year").Value;
            string price = element.Element("price").Value;
            using (SqlConnection sqlcnn = new SqlConnection(sqlstr))
            {
                using (SqlCommand sqlcmm = sqlcnn.CreateCommand())
                {
                    sqlcmm.CommandText = "insert into book values(@title,@author,@gender,@age,@year,@price)";
                   sqlcnn.Open();
                   sqlcmm.Parameters.AddWithValue("@title", title);
                   sqlcmm.Parameters.AddWithValue("@author", author);
                   sqlcmm.Parameters.AddWithValue("@gender", gender);
                   sqlcmm.Parameters.AddWithValue("@age", age);
                   sqlcmm.Parameters.AddWithValue("@year", year);
                   sqlcmm.Parameters.AddWithValue("@price", price);
                    int a = sqlcmm.ExecuteNonQuery();
                   
                }
            }
        }
    }
}


XML导出数据库:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class DaochuXml : System.Web.UI.Page
{
    string sqlstr=ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnDaochu_Click(object sender, EventArgs e)
    {
        using (SqlConnection sqlcnn=new SqlConnection(sqlstr))
        {
            using (SqlCommand sqlcmm=sqlcnn.CreateCommand())
            {
                sqlcmm.CommandText = "select * from book";
                SqlDataAdapter adapt = new SqlDataAdapter(sqlcmm);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                XElement xmlbookstore = new XElement("bookstore");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    XElement book = new XElement("book");
                    XElement title = new XElement("title",dt.Rows[i][0]);
                    XElement author = new XElement("author", dt.Rows[i][1]);
                    author.SetAttributeValue("age", dt.Rows[i][2]);
                    author.SetAttributeValue("sex", dt.Rows[i][3]);
                    XElement year = new XElement("year", dt.Rows[i][4]);
                    XElement price = new XElement("price", dt.Rows[i][5]);
                    book.Add(title);
                    book.Add(author);
                    book.Add(year);
                    book.Add(price);
                    xmlbookstore.Add(book);
                }
                
                FileStream stream =File.OpenWrite(@"c:\book.xml");
                StreamWriter writer = new StreamWriter(stream);
                writer.WriteLine(xmlbookstore.ToString());
                writer.Flush();
                stream.Dispose();

            }
        }
    }
}


 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值