最近做项目用到了SQLite数据库,就自己写了个SQLite帮助类,类似于SQLHelper。
不过是按照我常用方式写的,主要与SQLHelper不同的是
1、这个帮助类并没有内置ConnectionString,是需要在调用方法的时候指定的,这样的好处的是:在一般的三层架构时都会在Helper里指定一个数据库连接,但是如果我又想用这个帮助类但是我想查询其他数据库的时候就无法使用了。
2、PrepareCommand这个方法我也修改了下,由于增删改查4个方法都用到这个方法来减少重复代码,但是我目前就在插入和更新用到事务操作,查询没有用到,但不知道这样写到底对不对,哪位看了可以帮忙给出个了比较好的解决方法?
3、由于SQLite内置了limit,给我们分页提供了很大的便利,所以我在这个帮助类里面也自己封装了分页方法。
不多说了,附代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
namespace Tools.Data
{
///
/// 本类为SQLite数据库帮助静态类,使用时只需直接调用即可,无需实例化
///
public static class SQLiteHelper
{
#region ExecuteNonQuery
///
/// 执行数据库操作(新增、更新或删除)
///
/// 连接字符串
/// SqlCommand对象
/// 所受影响的行数
public static int ExecuteNonQuery(string connectionString, SQLiteCommand cmd)
{
int result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, cmd.CommandType, cmd.CommandText);
try
{
result = cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
///
/// 执行数据库操作(新增、更新或删除)
///
/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// 所受影响的行数
public static int ExecuteNonQuery(string connectionString, string commandText, CommandType commandType)
{
int result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, commandType, commandText);
try
{
result = cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
///
/// 执行数据库操作(新增、更新或删除)
///
/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// SQL参数对象
/// 所受影响的行数
public static int ExecuteNonQuery(string connectionString, string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
{
int result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, commandType, commandText);
try
{
result = cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
#endregion
#region ExecuteScalar
///
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
///
/// 连接字符串
/// SqlCommand对象
/// 查询所得的第1行第1列数据
public static object ExecuteScalar(string connectionString, SQLiteCommand cmd)
{
object result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, cmd.CommandType, cmd.CommandText);
try
{
result = cmd.ExecuteScalar();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
///
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
///
/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// 查询所得的第1行第1列数据
public static object ExecuteScalar(string connectionString, string commandText, CommandType commandType)
{
object result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, commandType, commandText);
try
{
result = cmd.ExecuteScalar();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
///
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
///
/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// SQL参数对象
/// 查询所得的第1行第1列数据
public static object ExecuteScalar(string connectionString, string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
{
object result = 0;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
SQLiteCommand cmd = new SQLiteCommand();
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, true, commandType, commandText);
try
{
result = cmd.ExecuteScalar();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
#endregion
#region ExecuteReader
///
/// 执行数据库查询,返回SqlDataReader对象
///
/// 连接字符串
/// SqlCommand对象
/// SqlDataReader对象
public static DbDataReader ExecuteReader(string connectionString, SQLiteCommand cmd)
{
DbDataReader reader = null;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
SQLiteConnection con = new SQLiteConnection(connectionString);
SQLiteTransaction trans = null;
PrepareCommand(cmd, con,ref trans, false, cmd.CommandType, cmd.CommandText);
try
{
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw ex;
}
return reader;
}
///
/// 执行数据库查询,返回SqlDataReader对象
///
/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// SqlDataReader对象
public static DbDataReader ExecuteReader(string connectionString, string commandText, CommandType commandType)
{
DbDataReader reader = null;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
SQLiteConnection con = new SQLiteConnection(connectionString);
SQLiteCommand cmd = new SQLiteCommand();
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, false, commandType, commandText);
try
{
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw ex;
}
return reader;
}
///
/// 执行数据库查询,返回SqlDataReader对象
///
/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// SQL参数对象
/// SqlDataReader对象
public static DbDataReader ExecuteReader(string connectionString, string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
{
DbDataReader reader = null;
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
SQLiteConnection con = new SQLiteConnection(connectionString);
SQLiteCommand cmd = new SQLiteCommand();
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, false, commandType, commandText, cmdParms);
try
{
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw ex;
}
return reader;
}
#endregion
#region ExecuteDataSet
///
/// 执行数据库查询,返回DataSet对象
///
/// 连接字符串
/// SqlCommand对象
/// DataSet对象
public static DataSet ExecuteDataSet(string connectionString, SQLiteCommand cmd)
{
DataSet ds = new DataSet();
SQLiteConnection con = new SQLiteConnection(connectionString);
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, false, cmd.CommandType, cmd.CommandText);
try
{
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
sda.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cmd.Connection != null)
{
if (cmd.Connection.State == ConnectionState.Open)
{
cmd.Connection.Close();
}
}
}
return ds;
}
///
/// 执行数据库查询,返回DataSet对象
///
/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// DataSet对象
public static DataSet ExecuteDataSet(string connectionString, string commandText, CommandType commandType)
{
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
DataSet ds = new DataSet();
SQLiteConnection con = new SQLiteConnection(connectionString);
SQLiteCommand cmd = new SQLiteCommand();
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, false, commandType, commandText);
try
{
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
sda.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (con != null)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
return ds;
}
///
/// 执行数据库查询,返回DataSet对象
///
/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// SQL参数对象
/// DataSet对象
public static DataSet ExecuteDataSet(string connectionString, string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
{
if (connectionString == null || connectionString.Length == 0)
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0)
throw new ArgumentNullException("commandText");
DataSet ds = new DataSet();
SQLiteConnection con = new SQLiteConnection(connectionString);
SQLiteCommand cmd = new SQLiteCommand();
SQLiteTransaction trans = null;
PrepareCommand(cmd, con, ref trans, false, commandType, commandText, cmdParms);
try
{
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
sda.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (con != null)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
return ds;
}
#endregion
///
/// 通用分页查询方法
///
/// 连接字符串
/// 表名
/// 查询字段名
/// where条件
/// 排序条件
/// 每页数据数量
/// 当前页数
/// 数据总量
/// DataTable数据表
public static DataTable SelectPaging(string connString, string tableName, string strColumns, string strWhere, string strOrder, int pageSize, int currentIndex, out int recordOut)
{
DataTable dt = new DataTable();
recordOut = Convert.ToInt32(ExecuteScalar(connString, "select count(*) from " + tableName, CommandType.Text));
string pagingTemplate = "select {0} from {1} where {2} order by {3} limit {4} offset {5} ";
int offsetCount = (currentIndex - 1) * pageSize;
string commandText = String.Format(pagingTemplate, strColumns, tableName, strWhere, strOrder, pageSize.ToString(), offsetCount.ToString());
using (DbDataReader reader = ExecuteReader(connString, commandText, CommandType.Text))
{
if (reader != null)
{
dt.Load(reader);
}
}
return dt;
}
///
/// 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化
///
/// Command对象
/// Connection对象
/// Transcation对象
/// 是否使用事务
/// SQL字符串执行类型
/// SQL Text
/// SQLiteParameters to use in the command
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, ref SQLiteTransaction trans, bool useTrans, CommandType cmdType, string cmdText, params SQLiteParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (useTrans)
{
trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
cmd.Transaction = trans;
}
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SQLiteParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
}
当然你也可以在这里下载:点我下载
PS:大家如果觉得好帮忙点下推荐,谢谢大家了!