在C#中使用SQLite数据库
轻量级桌面程序数据库不太适合用SQLServer、MySQL之类的重量级数据库,嵌入式数据库更好。在对比Access、SQLite、Firebird数据库后发现SQLite较另外两个有较多优点。
环境:.NET Framework 3.5、windows11 64位、Visual Studio 2010.
C#使用SQLite需要从SQLite官网下载DLL组件。
下载地址:https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
下载后将包解压放到一个固定目录,需要依赖System.Data.SQLite.dll和SQLite.Interop.dll这两个文件,
在项目里右键项目 > 添加引用 > 选“浏览”选项卡,找到解压后的目录,引入System.Data.SQLite.dll,另一个文件SQLite.Interop.dll不可以通过引用方式添加,必须只能复制文件到运行目录下,通过调试发现程序会自动把System.Data.SQLite.dll也复制到运行目录下,System.Data.SQLite.dll和SQLite.Interop.dll文件会在一起。(尝试过直接复制这两个文件到程序的运行目录下不可行,Visual Studio里不管怎么刷新项目的引用列表都不会出现这两个文件,运行会报错。)
C# 中使用SQLite示例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SQLite; namespace SQLiteTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("SQL Lite 数据库试验");
// 连接数据库,FailIfMissing=false时若文件不存在会自动创建
string connStr = "Data Source=test.db;Version=3;Pooling=true;FailIfMissing=false;";
SQLiteConnection conn = new SQLiteConnection(connStr);
conn.Open(); //在指定数据库中创建一个table
string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, conn);
command.ExecuteNonQuery(); // 插入一些数据
sql = "insert into highscores (name, score) values ('Me', 3000)";
command = new SQLiteCommand(sql, conn);
command.ExecuteNonQuery(); sql = "insert into highscores (name, score) values ('Myself', 6000)";
command = new SQLiteCommand(sql, conn);
command.ExecuteNonQuery(); sql = "insert into highscores (name, score) values ('And I', 9001)";
command = new SQLiteCommand(sql, conn);
command.ExecuteNonQuery(); // 查询数据
sql = "select * from highscores order by score desc";
command = new SQLiteCommand(sql, conn);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);
}
}
}
}
一般建表使用文本文件,不使用代码建(build.sql):
-- admin表
create table admin (
id nvarchar(32) primary key,
admin_account nvarchar(32) not null,
password nvarchar(32) not null
); -- file表
create table file (
id nvarchar(32) primary key,
user_account nvarchar(32) not null,
user_name nvarchar(32) not null,
file_path nvarchar(256) not null,
upload_start_time timestamp,
upload_end_time timestamp,
upload_ip nvarchar(20),
file_md5 nvarchar(32),
file_size integer,
file_suffix nvarchar(4)
); -- file_remove_history表
create table file_remove_history (
id nvarchar(32) primary key,
user_account nvarchar(32) not null,
user_name nvarchar(32) not null,
file_path nvarchar(256) not null,
upload_start_time timestamp,
upload_end_time timestamp,
upload_ip nvarchar(20),
file_md5 nvarchar(32),
file_size integer,
file_suffix nvarchar(4),
remove_user nvarchar(20),
remove_time timestamp
);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SQLite;
using System.IO; namespace U8FileBackupServer
{
public partial class Form1 : Form
{ string dbFile = System.Environment.CurrentDirectory + "\\xxx.db"; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ if (!File.Exists(dbFile))
{
Console.WriteLine("文件不存在,执行创建。");
SQLiteConnection.CreateFile(dbFile);
// 连接数据库,FailIfMissing=false时若文件不存在也会自动创建
SQLiteConnection conn = new SQLiteConnection("Data Source=" + dbFile + ";Version=3;Pooling=true;FailIfMissing=false;");
conn.Open(); // 打开连接 // 建表
string sqlText = new StreamReader(System.Environment.CurrentDirectory + "\\build.sql").ReadToEnd();
Console.WriteLine("= = = = = = = = = = = = = = = = = = = = = = = = =");
Console.WriteLine(sqlText);
Console.WriteLine("= = = = = = = = = = = = = = = = = = = = = = = = =");
SQLiteCommand cmd = new SQLiteCommand(sqlText, conn);
cmd.ExecuteNonQuery();
conn.Close(); // 关闭连接
} SQLiteConnection conn1 = new SQLiteConnection("Data Source=" + dbFile + ";Version=3;Pooling=true;FailIfMissing=true;");
conn1.Open();
// 插入一些数据
string sql = "insert into admin (id, admin_account, password) values ('111', '管理员', 'admin')";
SQLiteCommand command = new SQLiteCommand(sql, conn1);
command.ExecuteNonQuery();
// 查询数据
sql = "select * from admin";
command = new SQLiteCommand(sql, conn1);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("admin_account: " + reader["admin_account"] + "\tpassword: " + reader["password"]);
}
conn1.Close(); }
}
}
更多参考资料:
在C#中使用SQLite数据库的更多相关文章
- Android虚拟机中的sqlite数据库文件
Android虚拟机中的sqlite数据库文件 ①
- 在项目中使用SQLite数据库小结
------------------------------------------------------------------------推荐: - VS2012 使用 1.0.84 版的库 - ...
- 2014-08-01 ASP.NET中对SQLite数据库的操作——ADO.NET
今天是在吾索实习的第18天.我主要学习了如何在ASP.NET中对SQLite数据库的操作,其基本操作如下: 添加引用System.Data.SQLite.dll(PS:在网页里面任意找到适合的.NET ...
- android中与SQLite数据库相关的类
为什么要在应用程序中使用数据库?数据库最主要的用途就是作为数据的存储容器,另外,由于可以很方便的将应用程序中的数据结构(比如C语言中的结构体)转化成数据库的表,这样我们就可以通过操作数据库来替代写一堆 ...
- Go语言中使用SQLite数据库
Go语言中使用SQLite数据库 1.驱动 Go支持sqlite的驱动也比较多,但是好多都是不支持database/sql接口的 https://github.com/mattn/go-sqlite3 ...
- 在Android 开发中使用 SQLite 数据库笔记
SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PH ...
- 在 Android 应用程序中使用 SQLite 数据库以及怎么用
part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...
- 【Win10】在应用中使用 SQLite 数据库
在绝大多数应用中,免不了要做的一项就是设置这样的本地数据存储.简单的数据存储我们可以使用 LocalSettings 或者 IsolatedStorageFile(独立存储)等等的方式来进行本地数据存 ...
- C#中使用SQLite数据库简介(上)
[SQLite数据库] SQLite是一个开源的轻量级的桌面型数据库,它将几乎所有数据库要素(包括定义.表.索引和数据本身)都保存在一个单一的文件中.SQLite用C编写实现,它在内存消耗.文件体积. ...
- 在安卓开发中使用SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
随机推荐
- Apache Ranger系列六:Submarine Spark Security Plugin安装(0.6.0版本)
参考 https://submarine.apache.org/zh-cn/docs/0.6.0/userDocs/submarine-security/spark-security/ 从ranger ...
- 实验七:基于REST API的SDN北向应用实践
(一)基本要求 编写Python程序,调用OpenDaylight的北向接口实现以下功能 (1) 利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight: (2) 下发指令删除s1 ...
- vue自动展示一、二级路由
在vue项目中使用路由可以很方便的跳转要显示的页面 在初始页面当中,首先要显示的路由怎么实现呢? 只需要在index.js页面中存放路由的children:[...]最后加上 redirect:&qu ...
- Linux部署JDK教程
上一次说了windows下的jdk部署,这一次记录下Linux下的jdk部署,恰巧遇到一篇写的很清楚的教程,我就直接转过来啦,哈哈.. 一. 解压安装jdk 在shell终端下进入jdk-6u14-l ...
- [ACTF2020 新生赛]Include 1
首先进入靶场可以看到trip 查看源码 点击进入提示我们能不能找到flag 可以看到这里是文件包含,想着包含index.php但是根目录是自动索引的,无论输入什么都是trip页面 又想着包含flag. ...
- Spring--AOP简介+入门案例
AOP简介 面向切面编程:在不惊动原始设计的基础上,进行功能增强 各个要应用该功能的对象叫做连接点,那个功能叫做通知,表面上的代码没有发生变化,私下里发生变化的连接点,会出现切入点,切入点与通知通过切 ...
- MySQL 某一列的值加入到另一列
0.背景 文件url 文件名 /usr/local/img/goods/1/2021-12-22-e05bb433bc7a451ca5d7cc9d505d8ed8.jpg 酸枣糕.jpg /usr/l ...
- RPC 与 Restful 的区别
PRC 是一种技术的代名词,HTTP 是一种协议,RPC 可以通过 HTTP 来实现,也可以通过 Socket 自己实现一套协议来实现.所以谈论为什么用 RPC 不用 HTTP 是无意义的.但我们习惯 ...
- 超详细!新手如何创建一个Vue项目
目录 一.在官网下载Vue.js 二.使用<script>标签直接引入本地的vue.js 三.使用CDN引入Vue.js 四.验证是否安装成功 五.安装Vue Devtools浏览器调试插 ...
- 深入理解 python 虚拟机:字节码教程(2)——控制流是如何实现的?
深入理解 python 虚拟机:字节码教程(2)--控制流是如何实现的? 在本篇文章当中主要给大家分析 python 当中与控制流有关的字节码,通过对这部分字节码的了解,我们可以更加深入了解 pyth ...