SQLite特点
(1)轻量级,跨平台的关系型数据库,所以支持视图,事务,触发器等。
(2)零配置-无需安装和管理配置,存储在单一磁盘文件中的完整的数据库
(3)数据库文件可共享,支持多种开发语言。
 
SQLite应用场景
适用于并发量低,访问需求高于写入操作的一些引用
例如,最近的项目里,在客户的文件服务器下,需要要建立文件路径的索引,并能够文件打包下载,考虑到不想装单独的数据库,于是乎便用了SQLite
 
SQLite使用
封装SQliteHelper类。
  public static class SqliteHelper
{
private static string connectionString = ConfigurationManager.ConnectionStrings["SqliteConnectionstring"].ConnectionString; /// <summary>
/// 创建一个数据库文件。如果存在同名数据库文件,则会覆盖。
/// </summary>
/// <param name="dbName">数据库文件名。为null或空串时不创建。</param>
/// <param name="password">(可选)数据库密码,默认为空。</param>
/// <exception cref="Exception"></exception>
public static void CreateDB(string dbName)
{
SQLiteConnection conn = null;
string dbPath = "Data Source ="+ connectionString+"/"+dbName + ".db";
conn = new SQLiteConnection(dbPath);//创建数据库实例,指定文件位置
conn.Open();//打开数据库,若文件不存在会自动创建
string sql = "CREATE TABLE IF NOT EXISTS T_BuildChildPath(Id integer IDENTITY(1,1), ProductsCode nvarchar(100), CodePath nvarchar(100), CreateTime datetime);";//建表语句
SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn);
cmdCreateTable.ExecuteNonQuery();//如果表不存在,创建数据表
conn.Close();
}
/// <summary>
/// 对SQLite数据库执行增删改操作,返回受影响的行数。
/// </summary>
/// <param name="sql">要执行的增删改的SQL语句。</param>
/// <param name="parameters">执行增删改语句所需要的参数,参数必须以它们在SQL语句中的顺序为准。</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static int ExecuteNonQuery(string sql, params SQLiteParameter[] parameters)
{
int affectedRows = ;
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand command = new SQLiteCommand(connection))
{
try
{
connection.Open();
command.CommandText = sql; if (parameters.Length != )
{
command.Parameters.AddRange(parameters);
}
affectedRows = command.ExecuteNonQuery();
}
catch (Exception ex) { throw; }
}
}
return affectedRows;
}
public static int ExecuteNonQuery(string sql )
{
int affectedRows = ;
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand command = new SQLiteCommand(connection))
{
try
{
connection.Open();
command.CommandText = sql; affectedRows = command.ExecuteNonQuery();
}
catch (Exception ex) { throw; }
}
}
return affectedRows;
}
/// <summary>
/// 批量处理数据操作语句。
/// </summary>
/// <param name="list">SQL语句集合。</param>
/// <exception cref="Exception"></exception>
public static void ExecuteNonQueryBatch(List<KeyValuePair<string, SQLiteParameter[]>> list)
{
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
try { conn.Open(); }
catch { throw; }
using (SQLiteTransaction tran = conn.BeginTransaction())
{
using (SQLiteCommand cmd = new SQLiteCommand(conn))
{
try
{
foreach (var item in list)
{
cmd.CommandText = item.Key;
if (item.Value != null)
{
cmd.Parameters.AddRange(item.Value);
}
cmd.ExecuteNonQuery();
}
tran.Commit();
}
catch (Exception) { tran.Rollback(); throw; }
}
}
}
}
/// <summary>
/// 执行查询语句,并返回第一个结果。
/// </summary>
/// <param name="sql">查询语句。</param>
/// <returns>查询结果。</returns>
/// <exception cref="Exception"></exception>
public static object ExecuteScalar(string sql, params SQLiteParameter[] parameters)
{
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(conn))
{
try
{
conn.Open();
cmd.CommandText = sql;
if (parameters.Length != )
{
cmd.Parameters.AddRange(parameters);
}
return cmd.ExecuteScalar();
}
catch (Exception) { throw; }
}
}
}
public static bool ExecuteScalar(string sql )
{
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(conn))
{
try
{
conn.Open();
cmd.CommandText = sql; return (Convert.ToInt32(cmd.ExecuteScalar()?.ToString()??"")>)?true:false;
}
catch (Exception) { throw; }
}
}
}
public static string ExecuteScalarStr(string sql)
{
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(conn))
{
try
{
conn.Open();
cmd.CommandText = sql;
return cmd.ExecuteScalar()?.ToString();
}
catch (Exception) { throw; }
}
}
}
/// <summary>
/// 执行一个查询语句,返回一个包含查询结果的DataTable。
/// </summary>
/// <param name="sql">要执行的查询语句。</param>
/// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准。</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static DataTable ExecuteQuery(string sql, params SQLiteParameter[] parameters)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
if (parameters.Length != )
{
command.Parameters.AddRange(parameters);
}
SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
DataTable data = new DataTable();
try { adapter.Fill(data); }
catch (Exception) { throw; }
return data;
}
}
}
/// <summary>
/// 执行一个查询语句,返回一个关联的SQLiteDataReader实例。
/// </summary>
/// <param name="sql">要执行的查询语句。</param>
/// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准。</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] parameters)
{
SQLiteConnection connection = new SQLiteConnection(connectionString);
SQLiteCommand command = new SQLiteCommand(sql, connection);
try
{
if (parameters.Length != )
{
command.Parameters.AddRange(parameters);
}
connection.Open();
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception) { throw; }
}
/// <summary>
/// 查询数据库中的所有数据类型信息。
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static DataTable GetSchema()
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
try
{
connection.Open();
return connection.GetSchema("TABLES");
}
catch (Exception) { throw; }
}
}
}

这里的SqliteConnectionstring连接字符串和SQL类似,Data Source改成对应的物理路径即可。

  <add name="SqliteConnectionstring" connectionString="Data  Source=D:/ProductsIndex.db"  providerName="System.Data.SqlClient" />

页面的调用:

string sql = string.Format(@" select * from T_BuildChildPath where ProductsCode like '%"  + PartCode + "%'");
DataView dv = SqliteHelper.ExecuteQuery(sql).DefaultView;

SQLite介绍和使用的更多相关文章

  1. (转)Android学习笔记---SQLite介绍,以及使用Sqlite,进行数据库的创建,完成数据添删改查的理解

    原文:http://blog.csdn.net/lidew521/article/details/8655229 1.SQLite介绍:最大特点是,无数据类型;除了可以使用文件或SharedPrefe ...

  2. SQLite介绍、学习笔记、性能测试

    SQLite介绍.学习笔记.性能测试 哪些人,哪些公司或软件在用SQLite: Nokia's Symbian,Mozilla,Abobe,Google,阿里旺旺,飞信,Chrome,FireFox可 ...

  3. 【数据库】 SQLite 介绍

    [数据库] SQLite 介绍 一. 特点 : 小而精悍 1. 轻量级 : 占用资源低 2. 嵌入式 : 无需安装,直接引用就可用 3. 支持 SQL 语法, 大部分兼容 Sql Server 语法, ...

  4. 学习笔记——SQLite介绍

    简介:Google为android的较大数据的处理提供了SQLlite, 他在数据存储.管理.维护.等各方面都相当出色功能也非常强大. 1.创建数据库 Android 为了让我们能够更加方便地管理数据 ...

  5. Android中SQLite介绍

    现在的主流移动设备像Android.iPhone等都使用SQLite作为复杂数据的存储引擎,在我们为移动设备开发应用程序时,也许就要使用到SQLite来存储我们大量的数据,所以我们就需要掌握移动设备上 ...

  6. SQLite介绍及使用

    SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 ...

  7. SQLite介绍

    优点:轻量级.绿色组件.单一文件.跨平台.查询效率极高.使用事务插入速度极快.支持limit分页. 适合查询速度要求较高,内存占用较少的场合,尤其是嵌入式操作系统,如各种手机操作系统,低并发web(9 ...

  8. C#操作SQLite数据库

    SQLite介绍 SQLite is a software library that implements a self-contained, serverless, zero-configurati ...

  9. 数据库SQLite

    一.数据库 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等.离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式: 归档:NSKeyedArchiver 偏好设置:NSU ...

随机推荐

  1. [Gamma阶段]第五次Scrum Meeting

    Scrum Meeting博客目录 [Gamma阶段]第五次Scrum Meeting 基本信息 名称 时间 地点 时长 第五次Scrum Meeting 19/05/31 大运村寝室6楼 30min ...

  2. [Gamma]Scrum Meeting#1

    github 本次会议项目由PM召开,时间为5月26日晚上10点30分 时长25分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客,组织例会 swoip 前端显示屏幕,翻译坐标 bhlt 后端 ...

  3. web项目脱敏白名单管理

    写在前面 下午没事, 看一下同事写的脱敏白名单管理功能. 所谓的脱敏就是将页面中查询出的信息列表当中的身份证号及手机号等关键信息部分用*号代替.该功能主要思路是新增一个页面用于配置哪些页面以及页面中哪 ...

  4. 网页版查看Android源码地址

    Android社区 http://www.androidos.net.cn/sourcecode

  5. 《自然语言理解(Natural Language Understanding)》(2016-03-17)阅读笔记

    原文链接:https://yq.aliyun.com/articles/8301 作者:李永彬 发布时间:2016-03-17 16:37:47 自然语言理解(Natural Language Und ...

  6. Python向excel中写入数据的方法 方法简单

    最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中. 数据导入之前需要安装 xlwt依赖包,安装的方法就很简单,直接 p ...

  7. 【转】Redis哨兵(Sentinel)模式

    主从切换技术的方法是:当主服务器宕机后,需要手动把一台从服务器切换为主服务器,这就需要人工干预,费事费力,还会造成一段时间内服务不可用.这不是一种推荐的方式,更多时候,我们优先考虑哨兵模式. 一.哨兵 ...

  8. laravel5.8ajax请求auth认证返回302的解决方法。

    注册 /app/Http/Controller/Auth/RegisterController.php <?php namespace App\Http\Controllers\Auth; us ...

  9. https本地自签名证书添加到信任证书访问

    1.背景 本文适用于基于https(http+ssl)的网站通信.本地调试等,上线是请寻找免费 ssl证书申请. 本地调试过程中,一些特殊的场景需要我使用http+ssl通信,比如在Chrome中使用 ...

  10. maven工程仿springboot手写代码区分开发测试生产

    读取代码: package com.jz.compute.mc.v2.config; import java.util.Enumeration; import java.util.ResourceBu ...