C#读取数据库内容并转换成xml文件

时间:2021-05-14 13:35:17
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\bible.mdb");
string query_sql = "select VolumeName,ChapterNumber,SectionNumber,ChineseContent from BibleTable";
try
{
    conn.Open();
    OleDbCommand cmd = new OleDbCommand();
    cmd.CommandText = query_sql;
    cmd.Connection = conn;
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    cmd.ExecuteNonQuery();

    if (dt.Rows.Count > 0)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(sw);
        dt.TableName = "Bible";
        dt.WriteXml(xw);
        File.WriteAllText("bible.xml", sw.ToString(), Encoding.Default);
    }
}
catch (Exception a)
{
    throw a;
}
finally
{
    conn.Close();
}