zoukankan      html  css  js  c++  java
  • 提取数据表保存为XML文件

      1  //连接数据库
      2         SqlConnection con = new SqlConnection("server=****;database=****;uid=sa;pwd=********");
      3        
      4         /// <summary>
      5         /// 提取数据表保存为XML文件
      6         /// </summary>
      7         /// <param name="sender"></param>
      8         /// <param name="e"></param>
      9         private void button3_Click(object sender, EventArgs e)
     10         {
     11             try
     12             {
     13                 //打开数据库
     14                 con.Open();
     15 
     16                 //当数据库为打开时
     17                 if (con.State == ConnectionState.Open)
     18                 {
     19 
     20                     //声明一个DataTable 存储数据
     21                     DataTable dt = new DataTable();
     22                     //SQL语句
     23                     string str = "select * from CurrentStock";
     24                     
     25                     //声明一个sql数据适配器执行sql语句
     26                     SqlDataAdapter sad = new SqlDataAdapter(str, con);
     27 
     28                     //将SQLql数据适配器的内容填充到DATATABEL
     29                     sad.Fill(dt);
     30 
     31                     //当DataTable的内容大于0 时,向下执行
     32                     if (dt.Rows.Count > 0)
     33                     {
     34 
     35                         //创建一个DOM对象
     36                         XDocument xDoc = new XDocument();
     37                         //创建XML文档的声明语句
     38                         XDeclaration xDec = new XDeclaration("1.0", "UTF-8", "no");
     39                         //将声明语句给XML文档xDoc
     40                         xDoc.Declaration=xDec;
     41 
     42                         //创建一个根节点
     43                         XElement xrootElement = new XElement("CurrentStock");
     44                         //将根节点添加到XML文档xDoc中
     45                         xDoc.Add(xrootElement);
     46                       
     47                         //遍历DataTable的每一行
     48                         for (int i = 0; i < dt.Rows.Count; i++)
     49                         {
     50                             //创建一个子节点,区分每一行的数据
     51                             XElement xElementRow = new XElement("Row");
     52                             //给子节点Row添加一属性,这里是行号,识别一共有多少行
     53                             xElementRow.SetAttributeValue("Autoid", (i + 1).ToString());
     54                             
     55                             //遍历每一列,主要获取列名
     56                             for (int j = 0; j <dt.Columns.Count; j++)
     57                             {
     58                                //向子节点Row中添加子元素, 元素的名称是dt中的列表  ,值是dt中每一行每一列的值
     59                                 xElementRow.SetElementValue(dt.Columns[j].ColumnName, dt.Rows[i][j].ToString().Trim());
     60 
     61                             }
     62 
     63                             //将子点节Row添加到根节点下
     64                             xrootElement.Add(xElementRow);
     65 
     66 
     67 
     68                         }
     69 
     70                         //保存XML文件
     71                         xDoc.Save("TableNameA");
     72 
     73                         //提示成功
     74                         MessageBox.Show("OK");
     75                     
     76                     }
     77 
     78                     else
     79                     {
     80                     
     81                       MessageBox.Show("表中没有数据!");
     82                     
     83                     }
     84 
     85 
     86                 }
     87                 else
     88                 {
     89                     MessageBox.Show("数据库连接出错,请检查!");
     90 
     91                 }
     92 
     93 
     94             }
     95             catch
     96             { 
     97             
     98             
     99             throw;
    100             
    101             }
    102             
    103 
    104             con.Close();
    105         }
  • 相关阅读:
    Mysql 修改默认端口
    通过.pro文件生成C++工程
    内联函数知识点
    DICOM文件添加私有Tag(DCMTK Private Tag)
    poj 1185 炮兵阵地 状压dp
    cf #216 C
    cdoj1365 木杆上的蚂蚁
    cf #214 Dima and Salad
    cf #213 Matrix
    hdu 2222 Keywords Search(AC自动机入门题)
  • 原文地址:https://www.cnblogs.com/hobe6699/p/5775277.html
Copyright © 2011-2022 走看看