Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C向Sqlite数据库插入大量行的性能增强#_C#_Sqlite - Fatal编程技术网

C# 使用C向Sqlite数据库插入大量行的性能增强#

C# 使用C向Sqlite数据库插入大量行的性能增强#,c#,sqlite,C#,Sqlite,我有对象图包含字典,字典包含大量单词,大约165000行需要插入到sqlite“Word”表中,实现如下代码: public class Dictionary { public Dictionary() { Words = new List<Word>(); } public int DictionaryId { get; set; } public string Name { get; set; } public st

我有对象图包含字典,字典包含大量单词,大约165000行需要插入到sqlite“Word”表中,实现如下代码:

public class Dictionary
{
    public Dictionary()
    {
        Words = new List<Word>();
    }
    public int DictionaryId { get; set; }
    public string Name { get; set; }
    public string FromLanguage { get; set; }
    public string ToLanguage { get; set; }
    public int NoOfWords { get; set; }
    public virtual ICollection<Word> Words { get; set; }
}

public class Word
{
    public Word()
    {
        Definitions = new List<Definition>();
        Comments = new List<Comment>();
    }
    public int WordId { get; set; }
    public int DictionaryId { get; set; }
    public Dictionary Dictionary { get; set; }
    public string Text { get; set; }
}
公共类字典
{
公共词典()
{
单词=新列表();
}
公共int字典ID{get;set;}
公共字符串名称{get;set;}
来自语言{get;set;}的公共字符串
公共字符串ToLanguage{get;set;}
公共整数NoOfWords{get;set;}
公共虚拟ICollection字{get;set;}
}
公共类词
{
公共词()
{
定义=新列表();
注释=新列表();
}
public int WordId{get;set;}
公共int字典ID{get;set;}
公共字典{get;set;}
公共字符串文本{get;set;}
}
以及以下要从word列表插入到数据库word表的代码:

using (var transaction = m_dbConnection.BeginTransaction())
        {
            int count = 0;
            m_dbConnection.Open();
            foreach (Word entry in dictionaryInfo.Words)
            {

                string sql6 = @"insert into Word (DictionaryId,Text) 
            values (@DictionaryId, @Text)";
                SQLiteCommand command6 = new SQLiteCommand(sql6, m_dbConnection);
                command6.Parameters.Add("@DictionaryId", DbType.Int32).Value = dictionaryInfo.DictionaryId;
                command6.Parameters.Add("@Text", DbType.String).Value = entry.Text;
                command6.ExecuteNonQuery();
                count++;
                if (count < 1000000)
                {
                    transaction.Commit();
                    count = 0;
                }

            }
        }
        m_dbConnection.Close();
使用(var事务=m_dbConnection.BeginTransaction())
{
整数计数=0;
m_dbConnection.Open();
foreach(字典中的词条信息Words)
{
字符串sql6=@“插入Word(字典ID,文本)
值(@DictionaryId,@Text)”;
SQLiteCommand command6=新的SQLiteCommand(sql6,m_dbConnection);
command6.Parameters.Add(“@DictionaryId”,DbType.Int32).Value=dictionaryInfo.DictionaryId;
command6.Parameters.Add(“@Text”,DbType.String).Value=entry.Text;
command6.ExecuteNonQuery();
计数++;
如果(计数<1000000)
{
Commit();
计数=0;
}
}
}
m_dbConnection.Close();
插入过程需要大约8小时才能完成。如何提高性能以加快流程

提前谢谢

编辑

我要感谢Alex K和Dirk的帮助。错误在计数比较中应该是正确的
if(计数>1000000)
{
Commit();
计数=0;

}

您从不增加
计数
,因此它总是小于<1000000,这意味着您每次都要提交
Commit
。我很惊讶一个事务会有多个提交,甚至你使用的是哪个版本的SQLite?@Alex抱歉,我写问题时缺少增量代码。我编辑我的question@JordyvanEijk版本3@AnasJaber你的编辑根本没有帮助:1)你仍然在为每次迭代调用
Commit
。2.)您甚至不将事务用于命令。