c#sqlite 求教 批量插入,该怎么解决
1个回答
2016-04-07 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
用事务啊,以及参数化查询,一定要,示例
internal string FastInsertMany()
{
StringBuilder builder = new StringBuilder();
using (DbTransaction dbTrans = _cnn.BeginTransaction())
{
int dtStart;
int dtEnd;
using (DbCommand cmd = _cnn.CreateCommand())
{
cmd.Transaction = dbTrans;
cmd.CommandText = "INSERT INTO TestCase(Field1) VALUES(@p1)";
DbParameter Field1 = cmd.CreateParameter();
Field1.ParameterName = "@p1";
cmd.Parameters.Add(Field1);
//Console.WriteLine(String.Format(" Fast insert using parameters and prepared statement\r\n -> (100,000 rows) Begins ... "));
dtStart = Environment.TickCount;
for (int n = 0; n < 100000; n++)
{
Field1.Value = n + 200000;
cmd.ExecuteNonQuery();
}
dtEnd = Environment.TickCount;
dtEnd -= dtStart;
builder.AppendFormat("Ends in {0} ms ... ", (dtEnd));
}
dtStart = Environment.TickCount;
dbTrans.Commit();
dtEnd = Environment.TickCount;
dtEnd -= dtStart;
builder.AppendFormat("Commits in {0} ms", (dtEnd));
}
return builder.ToString();
}
internal string FastInsertMany()
{
StringBuilder builder = new StringBuilder();
using (DbTransaction dbTrans = _cnn.BeginTransaction())
{
int dtStart;
int dtEnd;
using (DbCommand cmd = _cnn.CreateCommand())
{
cmd.Transaction = dbTrans;
cmd.CommandText = "INSERT INTO TestCase(Field1) VALUES(@p1)";
DbParameter Field1 = cmd.CreateParameter();
Field1.ParameterName = "@p1";
cmd.Parameters.Add(Field1);
//Console.WriteLine(String.Format(" Fast insert using parameters and prepared statement\r\n -> (100,000 rows) Begins ... "));
dtStart = Environment.TickCount;
for (int n = 0; n < 100000; n++)
{
Field1.Value = n + 200000;
cmd.ExecuteNonQuery();
}
dtEnd = Environment.TickCount;
dtEnd -= dtStart;
builder.AppendFormat("Ends in {0} ms ... ", (dtEnd));
}
dtStart = Environment.TickCount;
dbTrans.Commit();
dtEnd = Environment.TickCount;
dtEnd -= dtStart;
builder.AppendFormat("Commits in {0} ms", (dtEnd));
}
return builder.ToString();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询