SQLite在C#中的使用
SQLite是一款轻型的数据库,在一些数据量不太大的程序中,它暂用的资源非常低。支持很多操作系统和许多语言,所以还是很方便的。在C#中,要用的话可以通过网站来下载或者在VS中通过NuGet来下载。这个就不多说了,当你下载并且装完数据库后,就可以直接使用它,下面就先看下最基础的数据库操作:
首先就是关于创建数据库和创建表:
private static void Create() {
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Data")) {
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Data");
SQLiteConnection.CreateFile(AppDomain.CurrentDomain.BaseDirectory + "Data\\test2.db3");
}
if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Data\\test2.db3")) {
string datasource = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "Data\\test2.db3";
SQLiteConnection conn = new SQLiteConnection(datasource);
conn.Open();
SQLiteCommand cmd = conn.CreateCommand();
string sql = "Create TABLE Test(ID GUID PRIMARY KEY,Content INTEGER NOT NULL)";
cmd.CommandText = sql;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
conn.Close();
}
}
然后么就是什么增删改查之类的东西了,这其实都差不多的:
private static void Create() {
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Data")) {
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Data");
SQLiteConnection.CreateFile(AppDomain.CurrentDomain.BaseDirectory + "Data\\test2.db3");
}
if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Data\\test2.db3")) {
string datasource = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "Data\\test2.db3";
SQLiteConnection conn = new SQLiteConnection(datasource);
conn.Open();
SQLiteCommand cmd = conn.CreateCommand();
string sql = "Create TABLE Test(ID GUID PRIMARY KEY,Content INTEGER NOT NULL)";
cmd.CommandText = sql;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
conn.Close();
}
}
private static void Add() {
using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "Data\\test2.db3")) {
conn.Open();
SQLiteCommand cmd = conn.CreateCommand();
DbTransaction trans = conn.BeginTransaction(); //延时事务
try {
for (int i = ; i < ; i++) {
cmd.CommandText = "insert into Test values(@ID,@Content)";
cmd.Parameters.Add(new SQLiteParameter("@ID", Guid.NewGuid()));
cmd.Parameters.Add(new SQLiteParameter("@Content", i));
cmd.ExecuteNonQuery();
}
trans.Commit(); //提交事务
} catch {
trans.Rollback(); //事务回滚
throw;
}
}
}
private static void DeleteOne() {
using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "Data\\test2.db3")) {
conn.Open();
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = string.Format("delete from Test where Content=@value");
cmd.Parameters.Add(new SQLiteParameter("@value", ));
cmd.ExecuteNonQuery();
}
}
private static List<Guid> GetGuid() {
using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "Data\\test2.db3")) {
List<Guid> list = new List<Guid>();
conn.Open();
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Test where Content=@content";
cmd.Parameters.Add(new SQLiteParameter("content", ));
SQLiteDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
Guid ID = dr.GetGuid();
list.Add(ID);
}
return list;
}
}
然后再看到上面的Add()的方法中,如果一次性的批量加入数据时,需要对事务进行一个处理,因为如果不处理的话一次的执行就是一条事务,这样时间都花在了开始事务、提交事务上,速度会很慢。应该把这个添加变为一个统一的事务,同事如果出错,还可以进行事务回滚。
跟事务有关联的是一个锁的问题,当有两个以上链接要读写同一个数据库的时候就会发生死锁的问题。虽然我还没碰到过,不过还是先知道下比较好,免的以后出错。具体的概念嘛还是看看别人写的:链接1&&链接2。
最后是我的Demo
SQLite在C#中的使用的更多相关文章
- sqlite在火狐中安装及使用
1.SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经在很多嵌入 ...
- sqlite 删除表中重复数据(亲测可用)
例子:表名 Paper .通过字段PaperID查找重复数据. 1 --查询某表中重复的数据 select * from Paper group by PaperID having co ...
- android 检测sqlite数据表中字段(列)是否存在 (转)
原文摘自 http://www.tuicool.com/articles/jmmMnu 一般数据库升级时,需要检测表中是否已存在相应字段(列),因为列名重复会报错.方法有很多,下面列举2种常见的方式: ...
- SQLite在C#中的安装与操作
SQLite 介绍 SQLite,是一款轻型的数据库,用于本地的数据储存. 先说说优点,它占用资源非常的低,在嵌入式设备中需要几百K的内存就够了:作为轻量级数据库,他的处理速度也足够快:支持的的容量级 ...
- SQLite在.NET中自适应32位/64位系统
如果一个.NET应用要自适应32位/64位系统,只需要在项目的“目标平台”设置为“Any CPU”.但是如果应用中使用了SQLite,情况就不同了. SQLite的.NET开发包来自是System.D ...
- SQLITE在IIS中使用问题
在WEB中使用这个数据库时,System.Data.SQLite.dll 这个经常会出问题 主要是版本问题,sqlite.DLL的版本要和Framework版本匹配 这是下载地址 http://www ...
- 看不见的攻击面:查看 SQLite 数据库就中招?
Navicat 客户端存在一个 XSS,在查看表字段时,没有对内容进行处理,导致一个 XSS 问题.利用这个漏洞可以读取敏感文件,比如 /Users/XXXX/.bash_history . 漏洞发现 ...
- sqlite 查询数据库中所有的表名,判断某表是否存在,将某列所有数值去重后获得数量
1.列出当前db文件中所有的表的表名 SQL语句:SELECT * FROM sqlite_master WHERE type='table'; 结构如下: 注:网上有人说可以带上db文件的名称,如: ...
- 【SQLite】 C#中操作SQlite
简介 SQLite是轻量级数据库,具体的介绍请参考官网(SQLite官网).在WinForm的项目中需要采用独立的数据库访问,可在断网单机上使用,所以选择了SQLite! 使用 主要针对C#项目(Wi ...
- 关于在SQLITE数据库表中插入本地系统时间的做法
首先,我参考下面的博文地址:http://blog.csdn.net/liuzhidong123/article/details/6847104 sqlite3 表里插入系统时间(时间戳) 分类: s ...
随机推荐
- reload函数
reload函数 python2中reload()是内置函数,可以直接调用: reload() python3中将reload()函数放到了imp包中,需要先引入imp包: from imp impo ...
- Python(进程池与协程)
1.进程池与线程池: 为什么要用“池”:池子使用来限制并发的任务数目,限制我们的计算机在一个自己可承受的范围内去并发地执行任务 池子内什么时候装进程:并发的任务属于计算密集型 池子内什么时候装线程:并 ...
- C#设置当前程序通过IE代理服务器上网
注意:以下设置只在当前程序中有效,对IE浏览器无效,且关闭程序后,自动释放代码. using System; using System.Collections.Generic; using Syste ...
- 添加github ssh 公钥
1.在控制台输入命令: ssh-keygen -t rsa -C "github上的邮箱地址" 2.将公匙复制后添加到github网站: id_rsa.pub文件 3.测试是 ...
- Redis一(Redis-py与String操作)
Redis 介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...
- PHP逐字符读取数据
<?php $file = fopen("Minot.txt", "r") or exit("Unable to open file!" ...
- 运用SQLAlchemy
result = engine.execute(s) for row in result: Info["UserId"]=row[0] Info["UserTitle&q ...
- cdoj1329卿学姐与魔法
地址:http://acm.uestc.edu.cn/#/problem/show/1329 题目: 卿学姐与魔法 Time Limit: 1200/800MS (Java/Others) M ...
- Oracle数据安全(四)j角色管理
一.角色管理的概述 1.角色的概念 为了简化数据库权限的管理,在Oracle数据库中引入了角色的概念.所谓的角色就是一系列相关权限的集合. 2.角色的特点 在数据库中,角色的名称必须是唯一的,不能与用 ...
- Mysql5.7 用户与授权
mysql -uroot -proot MySQL5.7 mysql.user表没有password字段改 authentication_string: 一. 创建用户: 命令:CREATE USER ...