求把SQL数据库里面的一张表转换成XML文件,求源码是C#
展开全部
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);
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);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询