oracle:
try
{
string tempTableName = "aaa";//数据库表名
String tempRowName = "row";//节点
String tempSavePath = "D:/Download/Master/users.xml";//保存地址
DataTable datatd = new DataTable();
using (OracleConnection conn = new OracleConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["Conn_DB"].ToString();
using (OracleCommand sqlcomm = conn.CreateCommand())
{
sqlcomm.CommandText = "select * from " + tempTableName;
OracleDataAdapter tempa = new OracleDataAdapter(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);
sqlserver:
string tempTableName = "ProductType";//数据库表名
String tempRowName = "row";//节点
String tempSavePath = "../Employees.xml";//保存地址
DataTable datatd = new DataTable();
using (SqlConnection sqlcon = new SqlConnection())
{
sqlcon.ConnectionString = "server=;database=;uid=sa;pwd=";
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);