求把SQL数据库里面的一张表转换成XML文件,求源码是C#

如题所述

第1个回答  推荐于2016-10-03
string tempTableName = "ProductType";//数据库表名
String tempRowName = "row";//节点
String tempSavePath = "../Employees.xml";//保存地址

DataTable datatd = new DataTable();
using (SqlConnection sqlcon = new SqlConnection())
{
sqlcon.ConnectionString = "server=192.168.11.111;database=Insects;uid=sa;pwd=2275570";
using (SqlCommand sqlcomm = sqlcon.CreateCommand())
{
sqlcomm.CommandText = "select * from " + tempTableName;
SqlDataAdapter tempa = new SqlDataAdapter(sqlcomm);
tempa.Fill(datatd);
}
}

XElement tempDoc = new XElement(tempTableName);
for (int i = 0; i < datatd.Rows.Count; i++)
{
var tempElement = new XElement(tempRowName);
for (int j = 0; j < datatd.Columns.Count; j++)
{
tempElement.Add(new XElement(datatd.Columns[j].ColumnName, datatd.Rows[i][j] == DBNull.Value ? String.Empty : datatd.Rows[i][j]));
}
tempDoc.Add(tempElement);

}
tempDoc.Save(tempSavePath);本回答被提问者采纳
相似回答