简体   繁体   English

将大型SQL Server CE数据库导出到XML文件的更快方法?

[英]Faster way to export large SQL Server CE database to XML file?

What is a faster way to export large (25,000 rows) SQL Server CE database into XML file? 将大型(25,000行)SQL Server CE数据库导出到XML文件的更快方法是什么?

Here is what I currently use: 这是我目前使用的:

using (SqlCeConnection cn = new SqlCeConnection(strConnection))
{
    if (cn.State == ConnectionState.Closed)
        cn.Open();
    using (SqlCeCommand cmd = new SqlCeCommand(strCommand, cn))
    {
        SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "item");
        StreamWriter xmlDoc = new StreamWriter("Output.xml");
        ds.WriteXml(xmlDoc);
        xmlDoc.Close();
    }
}

It takes about 60 seconds inside emulator (Windows Mobile). 在模拟器中(Windows Mobile)大约需要60秒。

Also.. I am using Compact Framework 3.5 with SQL Server CE 3.5. 另外,我正在使用带有SQL Server CE 3.5的Compact Framework 3.5。

Current performance: 60 seconds entire code ~20 seconds for everything without ds.WriteXml(xmlDoc); 当前性能:不使用ds.WriteXml(xmlDoc);所有代码,整个代码需要60秒ds.WriteXml(xmlDoc);ds.WriteXml(xmlDoc); , leaving ~40 seconds for ds.WriteXml(xmlDoc); ,留下约40秒的ds.WriteXml(xmlDoc); .

Maybe something like this is more lightweight: 也许这样的东西更轻量:

    private void ExportDataTable(SqlCeCommand cmd, SqlCeConnection conn)
    {
        cmd.Connection = conn;
        System.Data.DataTable table = new System.Data.DataTable();
        table.Locale = CultureInfo.InvariantCulture;
        table.Load(cmd.ExecuteReader());
        StreamWriter xmlDoc = new StreamWriter("Output.xml");
        table.WriteXml(xmlDoc);
    }

If the effort is worth it to you, the fastest method is probably to roll your own XML generation. 如果您的努力值得您付出,那么最快的方法可能就是推出您自己的XML一代。 The library implementation of XmlWriter() is loops within loops, with considerable generality. XmlWriter()的库实现是循环中的循环,具有相当大的通用性。

Declare your own output stream specifying the buffer size to something reasonably large (but still sane). 声明自己的输出流,将缓冲区大小指定为相当大的值(但仍然合理)。 I have tested StringBuilder() before and append() to it millions of times with good performance. 我已经对StringBuilder()进行了测试,并对其附加()进行了数百万次的测试,性能良好。 Other output options may not be as fast, but I would hope the StreamWriter gives good performance when buffersize if appropriate and it force you to build everything in memory. 其他输出选项可能不会那么快,但我希望StreamWriter在适当的缓冲区大小时能提供良好的性能,并能迫使您在内存中构建所有内容。

Don't use da.Fill() -- replace with a SqlCeDataReader. 不要使用da.Fill()-用SqlCeDataReader替换。

For each row. 对于每一行。 Generate the XML for a single row using logic that is coded as close as possible to the metal. 使用尽可能接近金属编码的逻辑为单行生成XML。 Ie, Precalculate column index values, etc. instead of using column names within this loop, use hard-coded type conversions as needed. 即,预先计算列索引值等,而不是在此循环中使用列名,而是根据需要使用硬编码的类型转换。 Don't loop through each column, put each column 不要遍历每一列,而是将每一列

Also, test by having the database generating XML output. 另外,通过让数据库生成XML输出进行测试。 Although I don't expect this to be the fastest, it is easy to try, and if it turns out to be fast you would never have discovered it without trying. 尽管我不认为这是最快的,但是它很容易尝试,而且如果事实证明它很快,您将不会尝试就发现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM