仿SqlHelper的Sqlite数据库操作类SqlHelper
Code
using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Collections;
using System.Collections.Generic;
namespace Aoner
{
/// <summary>
/// AccHelper类提供很高的数据访问性能,
/// 使用SQLite类的通用定义.
/// </summary>
public abstract class SqliteHelper
{
//定义数据库连接串
public static string connStr = ConfigurationManager.AppSettings["SqliteConnection"];
// 存贮Cache缓存的Hashtable集合
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
/// <summary>
/// 使用连接字符串,执行一个SQLiteCommand命令(没有记录返回)
/// 使用提供的参数集.
/// </summary>
/// <remarks>
/// 示例:int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的SQLiteConnection连接串</param>
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>受此命令影响的行数</returns>
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
{
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection conn = new SQLiteConnection(connStr))
{
//通过PrepareCommand方法将参数逐个加入到SQLiteCommand的参数集合中
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
//清空SQLiteCommand中的参数列表
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 在一个存在的连接上执行数据库的命令操作
/// 使用提供的参数集.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connection, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="conn">一个存在的数据库连接对象</param>
/// <param name="commandType">命令类型CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>受此命令影响的行数</returns>
public static int ExecuteNonQuery(SQLiteConnection connection, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
{
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 在一个事务的连接上执行数据库的命令操作
/// 使用提供的参数集.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="trans">一个存在的事务</param>
/// <param name="commandType">命令类型CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>受此命令影响的行数</returns>
public static int ExecuteNonQuery(SQLiteTransaction trans, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
{
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 在一个连接串上执行一个命令,返回一个SQLiteDataReader对象
/// 使用提供的参数.
/// </summary>
/// <remarks>
/// e.g.:
/// SQLiteDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的SQLiteConnection连接串(改了参数名称)</param>
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>一个结果集对象SQLiteDataReader</returns>
public static SQLiteDataReader ExecuteReader(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
{
SQLiteCommand cmd = new SQLiteCommand();
SQLiteConnection conn = new SQLiteConnection(connStr);
// 如果不存在要查询的对象,则发生异常
// 连接要关闭
// CommandBehavior.CloseConnection在异常时不发生作用
try{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SQLiteDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch{
conn.Close();
throw;
}
}
/// <summary>
/// 在一个连接串上执行一个命令,返回一个SQLiteDataAdapter对象
/// 使用提供的参数.
/// </summary>
/// <remarks>
/// e.g.:
/// SQLiteDataReader r = ExecuteAdapter(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的SQLiteConnection连接串(改了参数名称)</param>
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>一个结果集对象SQLiteDataReader</returns>
public static SQLiteDataAdapter ExecuteAdapter(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters){
SQLiteConnection conn = new SQLiteConnection(connStr);
SQLiteCommand cmd = new SQLiteCommand();
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SQLiteDataAdapter adp = new SQLiteDataAdapter(cmd);
cmd.Parameters.Clear();
return adp;
}
catch
{
conn.Close();
throw;
}
finally
{
conn.Close();
}
}
public static SQLiteDataAdapter Adapter(CommandType cmdType, string cmdText, params SQLiteParameter[] cmdParms)
{
SQLiteConnection conn = new SQLiteConnection(connStr);
SQLiteDataAdapter adp = new SQLiteDataAdapter();
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParms);
adp.SelectCommand = cmd;
return adp;
}
/// <summary>
/// 在一个连接串上执行一个命令,返回表中第一行,第一列的值
/// 使用提供的参数.
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的SQLiteConnection连接串</param>
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>返回的对象,在使用时记得类型转换</returns>
public static object ExecuteScalar(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters){
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection connection = new SQLiteConnection(connStr)){
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 在一个连接上执行一个命令,返回表中第一行,第一列的值
/// 使用提供的参数.
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的SQLiteConnection连接</param>
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param> /// <returns>返回的对象,在使用时记得类型转换</returns>
public static object ExecuteScalar(SQLiteTransaction tran,CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
{
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection connection = new SQLiteConnection(connStr))
{
PrepareCommand(cmd, connection, tran, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 在缓存中添加参数数组
/// </summary>
/// <param name="cacheKey">参数的Key</param>
/// <param name="cmdParms">参数数组</param>
public static void CacheParameters(string cacheKey, params SQLiteParameter[] commandParameters){
parmCache[cacheKey] = commandParameters;
}
/// <summary>
/// 提取缓存的参数数组
/// </summary>
/// <param name="cacheKey">查找缓存的key</param>
/// <returns>返回被缓存的参数数组</returns>
public static SQLiteParameter[] GetCachedParameters(string cacheKey){
SQLiteParameter[] cachedParms = (SQLiteParameter[])parmCache[cacheKey];
if (cachedParms == null)
return null;
SQLiteParameter[] clonedParms = new SQLiteParameter[cachedParms.Length];
for (int i = 0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (SQLiteParameter)((ICloneable)cachedParms[i]).Clone();
return clonedParms;
}
/// <summary>
/// 提供一个SQLiteCommand对象的设置
/// </summary>
/// <param name="cmd">SQLiteCommand对象</param>
/// <param name="conn">SQLiteConnection 对象</param>
/// <param name="trans">SQLiteTransaction 对象</param>
/// <param name="cmdType">CommandType 如存贮过程,T-SQLite</param>
/// <param name="cmdText">存贮过程名或查询串</param>
/// <param name="cmdParms">命令中用到的参数集</param>
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, CommandType cmdType, string cmdText, SQLiteParameter[] cmdParms){
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null){
foreach (SQLiteParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
/// <summary>
/// 提供一个SQLiteCommand对象的设置
/// </summary>
/// <param name="cmd">SQLiteCommand对象</param>
/// <param name="conn">SQLiteConnection 对象</param>
/// <param name="trans">SQLiteTransaction 对象</param>
/// <param name="cmdType">CommandType 如存贮过程,T-SQLite</param>
/// <param name="cmdText">存贮过程名或查询串</param>
/// <param name="cmdParms">命令中用到的参数集</param>
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, CommandType cmdType, string cmdText, SQLiteParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SQLiteParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
}
using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Collections;
using System.Collections.Generic;
namespace Aoner
{
/// <summary>
/// AccHelper类提供很高的数据访问性能,
/// 使用SQLite类的通用定义.
/// </summary>
public abstract class SqliteHelper
{
//定义数据库连接串
public static string connStr = ConfigurationManager.AppSettings["SqliteConnection"];
// 存贮Cache缓存的Hashtable集合
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
/// <summary>
/// 使用连接字符串,执行一个SQLiteCommand命令(没有记录返回)
/// 使用提供的参数集.
/// </summary>
/// <remarks>
/// 示例:int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的SQLiteConnection连接串</param>
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>受此命令影响的行数</returns>
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
{
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection conn = new SQLiteConnection(connStr))
{
//通过PrepareCommand方法将参数逐个加入到SQLiteCommand的参数集合中
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
//清空SQLiteCommand中的参数列表
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 在一个存在的连接上执行数据库的命令操作
/// 使用提供的参数集.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connection, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="conn">一个存在的数据库连接对象</param>
/// <param name="commandType">命令类型CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>受此命令影响的行数</returns>
public static int ExecuteNonQuery(SQLiteConnection connection, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
{
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 在一个事务的连接上执行数据库的命令操作
/// 使用提供的参数集.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="trans">一个存在的事务</param>
/// <param name="commandType">命令类型CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>受此命令影响的行数</returns>
public static int ExecuteNonQuery(SQLiteTransaction trans, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
{
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 在一个连接串上执行一个命令,返回一个SQLiteDataReader对象
/// 使用提供的参数.
/// </summary>
/// <remarks>
/// e.g.:
/// SQLiteDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的SQLiteConnection连接串(改了参数名称)</param>
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>一个结果集对象SQLiteDataReader</returns>
public static SQLiteDataReader ExecuteReader(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
{
SQLiteCommand cmd = new SQLiteCommand();
SQLiteConnection conn = new SQLiteConnection(connStr);
// 如果不存在要查询的对象,则发生异常
// 连接要关闭
// CommandBehavior.CloseConnection在异常时不发生作用
try{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SQLiteDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch{
conn.Close();
throw;
}
}
/// <summary>
/// 在一个连接串上执行一个命令,返回一个SQLiteDataAdapter对象
/// 使用提供的参数.
/// </summary>
/// <remarks>
/// e.g.:
/// SQLiteDataReader r = ExecuteAdapter(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的SQLiteConnection连接串(改了参数名称)</param>
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>一个结果集对象SQLiteDataReader</returns>
public static SQLiteDataAdapter ExecuteAdapter(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters){
SQLiteConnection conn = new SQLiteConnection(connStr);
SQLiteCommand cmd = new SQLiteCommand();
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SQLiteDataAdapter adp = new SQLiteDataAdapter(cmd);
cmd.Parameters.Clear();
return adp;
}
catch
{
conn.Close();
throw;
}
finally
{
conn.Close();
}
}
public static SQLiteDataAdapter Adapter(CommandType cmdType, string cmdText, params SQLiteParameter[] cmdParms)
{
SQLiteConnection conn = new SQLiteConnection(connStr);
SQLiteDataAdapter adp = new SQLiteDataAdapter();
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParms);
adp.SelectCommand = cmd;
return adp;
}
/// <summary>
/// 在一个连接串上执行一个命令,返回表中第一行,第一列的值
/// 使用提供的参数.
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的SQLiteConnection连接串</param>
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param>
/// <returns>返回的对象,在使用时记得类型转换</returns>
public static object ExecuteScalar(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters){
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection connection = new SQLiteConnection(connStr)){
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 在一个连接上执行一个命令,返回表中第一行,第一列的值
/// 使用提供的参数.
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的SQLiteConnection连接</param>
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
/// <param name="commandText">存贮过程名称或是一个T-SQLite语句串</param>
/// <param name="commandParameters">执行命令的参数集</param> /// <returns>返回的对象,在使用时记得类型转换</returns>
public static object ExecuteScalar(SQLiteTransaction tran,CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
{
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection connection = new SQLiteConnection(connStr))
{
PrepareCommand(cmd, connection, tran, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 在缓存中添加参数数组
/// </summary>
/// <param name="cacheKey">参数的Key</param>
/// <param name="cmdParms">参数数组</param>
public static void CacheParameters(string cacheKey, params SQLiteParameter[] commandParameters){
parmCache[cacheKey] = commandParameters;
}
/// <summary>
/// 提取缓存的参数数组
/// </summary>
/// <param name="cacheKey">查找缓存的key</param>
/// <returns>返回被缓存的参数数组</returns>
public static SQLiteParameter[] GetCachedParameters(string cacheKey){
SQLiteParameter[] cachedParms = (SQLiteParameter[])parmCache[cacheKey];
if (cachedParms == null)
return null;
SQLiteParameter[] clonedParms = new SQLiteParameter[cachedParms.Length];
for (int i = 0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (SQLiteParameter)((ICloneable)cachedParms[i]).Clone();
return clonedParms;
}
/// <summary>
/// 提供一个SQLiteCommand对象的设置
/// </summary>
/// <param name="cmd">SQLiteCommand对象</param>
/// <param name="conn">SQLiteConnection 对象</param>
/// <param name="trans">SQLiteTransaction 对象</param>
/// <param name="cmdType">CommandType 如存贮过程,T-SQLite</param>
/// <param name="cmdText">存贮过程名或查询串</param>
/// <param name="cmdParms">命令中用到的参数集</param>
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, CommandType cmdType, string cmdText, SQLiteParameter[] cmdParms){
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null){
foreach (SQLiteParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
/// <summary>
/// 提供一个SQLiteCommand对象的设置
/// </summary>
/// <param name="cmd">SQLiteCommand对象</param>
/// <param name="conn">SQLiteConnection 对象</param>
/// <param name="trans">SQLiteTransaction 对象</param>
/// <param name="cmdType">CommandType 如存贮过程,T-SQLite</param>
/// <param name="cmdText">存贮过程名或查询串</param>
/// <param name="cmdParms">命令中用到的参数集</param>
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, CommandType cmdType, string cmdText, SQLiteParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SQLiteParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
}