欢迎光临散文网 会员登陆 & 注册

数据库与XML文件交互

2023-03-19 21:00 作者:花大发555  | 我要投稿

using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System;
using System.Data.SqlClient;
using System.Xml.Linq;

//并未用到的库
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Net.Mime;

namespace 数据库与xml文件交互
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    static void ImportDatabase(string s2, string s3, string s4, string filename) //保存的参数需要有节点的属性
    {// 参数为: ImportDatabase("ProvinceName", "CID", "dbo.kk", "D:/桌面/city.xml");

        string connstr = "Data Source = DESKTOP-CHKF0KA;Initial Catalog=kkk;Integrated Security=True";

        string sql = string.Format("insert into {0}(AreaName,AreaPid)  values(@AreaName,@AreaPid)", s4);//"dbo.kk"
        
        //读取xml文件
        XDocument provinceDoc = XDocument.Load(filename);//构造一个xml文档对象
        XElement provinceroot = provinceDoc.Root;//获取文档对象的根节点
        IEnumerable<XElement> eles = provinceroot.Elements();//获取根节点下的所有元素,返回的是一个IEnumerable类型的
        
        using (SqlConnection conn = new SqlConnection(connstr))
        {
            conn.Open();
            if (conn.State != ConnectionState.Open)
            {
                MessageBox.Show("连接数据库失败");
            }

            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                foreach (var xElement in eles)
                {
                    cmd.Parameters.Clear();
                    string AreaName = xElement.Attribute(s2).Value; //获取xml属性名称为s2的属性值 "ProvinceName"
                    string AreaPid = "";
                    //如果xml属性名称为s3的属性的属性值为null时进行的操作,其实主要是针对省进行的操作
                    if (xElement.Attribute(s3) == null)
                        AreaPid = "0";
                    else
                        AreaPid = xElement.Attribute(s3).Value;

                    SqlParameter[] parameters =//定义sql参数
                        {
                                new SqlParameter("@AreaName", AreaName),
                                new SqlParameter("@AreaPid", AreaPid)
                            };
                    cmd.Parameters.AddRange(parameters);//传入参数

                    try
                    {
                        cmd.ExecuteNonQuery();//执行该语句
                    }
                    catch (SqlException ae)
                    {
                        MessageBox.Show(ae.Message);
                        MessageBox.Show("sql error");
                    }
                }
            }
            conn.Close();
        }


        MessageBox.Show("读入成功");
        //Console.ReadKey();//应该相当于pause
    }

    private void button1_Click(object sender, EventArgs e)
    {//将xml数据读入数据库
        string t = textBox1.Text;
        // 相当于先在数据库kkk中建了表kk。随后将xml数据插入进去
        //博客:https://www.cnblogs.com/hanwenhuazuibang/archive/2013/05/09/3067842.html
        ImportDatabase("ProvinceName", "CID", "dbo.kk",t );
                      //                        插入表 "D:/桌面/city.xml"
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        //将数据库内容读出至xml文件中
        string connStr = "Data Source = DESKTOP-CHKF0KA;Initial Catalog=kkk;Integrated Security=True";

        string sql = string.Format("select * from dbo.kk");
            DataSet ds = new DataSet();
            using (SqlDataAdapter sda = new SqlDataAdapter(sql, connStr))
            {
                sda.Fill(ds);
            }
            ds.WriteXml("D:/桌面/b.xml");//将读出来的内容写到一个xml文件中
            MessageBox.Show("读出成功");
    }
}

}


数据库与XML文件交互的评论 (共 条)

分享到微博请遵守国家法律