c# sqlite 求教 批量插入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
System.Data.SQLite.SQLiteConnection.CreateFile("datasource");
string datasource = "D:/test.db"; System.Data.SQLite.SQLiteConnection.CreateFile(datasource);
string connectionString = @"Server=localhost;Database=Northwind;Trusted_Connection=true";
System.Data.SQLite.SQLiteConnectionStringBuilder connstr = new System.Data.SQLite.SQLiteConnectionStringBuilder();
connstr.DataSource = datasource;
using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(connectionString))
{
conn.ConnectionString = connstr.ToString();
conn.Open();
System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
string sql = "CREATE TABLE test(devid int,data_time int,data_value int)";
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
System.Data.SQLite.SQLiteDataAdapter sd = new System.Data.SQLite.SQLiteDataAdapter();
sd.SelectCommand = new System.Data.SQLite.SQLiteCommand("select devid,data_time,data_value from Test", conn);
sd.InsertCommand = new System.Data.SQLite.SQLiteCommand("insert into Test (devid,data_time,data_value) "
+ " values (@devid,@data_time,@data_value);", conn);
sd.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
DataSet dataset = new DataSet();
sd.Fill(dataset);
for (int i = 0; i < 100000; i++)
{
Random r = new Random(1000);
object[] row = { i, r.Next(1, 1000), r.Next(1, 1000) };
dataset.Tables[0].Rows.Add(row);
try
{
if (i % 300 == 0)
{
sd.Update(dataset.Tables[0]);
dataset.Tables[0].Clear();
}
}
catch (Exception )
{ }
}
sd.Update(dataset.Tables[0]);
dataset.Tables[0].Clear();
sd.Dispose();
dataset.Dispose();
conn.Close();
}
}
}
}
就这个,错误语句:sd.Update(dataset.Tables[0]);原因SQLite error;Insufficient parameters supplied to the command 新手,详细点
不要用参数,目的是可以快速插入大量数据到表中,有一些没读懂瞎写的,看在我就接触两个星期的份上,不要喷
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
System.Data.SQLite.SQLiteConnection.CreateFile("datasource");
string datasource = "D:/test.db"; System.Data.SQLite.SQLiteConnection.CreateFile(datasource);
string connectionString = @"Server=localhost;Database=Northwind;Trusted_Connection=true";
System.Data.SQLite.SQLiteConnectionStringBuilder connstr = new System.Data.SQLite.SQLiteConnectionStringBuilder();
connstr.DataSource = datasource;
using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(connectionString))
{
conn.ConnectionString = connstr.ToString();
conn.Open();
System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
string sql = "CREATE TABLE test(devid int,data_time int,data_value int)";
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
System.Data.SQLite.SQLiteDataAdapter sd = new System.Data.SQLite.SQLiteDataAdapter();
sd.SelectCommand = new System.Data.SQLite.SQLiteCommand("select devid,data_time,data_value from Test", conn);
sd.InsertCommand = new System.Data.SQLite.SQLiteCommand("insert into Test (devid,data_time,data_value) "
+ " values (@devid,@data_time,@data_value);", conn);
sd.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
DataSet dataset = new DataSet();
sd.Fill(dataset);
for (int i = 0; i < 100000; i++)
{
Random r = new Random(1000);
object[] row = { i, r.Next(1, 1000), r.Next(1, 1000) };
dataset.Tables[0].Rows.Add(row);
try
{
if (i % 300 == 0)
{
sd.Update(dataset.Tables[0]);
dataset.Tables[0].Clear();
}
}
catch (Exception )
{ }
}
sd.Update(dataset.Tables[0]);
dataset.Tables[0].Clear();
sd.Dispose();
dataset.Dispose();
conn.Close();
}
}
}
}
就这个,错误语句:sd.Update(dataset.Tables[0]);原因SQLite error;Insufficient parameters supplied to the command 新手,详细点
不要用参数,目的是可以快速插入大量数据到表中,有一些没读懂瞎写的,看在我就接触两个星期的份上,不要喷
...全文
相关推荐
c# 对sqlite基本操作,带批量插入(百万级秒插)
下载来的资源,自己改了一下,共享出来,供小白使用,本想免费的,无奈最少是2分,再次吐槽一下csdn的下载规则变更,越来越商业化了。
SQLite INSERT 数据操作指南:用C#轻松搞定数据插入,你学会了吗?
SQLite是一个轻量级的嵌入式数据库管理系统,它不需要单独的服务器进程,也不需要复杂的配置。SQLite支持SQL语言,非常适合小型项目和嵌入式设备使用。通过以上详细的介绍和代码示例,相信你已经学会了如何在C#中使用SQLite进行数据插入操作。SQLite的轻量级特性和C#的强大功能相结合,使得数据操作变得非常简单和高效。如果你有任何问题或建议,欢迎在评论区留言交流!希望这篇教程对你有所帮助,期待你在C#和SQLite开发中取得更大的成就!🌟✨😊✨。
C#/.Net 中快速批量给SQLite数据库插入测试数据
主要介绍了C#/.Net 中快速批量给SQLite数据库插入测试数据,本文直接给出实例代码,需要的朋友可以参考下
c#实现几种数据库的大数据批量插入
主要介绍了c#实现几种数据库的大数据批量插入,主要包括SqlServer、Oracle、SQLite和MySQL,有兴趣的可以了解一下。
C# sqlite 批量更新及性能测试
C# sqlite 批量更新及性能测试


