public void SaveToDB(DataTable dt)
{
/* todo:sqlite没有提供批量插入的机制,需要通过事务处理 更新所有数据
* http://www.cnblogs.com/hbjohnsan/p/4169612.html
* Eorr 数据库加了锁,执行不了自己的代码。
*/
string connStr = @"data source=E:\YQSQLite\YQSQLite\Data\YQ.db";
using (SQLiteConnection conn = new SQLiteConnection(connStr))
{
conn.Open();
using (System.Data.SQLite.SQLiteTransaction trans = conn.BeginTransaction())
{
using (System.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand(conn))
{
cmd.Transaction = trans;
try
{
foreach (DataRow dr in dt.Rows)
{
cmd.CommandText = @"insert or ignore into RssItem() values ("+Int32.Parse(dr[0].ToString()) +",'"
+dr[1].ToString()+"','"+dr[2].ToString()+"','"
+ dr[3].ToString() + "','" + DateTime.Parse(dr[4].ToString()) + "','"
+dr[5].ToString()+"','"+dr[6].ToString()+"')";
cmd.ExecuteNonQuery();
}
trans.Commit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
trans.Rollback();
}
}
}
}
}