數(shù)據(jù)庫(kù)與XML文件交互
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System;
using System.Data.SqlClient;
using System.Xml.Linq;
//并未用到的庫(kù)
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Net.Mime;
namespace 數(shù)據(jù)庫(kù)與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) //保存的參數(shù)需要有節(jié)點(diǎn)的屬性
??? {// 參數(shù)為: 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);//構(gòu)造一個(gè)xml文檔對(duì)象
??????? XElement provinceroot = provinceDoc.Root;//獲取文檔對(duì)象的根節(jié)點(diǎn)
??????? IEnumerable<XElement> eles = provinceroot.Elements();//獲取根節(jié)點(diǎn)下的所有元素,返回的是一個(gè)IEnumerable類型的
?????? ?
??????? using (SqlConnection conn = new SqlConnection(connstr))
??????? {
??????????? conn.Open();
??????????? if (conn.State != ConnectionState.Open)
??????????? {
??????????????? MessageBox.Show("連接數(shù)據(jù)庫(kù)失敗");
??????????? }
??????????? 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時(shí)進(jìn)行的操作,其實(shí)主要是針對(duì)省進(jìn)行的操作
??????????????????? if (xElement.Attribute(s3) == null)
??????????????????????? AreaPid = "0";
??????????????????? else
??????????????????????? AreaPid = xElement.Attribute(s3).Value;
??????????????????? SqlParameter[] parameters =//定義sql參數(shù)
??????????????????????? {
??????????????????????????????? new SqlParameter("@AreaName", AreaName),
??????????????????????????????? new SqlParameter("@AreaPid", AreaPid)
??????????????????????????? };
??????????????????? cmd.Parameters.AddRange(parameters);//傳入?yún)?shù)
??????????????????? try
??????????????????? {
??????????????????????? cmd.ExecuteNonQuery();//執(zhí)行該語(yǔ)句
??????????????????? }
??????????????????? catch (SqlException ae)
??????????????????? {
??????????????????????? MessageBox.Show(ae.Message);
??????????????????????? MessageBox.Show("sql error");
??????????????????? }
??????????????? }
??????????? }
??????????? conn.Close();
??????? }
??????? MessageBox.Show("讀入成功");
??????? //Console.ReadKey();//應(yīng)該相當(dāng)于pause
??? }
??? private void button1_Click(object sender, EventArgs e)
??? {//將xml數(shù)據(jù)讀入數(shù)據(jù)庫(kù)
??????? string t = textBox1.Text;
??????? // 相當(dāng)于先在數(shù)據(jù)庫(kù)kkk中建了表kk。隨后將xml數(shù)據(jù)插入進(jìn)去
??????? //博客: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)
??? {
??????? //將數(shù)據(jù)庫(kù)內(nèi)容讀出至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");//將讀出來(lái)的內(nèi)容寫(xiě)到一個(gè)xml文件中
??????????? MessageBox.Show("讀出成功");
??? }
}
}
