C# 封装 System.Data.SQLite
参考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("&", "&").Replace("<", "<").Replace(">", "<").Replace(""", "\"").Replace(
"'", "'");
} /// <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的更多相关文章
- System.Data.SQLite数据库简介
SQLite介绍 在介绍System.Data.SQLite之前需要介绍一下SQLite,SQLite是一个类似于Access的单机版数据库管理系统,它将所有数据库的定义(包括定义.表.索引和数据本身 ...
- SQLite 之 C#版 System.Data.SQLite 使用
简介 SQLite简介 SQLite,是一款轻型的关系型数据库.它的设计目标是嵌入式. 它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 C++.C ...
- 启用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 ...
- .Net4.0以上使用System.Data.Sqlite
最近对Sqlite感兴趣,就尝试了一下用c#连接,我用的版本是vs2013,默认开发环境是.net4.5,,按照网上的教材,下载了System.Data.Sqlite,然后写了下面这个简单的测试代码, ...
- IIS发布网站出现“未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项。”的解决方法
未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈 ...
- 引用64位dll时候出现 未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项。试图加载格式不正确的程序。
引用64位dll时候出现 未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序. 需要在web.config增加配置 <startup use ...
- 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 ...
- System.Data.SQLite
SQLite介绍 在介绍System.Data.SQLite之前需要介绍一下SQLite,SQLite是一个类似于Access的单机版数据库管理系统,它将所有数据库的定义(包括定义.表.索引和数据本身 ...
- 未能加载文件或程序集“System.Data.SQLite.DLL”或它的某一个依赖项
今天在部署code到测试环境的时候 出现了未能加载文件或程序集"System.Data.SQLite.DLL"或它的某一个依赖项 这个错误,其实错误的的原因有很多,1.典型的是是版 ...
随机推荐
- c语言字符串比较函数strcmp
strcmp(s1,s2) 说明: 当s1<s2时,返回值<0 当s1=s2时,返回值=0 当s1>s2时,返回值>0两个字符串自左向右逐个字符相比(按ASCII值大小相比较) ...
- VB.NET Shared(共享)和 Static(静态)关键字的区别
共享成员(Shared): VB.NET现在是支持真正的面向对象编程,可以继承.使用多态.共享成员 和静态成员. 共享成员就是在所有类和所定义派生类的实例之间共享的方法.属 性.字段和事件.所有使用类 ...
- [Effective Modern C++] Item 2. Understand auto type deduction - 了解auto类型推断
条款二 了解auto类型推断 基础知识 除了一处例外,auto的类型推断与template一样.存在一个直接的从template类型推断到auto类型推断的映射 三类情况下的推断如下所示: // ca ...
- $ cd `dirname $0` 和PWD%/* shell变量的一些特殊用法
在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的".". $0:当前Shell程序的文件名dirname $0,获取当前Shell程序 ...
- 【转】mysql行列转换方法总结
转:http://blog.chinaunix.net/uid-7692530-id-2567582.html 在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解 ...
- Android 6.0权限问题
Android 6.0 open failed: EACCES (Permission denied) 对于6.0+权限问题,报错如上: 解决方案: Android 6.0 (Marshmallow) ...
- strace排除Linux服务器故障
strace是一个有用的小工具 – 大多数Linux系统默认已经安装 – 可以通过跟踪系统调用来让你知道一个程序在后台所做的事情.Strace是一个基础的调试工具;但是即便你不是在跟踪一个问题的时候它 ...
- NGINX+PHP+MYSQL服务器环境搭建
这条命令是配置vim的,请确保你能访问github wget -qO- https://raw.github.com/ma6174/vim/master/setup.sh | sh 说明有一些小问题, ...
- MySQL数据库主从同步安装与配置总结
MySQL的主从同步是一个很成熟的架构,优点为: ①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力: ②在从主服务器进行备份,避免备份期间影响主服务器服务: ③当主服务器出现问题时 ...
- javascript的stringFormat函数实现
写一个简单的stringFormat来给自己用 function stringFormat(format, args) { var formatData; if (arguments.length = ...