日期:2014-05-18  浏览次数:21022 次

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 新手,详细点
不要用参数,目的是可以快速插入大量数据到表中,有一些没读懂瞎写的,看在我就接触两个星期的份上,不要喷


------解决方案--------------------
改用sqlcommand去实现更新应该就不会出错了,如果需要做一次性更新可以使用事务sqltransaction
------解决方案--------------------
你设置了参数
sd.InsertCommand = new System.Data.SQLite.SQLiteCommand("insert into Test (devid,data_time,data_value