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中找到,具体步骤 ...
随机推荐
- 【Oracle】手工建库启动到nomount状态时错误ORA-09925,ORA-01017
配置好pfile和口令文件后启动数据库到nomount状态下出现错误: [oracle@localhost ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2 ...
- IIS网站调用FFMPEG执行截图命令失败的原因(暂时记录)
之前有个功能是用户上传视频后,网站对其进行截图处理.功能采用FFMPEG操作,在本机新建一个小程序进行调试时是没有问题的. 可功能原封不动挪到项目里,截图竟然失败了,于是开始排查. 期初以为是参数设置 ...
- Java数组的运用
Java数组 应用1: 大乐透彩票模拟器: 规则: 前区01-35中随机生成5个号码 后区01-12中随机生成2个号码 模拟操作,系统自动生成号码组合,并且按从小到大的顺序输出结果 同时要求可以选择生 ...
- 00-- 关于C++ const 的全面总结
转:关于C++ const 的全面总结 C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助. Const 是C ...
- CorelDRAW 2019线上发布会报名已开始
近日,由苏州思杰马克丁软件公司独家代理的CorelDRAW 2019将在苏州开启一场设计上的饕餮盛宴,您报名了么? 不管您是专业的设计师还是热爱设计的狂热粉丝,都将有机会参与到我们的活动中,为了这场盛 ...
- python之parameterized模块
parameterized扩展了py.test参数化测试,unittest参数化测试. <1>一个小练习 import unittest import math @parameterize ...
- redi通过哨兵sentinel实现主从切换
本次实验主要为了让哨兵监听redis主从复制,当主节点关闭后,哨兵会选举一台从节点成为主节点,并且让其他从节点变成新主节点得从节点 本次理论需要三台机器,一主两从,为了方便用一台服务器开启三个实例,一 ...
- TensorFlow实现LeNet5模型
# -*- coding: utf-8 -*-import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_ ...
- 洛谷 1775. [国家集训队2010]小Z的袜子
1775. [国家集训队2010]小Z的袜子 ★★★ 输入文件:hose.in 输出文件:hose.out 简单对比时间限制:1 s 内存限制:512 MB [题目描述] 作为一个生活 ...
- Android 开发最佳实践
原文地址:https://github.com/futurice/android-best-practices/blob/master/translations/Chinese/README.cn.m ...