SQLiteHelp
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using System.Data;
namespace MySQLiteDemo
{
public class SqliteHelper
{
private string _connectString = string.Empty;
public string ConnectString
{
get { return _connectString; }
set { _connectString = value; }
}
/// <summary>
/// 检测
/// </summary>
/// <returns></returns>
public static bool TestLite(string strConn)
{
SQLiteConnection conn = new SQLiteConnection(strConn);
conn.Open();
if (conn.State != ConnectionState.Open)
{
return false;
}
return true;
}
/// <summary>
/// 获取数据
/// </summary>
/// <returns></returns>
public DataTable GetData(string strSql)
{
return GetData(strSql, null);
}
/// <summary>
/// 获取数据
/// </summary>
/// <returns></returns>
public DataTable GetData(string strSql, SQLiteParameter[] cmdParams)
{
using (SQLiteConnection con = new SQLiteConnection(_connectString))
{
try
{
SQLiteCommand cmd = new SQLiteCommand(strSql, con);
if (cmdParams != null)
{
foreach (SQLiteParameter param in cmdParams)
{
cmd.Parameters.Add(param);
}
}
con.Open();
DataTable ret = new DataTable();
SQLiteDataAdapter ad = new SQLiteDataAdapter(cmd);
ad.Fill(ret);
return ret;
}
catch (SQLiteException ex)
{
throw new Exception(ex.Message);
}
}
}
/// <summary>
/// 获取SQLiteDataReader
/// </summary>
/// <returns></returns>
public SQLiteDataReader GetReader(string strSql)
{
return GetReader(strSql, null);
}
/// <summary>
/// 执行带参数的查询语句,返回SQLiteDataReader ( 注意:调用该方法后,一定要对SQLiteDataReader进行Close )
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>SqlDataReader</returns>
public SQLiteDataReader GetReader(string strSql, params SQLiteParameter[] cmdParms)
{
using (SQLiteConnection con = new SQLiteConnection(_connectString))
{
try
{
SQLiteCommand cmd = new SQLiteCommand(strSql, con);
if (cmdParms != null)
{
foreach (SQLiteParameter param in cmdParms)
{
cmd.Parameters.Add(param);
}
}
con.Open();
SQLiteDataReader myReader = cmd.ExecuteReader();
cmd.Parameters.Clear();
return myReader;
}
catch (SQLiteException ex)
{
throw new Exception(ex.Message);
}
}
}
/// <summary>
/// 执行语句
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int ExecuteSql(string strSql)
{
using (SQLiteConnection con = new SQLiteConnection(_connectString))
{
try
{
SQLiteCommand cmd = new SQLiteCommand(strSql, con);
con.Open();
return cmd.ExecuteNonQuery();
}
catch (SQLiteException ex)
{
throw new Exception(ex.Message);
}
}
}
/// <summary>
/// 执行语句
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int ExecuteSql(string strSql, SQLiteParameter[] cmdParams)
{
using (SQLiteConnection con = new SQLiteConnection(_connectString))
{
try
{
SQLiteCommand cmd = new SQLiteCommand(strSql, con);
if (cmdParams != null)
{
foreach (SQLiteParameter param in cmdParams)
{
cmd.Parameters.Add(param);
}
}
con.Open();
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (SQLiteException ex)
{
throw new Exception(ex.Message);
}
}
}
/// <summary>
/// 获取当前第一行第一列值
/// </summary>
/// <param name="strsql"></param>
/// <param name="cmdParams"></param>
/// <returns></returns>
public object GetSingle(string strSql)
{
return GetSingle(strSql, null);
}
/// <summary>
/// 获取当前第一行第一列值
/// </summary>
/// <param name="strsql"></param>
/// <param name="cmdParams"></param>
/// <returns></returns>
public object GetSingle(string strSql, SQLiteParameter[] cmdParams)
{
using (SQLiteConnection con = new SQLiteConnection(_connectString))
{
try
{
SQLiteCommand cmd = new SQLiteCommand(strSql, con);
if (cmdParams != null)
{
foreach (SQLiteParameter param in cmdParams)
{
cmd.Parameters.Add(param);
}
}
con.Open();
return cmd.ExecuteScalar();
}
catch (SQLiteException ex)
{
throw new Exception(ex.Message);
}
}
}
/// <summary>
/// 页级操作
/// </summary>
/// <param name="sql"></param>
/// <param name="startRecord"></param>
/// <param name="maxRecord"></param>
/// <returns></returns>
public DataTable FindByPaging(string strSql, SQLiteParameter[] cmdParams, int startRecord, int maxRecord)
{
using (SQLiteConnection con = new SQLiteConnection(_connectString))
{
try
{
SQLiteCommand cmd = new SQLiteCommand(strSql + " LIMIT " + +startRecord + "," + maxRecord, con);
if (cmdParams != null)
{
foreach (SQLiteParameter param in cmdParams)
{
cmd.Parameters.Add(param);
}
}
con.Open();
DataTable ret = new DataTable();
SQLiteDataAdapter ad = new SQLiteDataAdapter(cmd);
ad.Fill(ret);
return ret;
}
catch (SQLiteException ex)
{
throw new Exception(ex.Message);
}
}
}
/// <summary>
/// 启用事务.
/// </summary>
/// <param name="objTrans"></param>
/// <returns></returns>
public SQLiteTransaction BeginSQLTrans()
{
SQLiteConnection con = new SQLiteConnection(_connectString);
con.Open();
return con.BeginTransaction();
}
/// <summary>
/// 执行语句
/// </summary>
/// <param name="strSql"></param>
/// <param name="cmdParams"></param>
/// <param name="trans"></param>
/// <returns></returns>
public int ExecuteSql(string strSql, SQLiteTransaction trans)
{
return ExecuteSql(strSql, null, trans);
}
/// <summary>
/// 执行语句
/// </summary>
/// <param name="strSql"></param>
/// <param name="cmdParams"></param>
/// <param name="trans"></param>
/// <returns></returns>
public int ExecuteSql(string strSql, SQLiteParameter[] cmdParams, SQLiteTransaction trans)
{
int ret = 0;
using (SQLiteCommand cmd = new SQLiteCommand(strSql, trans.Connection))
{
try
{
if (cmdParams != null)
{
foreach (SQLiteParameter param in cmdParams)
{
cmd.Parameters.Add(param);
}
}
if (trans != null)
cmd.Transaction = trans;
ret = cmd.ExecuteNonQuery();
}
catch (SQLiteException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
return ret;
}
}
}
SQLiteHelp的更多相关文章
- [android] SQLite 数据库的升级 和 降级
public class SqliteHelp extends SQLiteOpenHelper { /* * context:创建数据库所需的 上下文对象 * name: 数据库名字 * facto ...
- 要知道的DbProviderFactory
了解DbProviderFactory 前不久想使用下EF的通用单表增删改的特性,当时使用控制台做测试,怎么弄都没成功,原因出在app.config中,反而在mvc项目中,就没有任何问题.在反复的更改 ...
- Unity3D 使用SQLite
使用Unity操作SQLite,需要用到三个库文件,分别是Mono.Data.Sqlite.dll和System.Data.dll和Sqlite3.dll,前两个库文件可以在unity中找到,具体步骤 ...
随机推荐
- 在AndroidManifest(清单文件)中注册activity(活动)及配置主活动、更改App图标、App名称、修改隐藏标题栏
打开app/src/main/AndroidManifest. <?xml version="1.0" encoding="utf-8"?> < ...
- Windows 下修改 MySQL 编码为 utf8
问题 Windows 下安装 MySQL 后,默认编码不全utf8. mysql> show variables like '%char%'; +------------------------ ...
- readonly and const variable
共同点:都是常量: 不同点:const的值必须在编译前确定,通常在声明的同时赋值:而readonly可在运行时确定:
- size_t与size_type的使用
size_t 是为了方便系统之间的移植而定义的 在32位系统上 定义为 unsigned int在64位系统上 定义为 unsigned long 更准确地说法是 在 32位系统上是32位无符号整形在 ...
- ReactiveX Operators
This documentation groups information about the various operators and examples of their usage into t ...
- Linux搭建Jenkins
1.添加存储库 yum的repo中默认没有Jenkins,需要先将Jenkins存储库添加到yum repos,执行下面的命令: 使用wget -O下载并以不同的文件名保存 [root@besttes ...
- jmeter实现逻辑控制器
- crontab 设置定时任务
查看当前用户已有的定时任务: crontab -l 编辑crontab: crontab -e 加入需要执行的命令: 0 */4 * * * /www/shwww.net/venv/bin/pytho ...
- easyui获取当前点击对象tabs的title和Index
观察上面打开的tabs选项卡,肯定会有一个目前是被选中状态,而这个状态的class属性也肯定是和其他tabs不一样的,有个class等于tabs-selected的 var title = $('.t ...
- nyoj 803 大数问题
#include<stdio.h> #include<string.h> #define ll long long #define N 110000 int main() { ...