更新
:
在阅读了你的解决方案之后,让我说,我很困惑,我没有理解。枚举行以设置要添加的行状态将有效。
但让我用adapter.acceptChangesDuringFill为您提供一种更清晰的方法。
using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + name, conn))
{
// this is the proper way to transfer rows
adapter.AcceptChangesDuringFill = false;
QuoteDataSet ds = new QuoteDataSet();
adapter.Fill(ds, tableName);
Console.WriteLine("Num rows loaded is " + ds.Tags.Rows.Count);
InsertData(ds, tableName);
}
这将导入83行:
程序CS
using System;
using System.Data;
using System.Data.SQLite;
namespace SqliteCsv
{
internal class Program
{
private static void Main(string[] args)
{
// fill your dataset
QuoteDataSet ds = new QuoteDataSet();
ds.ReadXml("data.xml", XmlReadMode.InferTypedSchema);
// hack to flag each row as new as per lirik
// OR just set .AcceptChangesDuringFill=false on adapter
foreach (DataTable table in ds.Tables)
{
foreach (DataRow row in table.Rows)
{
row.SetAdded();
}
}
int insertedRows;
using (
SQLiteConnection con =
new SQLiteConnection(@"data source=C:\Projects\StackOverflowAnswers\SqliteCsv\SqliteCsv\Quotes.db"))
{
con.Open();
// clear table - you may not want this
SQLiteCommand cmd = con.CreateCommand();
cmd.CommandText = "delete from Tags";
cmd.ExecuteNonQuery();
using (SQLiteTransaction txn = con.BeginTransaction())
{
using (SQLiteDataAdapter dbAdapter = new SQLiteDataAdapter("select * from Tags", con))
{
dbAdapter.InsertCommand = new SQLiteCommandBuilder(dbAdapter).GetInsertCommand(true);
insertedRows = dbAdapter.Update(ds, "Tags");
}
txn.Commit();
}
}
Console.WriteLine("Inserted {0} rows", insertedRows);
Console.WriteLine("Press any key");
Console.ReadKey();
}
}
}
原始答案:
这是最终被调用的sqlitedataadapter ctor的源。注意,没有使用命令生成器。您需要显式设置sqlitedataadapter上的insercommand属性,或者使用sqliteCommandBuilder?
public SQLiteDataAdapter(string commandText, SQLiteConnection connection)
{
this.SelectCommand = new SQLiteCommand(commandText, connection);
}