开发者

.NET C# insert into SQLite > 1 million records in one batch

I am trying to insert over 1 million entries in a SQLite database from C# code. If I do it one by one, it takes forever. The insert statement is the same just with different parameters. Was wondering if there is something like in Java PreparedStatement st.addBatch(sql). Also I am using the Entity Framework so 开发者_Go百科wondered if it had any way of doing batch inserts as well. If not, then any other way will work as long as I can do it under reasonable amount of time.

I have tried to do it using transactions what seemed to work but wondering if there is any other proper way of doing it. Also having this in Entity Framework would be the best for me.

This is what I have so far to get it to work:

SQLiteCommand com = new SQLiteCommand("insert into Entrys(ID,KeyEntry,Password) values(@id,@key,@pass)", con);
com.Parameters.Add("@id", DbType.Int64);
com.Parameters.Add("@key", DbType.Int64);
com.Parameters.Add("@pass", DbType.Int64);
com.Prepare();
SQLiteTransaction tr = con.BeginTransaction();
com.Transaction = tr;

int limit = 1000000;
for(int i=1;i<limit;i++)
{
    com.Parameters[0].Value = 1;
    com.Parameters[1].Value = i;
    com.Parameters[2].Value = i;

    com.ExecuteNonQuery();
}

tr.Commit();
con.Close()

Thank you

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜