793291696 servertoxml

连接数据库,dataset存查询数据,每条记录对应dataset里的datatable 控制台程序

第一种方法

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace readtxttoxml
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string connStr = "Data Source=localhost;Initial Catalog=scnt;Integrated Security=True;";

            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();

                // 查询数据
                string query = "SELECT * FROM student";
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet("students");//dataset 名称
                    adapter.Fill(ds,"student");//datatable 名称对应数据库查询的一条条记录

                    // 将数据写入 XML 文件,整个dataset名为xml的父标签,datatable名即每条数据标签名称
                    ds.WriteXml("D:/DDD/test.xml");
                }
            }

            Console.WriteLine("Done.");
        }
    }
}

第二种方法

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string connStr = "Data Source=localhost;Initial Catalog=scnt;Integrated Security=True;";
            string query = "select * from student";
            using (SqlConnection connection = new SqlConnection(connStr))
            {
                SqlCommand command = new SqlCommand(query, connection);
                connection.Open();

                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet, "Student");

                // 创建一个新的 XML 文档
                XmlDocument xmlDoc = new XmlDocument();

                // 创建根元素
                XmlElement root = xmlDoc.CreateElement("Students");
                xmlDoc.AppendChild(root);

                // 遍历查询结果,并将每一行作为一个子元素添加到根元素下面
                foreach (DataRow row in dataSet.Tables["Student"].Rows)
                {
                    XmlElement rowElement = xmlDoc.CreateElement("Student");
                    root.AppendChild(rowElement);

                    // 遍历每一列,并将列名作为子元素的名称,列值作为子元素的文本内容
                    foreach (DataColumn col in dataSet.Tables["Student"].Columns)
                    {
                        XmlElement colElement = xmlDoc.CreateElement(col.ColumnName);
                        colElement.InnerText = row[col].ToString();
                        rowElement.AppendChild(colElement);
                    }
                }
                xmlDoc.Save("D:/DDD/test.xml");


                Console.WriteLine("Done.");
            }
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值