参考1:

关于如何使用System.Data.SQLite的入门:

http://www.dreamincode.net/forums/topic/157830-using-sqlite-with-c%23/

 using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Globalization;
using System.Linq;
using System.Windows.Forms; namespace Simple_Disk_Catalog
{
public class SQLiteDatabase
{
String DBConnection; private readonly SQLiteTransaction _sqLiteTransaction; private readonly SQLiteConnection _sqLiteConnection; private readonly bool _transaction; /// <summary>
/// Default Constructor for SQLiteDatabase Class.
/// </summary>
/// <param name="transaction">Allow programmers to insert, update and delete values in one transaction</param>
public SQLiteDatabase(bool transaction = false)
{
_transaction = transaction;
DBConnection = "Data Source=recipes.s3db";
if (transaction)
{
_sqLiteConnection = new SQLiteConnection(DBConnection);
_sqLiteConnection.Open();
_sqLiteTransaction = _sqLiteConnection.BeginTransaction();
}
} /// <summary>
/// Single Param Constructor for specifying the DB file.
/// </summary>
/// <param name="inputFile">The File containing the DB</param>
public SQLiteDatabase(String inputFile)
{
DBConnection = String.Format("Data Source={0}", inputFile);
} /// <summary>
/// Commit transaction to the database.
/// </summary>
public void CommitTransaction()
{
_sqLiteTransaction.Commit();
_sqLiteTransaction.Dispose();
_sqLiteConnection.Close();
_sqLiteConnection.Dispose();
} /// <summary>
/// Single Param Constructor for specifying advanced connection options.
/// </summary>
/// <param name="connectionOpts">A dictionary containing all desired options and their values</param>
public SQLiteDatabase(Dictionary<String, String> connectionOpts)
{
String str = connectionOpts.Aggregate("", (current, row) => current + String.Format("{0}={1}; ", row.Key, row.Value));
str = str.Trim().Substring(, str.Length - );
DBConnection = str;
} /// <summary>
/// Allows the programmer to create new database file.
/// </summary>
/// <param name="filePath">Full path of a new database file.</param>
/// <returns>true or false to represent success or failure.</returns>
public static bool CreateDB(string filePath)
{
try
{
SQLiteConnection.CreateFile(filePath);
return true;
}
catch (Exception e)
{
MessageBox.Show(e.Message, e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
} /// <summary>
/// Allows the programmer to run a query against the Database.
/// </summary>
/// <param name="sql">The SQL to run</param>
/// <param name="allowDBNullColumns">Allow null value for columns in this collection.</param>
/// <returns>A DataTable containing the result set.</returns>
public DataTable GetDataTable(string sql, IEnumerable<string> allowDBNullColumns = null)
{
var dt = new DataTable();
if (allowDBNullColumns != null)
foreach (var s in allowDBNullColumns)
{
dt.Columns.Add(s);
dt.Columns[s].AllowDBNull = true;
}
try
{
var cnn = new SQLiteConnection(DBConnection);
cnn.Open();
var mycommand = new SQLiteCommand(cnn) {CommandText = sql};
var reader = mycommand.ExecuteReader();
dt.Load(reader);
reader.Close();
cnn.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return dt;
} public string RetrieveOriginal(string value)
{
return
value.Replace("&amp;", "&").Replace("&lt;", "<").Replace("&gt;", "<").Replace("&quot;", "\"").Replace(
"&apos;", "'");
} /// <summary>
/// Allows the programmer to interact with the database for purposes other than a query.
/// </summary>
/// <param name="sql">The SQL to be run.</param>
/// <returns>An Integer containing the number of rows updated.</returns>
public int ExecuteNonQuery(string sql)
{
if (!_transaction)
{
var cnn = new SQLiteConnection(DBConnection);
cnn.Open();
var mycommand = new SQLiteCommand(cnn) {CommandText = sql};
var rowsUpdated = mycommand.ExecuteNonQuery();
cnn.Close();
return rowsUpdated;
}
else
{
var mycommand = new SQLiteCommand(_sqLiteConnection) { CommandText = sql };
return mycommand.ExecuteNonQuery();
}
} /// <summary>
/// Allows the programmer to retrieve single items from the DB.
/// </summary>
/// <param name="sql">The query to run.</param>
/// <returns>A string.</returns>
public string ExecuteScalar(string sql)
{
if (!_transaction)
{
var cnn = new SQLiteConnection(DBConnection);
cnn.Open();
var mycommand = new SQLiteCommand(cnn) {CommandText = sql};
var value = mycommand.ExecuteScalar();
cnn.Close();
return value != null ? value.ToString() : "";
}
else
{
var sqLiteCommand = new SQLiteCommand(_sqLiteConnection) { CommandText = sql };
var value = sqLiteCommand.ExecuteScalar();
return value != null ? value.ToString() : "";
}
} /// <summary>
/// Allows the programmer to easily update rows in the DB.
/// </summary>
/// <param name="tableName">The table to update.</param>
/// <param name="data">A dictionary containing Column names and their new values.</param>
/// <param name="where">The where clause for the update statement.</param>
/// <returns>A boolean true or false to signify success or failure.</returns>
public bool Update(String tableName, Dictionary<String, String> data, String where)
{
String vals = "";
Boolean returnCode = true;
if (data.Count >= )
{
vals = data.Aggregate(vals, (current, val) => current + String.Format(" {0} = '{1}',", val.Key.ToString(CultureInfo.InvariantCulture), val.Value.ToString(CultureInfo.InvariantCulture)));
vals = vals.Substring(, vals.Length - );
}
try
{
ExecuteNonQuery(String.Format("update {0} set {1} where {2};", tableName, vals, where));
}
catch
{
returnCode = false;
}
return returnCode;
} /// <summary>
/// Allows the programmer to easily delete rows from the DB.
/// </summary>
/// <param name="tableName">The table from which to delete.</param>
/// <param name="where">The where clause for the delete.</param>
/// <returns>A boolean true or false to signify success or failure.</returns>
public bool Delete(String tableName, String where)
{
Boolean returnCode = true;
try
{
ExecuteNonQuery(String.Format("delete from {0} where {1};", tableName, where));
}
catch (Exception fail)
{
MessageBox.Show(fail.Message, fail.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
returnCode = false;
}
return returnCode;
} /// <summary>
/// Allows the programmer to easily insert into the DB
/// </summary>
/// <param name="tableName">The table into which we insert the data.</param>
/// <param name="data">A dictionary containing the column names and data for the insert.</param>
/// <returns>returns last inserted row id if it's value is zero than it means failure.</returns>
public long Insert(String tableName, Dictionary<String, String> data)
{
String columns = "";
String values = "";
String value;
foreach (KeyValuePair<String, String> val in data)
{
columns += String.Format(" {0},", val.Key.ToString(CultureInfo.InvariantCulture));
values += String.Format(" '{0}',", val.Value);
}
columns = columns.Substring(, columns.Length - );
values = values.Substring(, values.Length - );
try
{
if (!_transaction)
{
var cnn = new SQLiteConnection(DBConnection);
cnn.Open();
var sqLiteCommand = new SQLiteCommand(cnn)
{
CommandText =
String.Format("insert into {0}({1}) values({2});", tableName, columns,
values)
};
sqLiteCommand.ExecuteNonQuery();
sqLiteCommand = new SQLiteCommand(cnn) { CommandText = "SELECT last_insert_rowid()" };
value = sqLiteCommand.ExecuteScalar().ToString();
}
else
{
ExecuteNonQuery(String.Format("insert into {0}({1}) values({2});", tableName, columns, values));
value = ExecuteScalar("SELECT last_insert_rowid()");
}
}
catch (Exception fail)
{
MessageBox.Show(fail.Message, fail.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
return ;
}
return long.Parse(value);
} /// <summary>
/// Allows the programmer to easily delete all data from the DB.
/// </summary>
/// <returns>A boolean true or false to signify success or failure.</returns>
public bool ClearDB()
{
try
{
var tables = GetDataTable("select NAME from SQLITE_MASTER where type='table' order by NAME;");
foreach (DataRow table in tables.Rows)
{
ClearTable(table["NAME"].ToString());
}
return true;
}
catch
{
return false;
}
} /// <summary>
/// Allows the user to easily clear all data from a specific table.
/// </summary>
/// <param name="table">The name of the table to clear.</param>
/// <returns>A boolean true or false to signify success or failure.</returns>
public bool ClearTable(String table)
{
try
{
ExecuteNonQuery(String.Format("delete from {0};", table));
return true;
}
catch
{
return false;
}
} /// <summary>
/// Allows the user to easily reduce size of database.
/// </summary>
/// <returns>A boolean true or false to signify success or failure.</returns>
public bool CompactDB()
{
try
{
ExecuteNonQuery("Vacuum;");
return true;
}
catch (Exception)
{
return false;
}
}
}
}

参考2:

关于如何正确使用System.Data.SQLite提高性能

SQLite .NET performance, how to speed up things?

You definitely need a transaction. If you don't, SQLite starts its own transaction for every insert command so you're effectively doing 86000 transactions as is.

It looks you're also opening and closing the connection each time, along with resetting the CommandText each time. This is unnecessary and doubtless slowing you down, it'll go much faster if you:

  • Open the connection once
  • Build the command once , adding the parameters to it once.
  • Start the transaction
  • Loop through, changing the parameter values only before calling ExecuteNonQuery
  • Commit the transaction.
  • Close the connection.

I think you could reduce your 20 minutes down to just a few seconds this way.

Edit: this is what I mean:

 public void InsertItems()
{
SQLiteConnection connection = new SQLiteConnection(SomeConnectionString);
SQLiteCommand command = connection.CreateCommand();
SQLiteTransaction transaction = connection.BeginTransaction(); command.CommandText = "INSERT OR IGNORE INTO Result "
+ "(RunTag, TopicId, DocumentNumber, Rank, Score) " +
"VALUES (@RunTag, @TopicId, @DocumentNumber, @Rank, @Score)"; command.Parameters.AddWithValue("@RunTag", "");
command.Parameters.AddWithValue("@TopicId", "");
command.Parameters.AddWithValue("@DocumentNumber", "");
command.Parameters.AddWithValue("@Rank", "");
command.Parameters.AddWithValue("@Score", ""); foreach ( /* item to loop through and add to db */ )
{
InsertResultItem(runTag, topicId, documentNumber, rank, score, command);
} transaction.Commit();
command.Dispose();
connection.Dispose();
} public int InsertResultItem(string runTag, int topicId, string documentNumber, int rank, double score, SQLiteCommand command)
{
command.Parameters["@RunTag"].Value = runTag;
command.Parameters["@TopicId"].Value = topicId;
command.Parameters["@DocumentNumber"].Value = documentNumber;
command.Parameters["@Rank"].Value = rank;
command.Parameters["@Score"].Value = score;
return command.ExecuteNonQuery();
}

It only uses one connection, one transaction and one command, so all you're changing is the parameter values each time.

我自己实现的一个简易版本:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite; namespace Utility
{
/*Multithread safety: No!*/
public class SQLiteDB
{
private SQLiteConnection m_db_conn;
private SQLiteTransaction m_transaction;
private bool m_is_transaction;
private SQLiteCommand m_transaction_cmd; public SQLiteDB()
{
m_is_transaction = false;
} public SQLiteDB(string db_path)
{
m_is_transaction = false;
m_db_conn = new SQLiteConnection(string.Format("Data Source={0}", db_path));
m_db_conn.Open();
}
public void Open(string db_path)
{
m_db_conn = new SQLiteConnection(string.Format("Data Source={0}", db_path));
m_db_conn.Open();
}
public void Close()
{
if (m_db_conn.State != System.Data.ConnectionState.Closed)
{
m_db_conn.Close();
m_db_conn.Dispose();
}
if (null != m_transaction_cmd)
{
m_transaction_cmd.Dispose();
}
} public void BeginTransaction()
{
m_is_transaction = true;
m_transaction_cmd = m_db_conn.CreateCommand();
m_transaction = m_db_conn.BeginTransaction();
} public void Commit()
{
if (m_is_transaction)
{
m_transaction.Commit();
m_transaction_cmd.Dispose();
m_is_transaction = false;
}
} public void Rollback()
{
if (m_is_transaction)
{
m_transaction.Rollback();
m_transaction_cmd.Dispose();
m_is_transaction = false;
}
} public int ExecuteNonQuery(string non_query_sql)
{
if (!m_is_transaction)
{
SQLiteCommand sql_cmd = new SQLiteCommand(m_db_conn);
sql_cmd.CommandText = non_query_sql;
return sql_cmd.ExecuteNonQuery();
}
else
{
m_transaction_cmd.CommandText = non_query_sql;
return m_transaction_cmd.ExecuteNonQuery();
}
}
}
}

C# 封装 System.Data.SQLite的更多相关文章

  1. System.Data.SQLite数据库简介

    SQLite介绍 在介绍System.Data.SQLite之前需要介绍一下SQLite,SQLite是一个类似于Access的单机版数据库管理系统,它将所有数据库的定义(包括定义.表.索引和数据本身 ...

  2. SQLite 之 C#版 System.Data.SQLite 使用

    简介 SQLite简介 SQLite,是一款轻型的关系型数据库.它的设计目标是嵌入式. 它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 C++.C ...

  3. 启用SQLite的Data Provider 运行WECOMPANYSITE时遇到ERROR CREATING CONTEXT 'SPRING.ROOT': ERROR THROWN BY A DEPENDENCY OF OBJECT 'SYSTEM.DATA.SQLITE'

    从网上下载的源码WeCompanySite,运行时报错 Error creating context 'spring.root': Error thrown by a dependency of ob ...

  4. .Net4.0以上使用System.Data.Sqlite

    最近对Sqlite感兴趣,就尝试了一下用c#连接,我用的版本是vs2013,默认开发环境是.net4.5,,按照网上的教材,下载了System.Data.Sqlite,然后写了下面这个简单的测试代码, ...

  5. IIS发布网站出现“未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项。”的解决方法

    未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序.              说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈 ...

  6. 引用64位dll时候出现 未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项。试图加载格式不正确的程序。

    引用64位dll时候出现 未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序. 需要在web.config增加配置 <startup use ...

  7. Could not load file or assembly 'System.Data.SQLite' or one of its dependencies

    试图加载格式不正确的程 异常类型 异常消息Could not load file or assembly 'System.Data.SQLite' or one of its dependencies ...

  8. System.Data.SQLite

    SQLite介绍 在介绍System.Data.SQLite之前需要介绍一下SQLite,SQLite是一个类似于Access的单机版数据库管理系统,它将所有数据库的定义(包括定义.表.索引和数据本身 ...

  9. 未能加载文件或程序集“System.Data.SQLite.DLL”或它的某一个依赖项

    今天在部署code到测试环境的时候 出现了未能加载文件或程序集"System.Data.SQLite.DLL"或它的某一个依赖项 这个错误,其实错误的的原因有很多,1.典型的是是版 ...

随机推荐

  1. 数据库监控[Z]

    --查看表锁 select * from sys.v_$sqlarea where disk_reads>100    --监控事例的等待 select event,sum(decode(wai ...

  2. UIImageView~动画播放的内存优化

    我目前学到的知识,播放动画的步骤就是下面的几个步骤,把照片资源放到数组里面,通过动画animationImage加载数组,设置动画播放的 时间和次数完成播放. 后来通过看一些视频了解到:当需要播放多个 ...

  3. php不区分大小写

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Spring-----6、Spring3.0提供的Java配置管理

    转载自:http://blog.csdn.net/hekewangzi/article/details/45646279

  5. Q - 密码(第二季水)

    Description 网上流传一句话:"常在网上飘啊,哪能不挨刀啊-".其实要想能安安心心地上网其实也不难,学点安全知识就可以.         首先,我们就要设置一个安全的密码 ...

  6. JQuery中如何动态修改input的type属性

    代码如下: jQuery(".member_id").focus(function() { jQuery(this).val(''); }).blur(function() { i ...

  7. Python学习笔记(六)Python的列表生成式、生成器

    列表生成式 List Comprehensions 列表生成式是Python内置的非常简单却强大的可以用来创建list的生成式. 简单的数值范围的list可以使用一下方式生成: >>> ...

  8. Ubuntu配置OpenGL环境

    建立基本编译环境 sudo apt-get install build-essential 安装OpenGL Library sudo apt-get install libgl1-mesa-dev ...

  9. Qt5-MSVC2012-qDebug中文乱码解决方法

    1.首先看代码: int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); qDeb ...

  10. ASCII、ANSI、GB2312、Unicode、UTF-8之间的关系

    1.ASCII码: ASCII(American Standard Code for Information Interchange,美国信息互换标准代码)是基于拉丁字母的一套电脑编码系统.它主要用于 ...