C# SQLite数据库
在客户端配置文件<configuration>节点下,添加:
- <connectionStrings>
- <add name="localdb" connectionString="Data Source=config/local.db;Version=3;UseUTF16Encoding=True;" providerName="System.Data.SQLite.SQLiteFactory"/>
- </connectionStrings>
其中【localdb】是本地SQLite数据库的名称,【config/local.db】是在当前程序运行目录下SQLite数据库位置
C# SQLite数据库 访问封装类代码:
/// <summary>
/// 本类为SQLite数据库帮助静态类,使用时只需直接调用即可,无需实例化
/// </summary>
public static class SQLiteHelper
{
// Application.StartupPath
public static string LocalDbConnectionString = ConfigurationManager.ConnectionStrings["localdb"].ConnectionString; #region ExecuteNonQuery
/// <summary>
/// 执行数据库操作(新增、更新或删除)
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>所受影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, 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="connectionString">连接字符串</param>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型</param>
/// <returns>所受影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, string commandText, CommandType commandType)
{
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="connectionString">连接字符串</param>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型</param>
/// <param name="cmdParms">SQL参数对象</param>
/// <returns>所受影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, string commandText, CommandType commandType, 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);
try
{
result = cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
#endregion #region ExecuteScalar
/// <summary>
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>查询所得的第1行第1列数据</returns>
public static object ExecuteScalar(string connectionString, 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="connectionString">连接字符串</param>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型</param>
/// <returns>查询所得的第1行第1列数据</returns>
public static object ExecuteScalar(string connectionString, string commandText, CommandType commandType)
{
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="connectionString">连接字符串</param>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型</param>
/// <param name="cmdParms">SQL参数对象</param>
/// <returns>查询所得的第1行第1列数据</returns>
public static object ExecuteScalar(string connectionString, string commandText, CommandType commandType, 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);
try
{
result = cmd.ExecuteScalar();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return result;
}
#endregion #region ExecuteReader
/// <summary>
/// 执行数据库查询,返回SqlDataReader对象
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>SqlDataReader对象</returns>
public static DbDataReader ExecuteReader(string connectionString, 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="connectionString">连接字符串</param>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型</param>
/// <returns>SqlDataReader对象</returns>
public static DbDataReader ExecuteReader(string connectionString, string commandText, CommandType commandType)
{
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="connectionString">连接字符串</param>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型</param>
/// <param name="cmdParms">SQL参数对象</param>
/// <returns>SqlDataReader对象</returns>
public static DbDataReader ExecuteReader(string connectionString, string commandText, CommandType commandType, 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 ExecuteDataSet
/// <summary>
/// 执行数据库查询,返回DataSet对象
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>DataSet对象</returns>
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;
} /// <summary>
/// 执行数据库查询,返回DataSet对象
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型</param>
/// <returns>DataSet对象</returns>
public static DataSet ExecuteDataSet(string connectionString, string commandText, CommandType commandType)
{
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="connectionString">连接字符串</param>
/// <param name="commandText">执行语句或存储过程名</param>
/// <param name="commandType">执行类型</param>
/// <param name="cmdParms">SQL参数对象</param>
/// <returns>DataSet对象</returns>
public static DataSet ExecuteDataSet(string connectionString, string commandText, CommandType commandType, 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 通用分页查询方法
/// <summary>
/// 通用分页查询方法
/// </summary>
/// <param name="connString">连接字符串</param>
/// <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 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 - ) * 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;
}
#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
}
C# SQLite数据库的更多相关文章
- Android之SQLite数据库篇
一.SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大. 二.SQLite的特点 1.轻量级使用 SQLit ...
- Qt5 开发 iOS 应用之访问 SQLite 数据库
开发环境: macOS 10.12.1 Xcode 8.1 Qt 5.8 iPhone 6S+iOS 10.1.1 源代码: 我在 Qt 程序里指定了数据库的名称来创建数据库,在 Win10.An ...
- 【Win 10 应用开发】Sqlite 数据库的简单用法
如果老周没记错的话,园子里曾经有朋友写过如何在 UWP 项目中使用 Sqlite数据库的文章.目前我们都是使用第三方封装的库,将来,SDK会加入对 Sqlite 的支持. 尽管目前 UWP-RT 库中 ...
- Android之SQLite数据库使用
转载整理于:http://my.csdn.net/lmj623565791 我刚开始接触Android的时候甚至都不敢相信,Android系统竟然是内置了数据库的!好吧,是我太孤陋寡闻了.由于我之前是 ...
- 让PDF.NET支持最新的SQLite数据库
最近项目中用到了SQLite,之前项目中用的是PDF.NET+MySQL的组合,已经写了不少代码,如果能把写好的代码直接用在SQLite上就好了,PDF.NET支持大部分主流的数据库,这个当然可以,只 ...
- iOS sqlite数据库图像化查看
问题描述:在xocde上用sqlite数据库的时候,因为没有图形化界面,有些时候很难看出自己设计的数据库是否有问题,比如我刚上手sqlite数据库设计id为自增长时,很自然的用了identify(1, ...
- Android中SQLite数据库小计
2016-03-16 Android数据库支持 本文节选并翻译<Enterprise Android - Programing Android Database Applications for ...
- Android开发-之SQLite数据库
之前我们讲了如何将数据存储在文件中,那么除了这种方式呢,就是我们常见的大家都知道的将数据存储在数据库当中了. 将数据存储在数据库中的优势: 1)存储在数据库中的数据更加方便操作,比如增.删.改.查等 ...
- Java操作Sqlite数据库-jdbc连接
Java操作Sqlite数据库步骤: 1. 导入Sqlite jdbc 本文使用sqlite-jdbc-3.7.2.jar,下载地址 http://pan.baidu.com/s/1kVHAGdD 2 ...
- Android开发学习——SQLite数据库与单元测试
SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHelper public class Myopenhelper extends SQLiteOpenHelp ...
随机推荐
- quartz详解2:quartz由浅入深
http://blog.itpub.net/11627468/viewspace-1763498/ 一.quartz核心概念 先来看一张图: scheduler 任务调度器 trigger 触 ...
- 关于setTimeout和setInterval的函数参数问题
今天在写验证码倒计时小demo时,用了如下代码: window.setTimeout(count(num),1000); 这样直接使用将使count函数立即执行,并将返回值传递给setTimeout函 ...
- cookies和session
基于cookies做用户验证时,敏感信息不适合放在cookies中 cookies保存在客户浏览器端的键值对 session保存在服务器端的键值对(依赖于cookies),把用户浏览器中的cook ...
- JS自学笔记01
JS自学笔记01 1.开发工具 webstorm 2.js(javascript) 是一门脚本.解释性.动态类型.基于对象的语言 含三个部分: ECMAScript标准–java基本语法 DOM(Do ...
- Servlet(7)—ServletConfig接口和SevletContext接口
ServletConfig接口 1. 可以获取当前Servlet在web.xml中的配置信息(用的不多) 2. 在不使用"硬编码"的情况下,将部署状态信息传递给Servlet.这个 ...
- 微信小程序中遮罩层的滚动穿透问题
如果弹出层没有滚动事件: <view wx:if="{{alert}}" catchtouchmove="myCatchTouch"> <te ...
- hadoop2.7.3编译,支持snappy、bzip2本地压缩
软件包: apache-ant-1.9.9-bin.tar.gz apache-maven-3.3.9-bin.tar.gz apache-tomcat-6.0.44.tar.gz CentOS-6. ...
- golang使用chan注意事项
背景 最近老代码中遇到的一个问题,表现为: goroutine数量在高峰期上涨,上涨后平峰期将不下来.也就是goroutine泄露 使用pprof看,进程堵塞在chan chan的使用经验 在使用ch ...
- MUI框架开发HTML5手机APP(二)--页面跳转传值&底部选项卡切换
概 述 JRedu 在上一篇博客中,我们学习了如何使用Hbuilder创建一个APP,同时如何使用MUI搭建属于自己的第一款APP,没有学习的同学可以戳链接学习: http://www.cnblo ...
- InfluxDB服务器启动流程
操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 源码路径: github.com/influxdata/infl ...