首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#:SQLiteDataAdapter插入速度慢

C#:SQLiteDataAdapter插入速度慢
EN

Stack Overflow用户
提问于 2018-05-15 09:21:50
回答 1查看 438关注 0票数 1

我有一个包含25-30K项的JSON文件,我想将其保存在sqlite数据库中。但是,SQLiteDataAdapter类用包含所有项的DataTable对象更新数据库所需的时间大约是15K项的35分钟。执行的更新的性能受到UpdateBatchSize的限制,在我的例子中它是1,当我试图更改它时,我得到了一个异常,它说“不支持指定的方法”。

以下是更新方法:

代码语言:javascript
复制
        public async Task<bool> SaveTable()
    {
        SQLiteDataAdapter data_adapter = null;
        try
        {

            data_adapter = new SQLiteDataAdapter(sql_cmd);
            data_adapter.UpdateBatchSize = 10;
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception Thrown: " + e.Message);
            return false;
        }

        try
        {
            SQLiteCommandBuilder cmd_bldr = new SQLiteCommandBuilder(data_adapter);
            data_adapter.InsertCommand = cmd_bldr.GetInsertCommand();
            Console.WriteLine(data_adapter.InsertCommand.CommandText);
            data_adapter.AcceptChangesDuringUpdate = true;
            data_adapter.UpdateCommand = data_adapter.InsertCommand;
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception Thrown: " + e.Message);
            return false;
        }

        try
        {
            var size = data_adapter.UpdateBatchSize;
            Console.WriteLine("Updating Table. Batch Size: " + size);
            var rows_updated = data_adapter.Update(data_table);
            Console.WriteLine("Rows Updated: " + rows_updated.ToString());
            data_adapter.Dispose();
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception Thrown: " + e.Message);
            return false;
        }
        return true;
    }

有没有办法改变UpdateBatchSize或增加每秒更新的次数?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-15 09:58:39

我认为您使用SQLiteDataAdapter的方法是错误的。UpdateBatchSize属性不是由SQLite连接器实现的,因此不能更改。默认值1表示禁用了更新批处理功能。因此,当请求更新时,适配器被迫逐行运行一个缓慢的一行进程。

最好的方法是手动执行每个插入命令,但将它们封装在事务中。

代码语言:javascript
复制
// Getting the command text from the SQLiteCommandBuilder, but at this
// point you could simply write it as text directly in the constructor
SQLiteCommandBuilder cmd_bldr = new SQLiteCommandBuilder(data_adapter);
using (var cmd = new SQLiteCommand(cmd_bldr.GetInsertCommand(), conn))
{
    conn.Open();

    // Create the parameters collection, setting the type for each 
    // parameter but without setting an explicit value
    cmd.Parameters.Add("@p1", DbType.Int);
    // create other parameters for each field to insert ....

    using (var transaction = conn.BeginTransaction())
    {
        // Inform the command about the open transaction
        cmd.Transaction = transaction;

        // Loop over your table rows....
        foreach(DataRow row in data_table.Rows)
        {
            // get the parameters value from the row's field
            cmd.Parameters["@p1"].Value = row[fieldIndex];
            .... repeat for other parameters ...

            cmd.ExecuteNonQuery();
        }
        transaction.Commit();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50346578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档