在客户端配置文件<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 ...
随机推荐
-
自编译ngrok服务器
转载:http://www.haiyun.me/archives/1012.html 首先安装GO环境,http://www.haiyun.me/archives/1009.html 1 2 3 4 ...
-
微软已于10月底停止销售预装Windows 7/8.1的电脑
如果你想买新电脑,但是对Windows 10不感冒,你最好抓紧时间,因为这个月底是Windows7和Windows8.1的最后销售期限.10月31号是你能买到预装这两款操作系统的新电脑的最后一天,微软 ...
-
load_library(linker.cpp:759): library ";libmaliinstr.so"; not found
1.02-07 15:19:09.277: E/linker(16043): load_library(linker.cpp:759): library "libmaliinstr.so&q ...
-
依赖注入(DI)
依赖注入(DI) IoC主要体现了这样一种设计思想:通过将一组通用流程的控制从应用转移到框架之中以实现对流程的复用,同时采用“好莱坞原则”是应用程序以被动的方式实现对流程的定制.我们可以采用若干设 ...
-
jsp数据
---恢复内容开始--- JSP 是什么 JSP全称Java Server Pages,是一种动态网页开发技术.它使用JSP标签在HTML网页中插入Java代码.标签通常以<%开头以%>结 ...
-
vue脚手架安装步骤vue-cli
1.环境搭建 安装node.js: 从node.js官网下载并安装node,安装过程很简单. npm 版本需要大于 3.0,如果低于此版本需要升级它: # 查看版本 $ npm -v 2.3 ...
-
Spring Boot入门(11)实现文件下载功能
在这篇博客中,我们将展示如何在Spring Boot中实现文件的下载功能. 还是遵循笔者写博客的一贯风格,简单又不失详细,实用又能让你学会. 本次建立的Spring Boot项目的主要功能 ...
-
和docket的第一次亲密接触
很久很久以前,第一次听说docker时,感觉很高大上,同时自我感觉会很难.所以一直没有详细了解.前一段时间偶尔看到关于docker的详细介绍,于是乎来了兴趣.自已折腾了一下,发现不是想象中的那么难. ...
-
A+B for Input-Output Practice (VII)
A+B for Input-Output Practice (VII) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
-
torchnet package (2)
torchnet package (2) torchnet torch7 Dataset Iterators 尽管是用for loop语句很容易处理Dataset,但有时希望以on-the-fly m ...