///源码下载地址:http://download.csdn.net/detail/kehaigang29/8836171
///dll下载地址:http://download.csdn.net/detail/kehaigang29/8837257
/// <summary>
/// 本类为SQLite数据库帮助静态类,使用时只需直接调用即可,无需实例化
/// </summary>
public static class SQLiteHelper
{
/// <summary>
/// 数据库连接字符串
/// </summary>
public static string connectionString = "Data Source=" + Application.StartupPath + "\\" + System.Configuration.ConfigurationSettings.AppSettings["Contr"]; #region 执行数据库操作(新增、更新或删除),返回影响行数
/// <summary>
/// 执行数据库操作(新增、更新或删除)
/// </summary>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>所受影响的行数</returns>
public static int ExecuteNonQuery(SQLiteCommand cmd)
{
int result = ;
if (connectionString == null || connectionString.Length == )
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;
} /// <summary>
/// 执行数据库操作(新增、更新或删除)
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <returns>所受影响的行数</returns>
public static int ExecuteNonQuery(string commandText, CommandType commandType = CommandType.Text)
{
int result = ;
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
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;
} /// <summary>
/// 执行数据库操作(新增、更新或删除)
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <param name="cmdParms">SQL参数对象</param>
/// <returns>所受影响的行数</returns>
public static int ExecuteNonQuery(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms)
{
int result = ;
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
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,cmdParms);
try
{
result = cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
#endregion #region 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
/// <summary>
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
/// </summary>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>查询所得的第1行第1列数据</returns>
public static object ExecuteScalar(SQLiteCommand cmd)
{
object result = ;
if (connectionString == null || connectionString.Length == )
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;
} /// <summary>
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <returns>查询所得的第1行第1列数据</returns>
public static object ExecuteScalar(string commandText, CommandType commandType = CommandType.Text)
{
object result = ;
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
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;
} /// <summary>
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <param name="cmdParms">SQL参数对象</param>
/// <returns>查询所得的第1行第1列数据</returns>
public static object ExecuteScalar(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms)
{
object result = ;
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
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, cmdParms);
try
{
result = cmd.ExecuteScalar();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
#endregion #region 执行数据库查询,返回SqlDataReader对象
/// <summary>
/// 执行数据库查询,返回SqlDataReader对象
/// </summary>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>SqlDataReader对象</returns>
public static DbDataReader ExecuteReader(SQLiteCommand cmd)
{
DbDataReader reader = null;
if (connectionString == null || connectionString.Length == )
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;
} /// <summary>
/// 执行数据库查询,返回SqlDataReader对象
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <returns>SqlDataReader对象</returns>
public static DbDataReader ExecuteReader(string commandText, CommandType commandType = CommandType.Text)
{
DbDataReader reader = null;
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
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;
} /// <summary>
/// 执行数据库查询,返回SqlDataReader对象
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <param name="cmdParms">SQL参数对象</param>
/// <returns>SqlDataReader对象</returns>
public static DbDataReader ExecuteReader(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms)
{
DbDataReader reader = null;
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
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 执行数据库查询,返回DataSet对象
/// <summary>
/// 执行数据库查询,返回DataSet对象
/// </summary>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>DataSet对象</returns>
public static DataSet ExecuteDataSet(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;
} /// <summary>
/// 执行数据库查询,返回DataSet对象
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <returns>DataSet对象</returns>
public static DataSet ExecuteDataSet(string commandText, CommandType commandType = CommandType.Text)
{
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
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;
} /// <summary>
/// 执行数据库查询,返回DataSet对象
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <param name="cmdParms">SQL参数对象</param>
/// <returns>DataSet对象</returns>
public static DataSet ExecuteDataSet(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms)
{
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
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 #region 执行数据库查询,返回DataTable对象
/// <summary>
/// 执行数据库查询,返回DataTable对象
/// </summary>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型(默认语句)</param>
/// <returns>DataTable对象</returns>
public static DataTable ExecuteDataTable(string commandText, CommandType commandType = CommandType.Text)
{
if (connectionString == null || connectionString.Length == )
throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == )
throw new ArgumentNullException("commandText");
DataTable dt = new DataTable();
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(dt);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (con != null)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
return dt;
} #endregion #region 通用分页查询方法
/// <summary>
/// 通用分页查询方法
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="strColumns">查询字段名</param>
/// <param name="strWhere">where条件</param>
/// <param name="strOrder">排序条件</param>
/// <param name="pageSize">每页数据数量</param>
/// <param name="currentIndex">当前页数</param>
/// <param name="recordOut">数据总量</param>
/// <returns>DataTable数据表</returns>
public static DataTable SelectPaging(string tableName, string strColumns, string strWhere, string strOrder, int pageSize, int currentIndex, out int recordOut)
{
DataTable dt = new DataTable();
recordOut = Convert.ToInt32(ExecuteScalar("select count(*) from " + tableName, CommandType.Text));
string pagingTemplate = "select {0} from {1} where {2} order by {3} limit {4} offset {5} ";
int offsetCount = (currentIndex - ) * pageSize;
string commandText = String.Format(pagingTemplate, strColumns, tableName, strWhere, strOrder, pageSize.ToString(), offsetCount.ToString());
using (DbDataReader reader = ExecuteReader(commandText, CommandType.Text))
{
if (reader != null)
{
dt.Load(reader);
}
}
return dt;
} #endregion #region 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化
/// <summary>
/// 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化
/// </summary>
/// <param name="cmd">Command对象</param>
/// <param name="conn">Connection对象</param>
/// <param name="trans">Transcation对象</param>
/// <param name="useTrans">是否使用事务</param>
/// <param name="cmdType">SQL字符串执行类型</param>
/// <param name="cmdText">SQL Text</param>
/// <param name="cmdParms">SQLiteParameters to use in the command</param>
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);
}
} #endregion }

Sqlite 帮助类 SQLiteHelper的更多相关文章

  1. SQLite帮助类SQlitehelper 实现对SQLite数据的增删改查

    public class SQLiteHelper { public const string sConn = "Data Source=" + @"path" ...

  2. PHP基于PDO实现的SQLite操作类

    <?php // sqlite分页类 class SqliteDB{ public function __construct(){ // 初始化数据库,并且连接数据库 数据库配置 $this-& ...

  3. Android Sqlite 工具类封装

    鉴于经常使用 Sqlite 数据库做数据持久化处理,进行了一点封装,方便使用. 该封装类主要支持一下功能 支持多用户数据储存 支持 Sqlite数据库升级 支持传入 Sql 语句建表 支持 SQLit ...

  4. SQLite存储类(数据类型)

    SQLite数据类型更普遍,采用动态类型系统. 说是数据类型,更像是存储类,如:INTEGER存储类就包含多种不同长度的整数数据类型 [INTEGER]带符号的整数类型 [REAL]浮点值,小数类型 ...

  5. c# Sqlite帮助类

    最近有WPF做客户端,需要离线操作存储数据,在项目中考虑使用Sqlite嵌入式数据库,在网上找了不少资料,最终整理出一个公共的帮助类. Sqlite是一个非常小巧的数据库,基本上具备关系型数据库操作的 ...

  6. sqlite帮助类

    帮助类 using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; usi ...

  7. Android SQLITE 操作工具类

    首先创建一个类 DatabaseHelper 继承SQLiteOpenHelper帮助类,定义数据库版本,数据库名称,创建表名. private static final int DATABASE_V ...

  8. php封装的sqlite操作类

    sqlite在php中是默认安装的本地小型化数据库,类似于xml的小型数据库,但sqlite功能更强. sqlite.class.php文件: <?php class sqliteDB{ pri ...

  9. 封装Qt的SQLite接口类

    还没测试完善.. #ifndef SQLITE_H #define SQLITE_H #include <QSqlDatabase> #include <QSqlQuery> ...

随机推荐

  1. 如何让搜索引擎抓取AJAX内容? 转

    越来越多的网站,开始采用"单页面结构"(Single-page application). 整个网站只有一张网页,采用 Ajax 技术,根据用户的输入,加载不同的内容. 这种做法的 ...

  2. Android px、dp和sp单位区别

    [转]http://www.cnblogs.com/bjzhanghao/archive/2012/11/06/2757300.html 在调整布局的时候,某些控件的大小需要设置单位: px: 即像素 ...

  3. java制作二维码的两种方式

    原博:http://www.importnew.com/15028.html Zebra Crossing(ZXing)是一个很棒的,几乎可以在所有平台(Android.JavaSE.iPhone.R ...

  4. linux mint运行docker

    1,sudo apt-get install docker.io 或者sudo apt-get install docker* 2,安装好之后 sudo docker -d 启动进程提示: yimiy ...

  5. dev repositoryItem 手工定义

    一.打开设计界面 二.定义Repository 事件定义 三.把repositoryItemTextEdit1邦定存在的列

  6. Mybatis学习(7)spring和mybatis整合

    整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...

  7. YII 1.0 上传文件

    $upload = CUploadedFile::getInstance($articleModel,'thumb'); if($upload){ $name = date("Ymd&quo ...

  8. Bootstrap3写的红色警告框样式组件

    用的是BT3的类和fa的图标 <!DOCTYPE html><html><head lang="en">    <meta charset ...

  9. Goldengate双向复制配置

    一.Goldengate双向复制配置 1.1.在进行如下配置之前,先在源数据库source system(原来的目标数据库)端 添加辅助的redolog配置: SQL>alter databas ...

  10. Pomelo聊天室框架

    聊天室是验证服务器最典型的例子,Pomelo的运行架构: 在这个架构里,前端服务器也就是connector专门负责承载连接, 后端的聊天服务器则是处理具体逻辑的地方. 这样扩展的运行架构具有如下优势: ...