110,566
社区成员
发帖
与我相关
我的任务
分享
using (SqlConnection conn = new SqlConnection (@"server = localhost; integrated security = ture; database = industry"))
{
string query = @"select * from department";
SqlDataAdapter da = new SqlDataAdapter (query, conn);
DataTable dt = new DataTable();
da.Fill(dt);
dt.WriteXml(@".\department.xml", XmlWriteMode.WriteSchema);
}
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
DataTable tempDt = new DataTable();
String tempTableName = "Employees";
String tempRowName = "Employee";
String tempSavePath = "../../Employees.xml";
using (SqlConnection tempCon = new SqlConnection())
{
tempCon.ConnectionString = "Data Source=.; Integrated Security=SSPI; Initial Catalog=Northwind;";
using (SqlCommand tempCmd = tempCon.CreateCommand())
{
tempCmd.CommandText = "Select top 100 * from " + tempTableName;
SqlDataAdapter tempDa = new SqlDataAdapter(tempCmd);
tempDa.Fill(tempDt);
}
}
XElement tempDoc = new XElement(tempTableName);
for (int i = 0; i < tempDt.Rows.Count; i++)
{
var tempElement = new XElement(tempRowName);
for (int j = 0; j < tempDt.Columns.Count; j++)
{
tempElement.Add(new XElement(tempDt.Columns[j].ColumnName, tempDt.Rows[i][j] == DBNull.Value ? String.Empty : tempDt.Rows[i][j]));
}
tempDoc.Add(tempElement);
}
tempDoc.Save(tempSavePath);