c# sqlite 求教 批量插入

bs897945043 bs897945043 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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 新手,详细点
不要用参数,目的是可以快速插入大量数据到表中,有一些没读懂瞎写的,看在我就接触两个星期的份上,不要喷
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
bs897945043
C# 111,129 成员 642,538 内容