当前位置:网站首页>C# 使用SQLite
C# 使用SQLite
2022-07-24 21:46:00 【flysh05】
SQLite是一个进程内的轻量级嵌入式数据库,它的数据库就是一个文件,实现了自给自足、无服务器、零配置的、事务性的SQL数据库引擎。它是一个零配置的数据库,这就体现出来SQLite与其他数据库的最大的区别:SQLite不需要在系统中配置,直接可以使用。且SQLite不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite可直接访问其存储文件。
1. 安装:System.Data.SQLite 组件

2. 创建数据表
首先需要在数据创建一张表,才能添加数据,有数据了才能演示查询,修改和删除。
创建一张Person表。
string dbfile = @"URI=file:sql.db";
SQLiteConnection cnn = new SQLiteConnection(dbfile);
cnn.Open();
string sql = "Create table Person (Id integer primary key, Name text);";
SQLiteCommand cmd = new SQLiteCommand(sql, cnn);
cmd.ExecuteNonQuery();
cnn.Close();
3.添加行数据
string dbfile = @"URI=file:sql.db";
SQLiteConnection cnn = new SQLiteConnection(dbfile);
cnn.Open();
string sql = "insert into Person (Id , Name) values(1,'Mike');";
SQLiteCommand cmd = new SQLiteCommand(sql, cnn);
cmd.ExecuteNonQuery();
cnn.Close();
Console.WriteLine("Insert row OK");
4. 查询数据
string dbfile = @"URI=file:sql.db";
SQLiteConnection cnn = new SQLiteConnection(dbfile);
cnn.Open();
string sql = "Select * From Person";
SQLiteCommand cmd = new SQLiteCommand(sql, cnn);
SQLiteDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
Console.WriteLine($"{
reader.GetInt32(0)} {
reader.GetString(1)} ");
}
reader.Close();
cnn.Close();
5. 更新数据
string dbfile = @"URI=file:sql.db";
SQLiteConnection cnn = new SQLiteConnection(dbfile);
cnn.Open();
string sql = "update Person set Name='Jim jones' where id=1;";
SQLiteCommand cmd = new SQLiteCommand(sql, cnn);
cmd.ExecuteNonQuery();
cnn.Close();
Console.WriteLine("Update row OK");
6.删除数据
string dbfile = @"URI=file:sql.db";
SQLiteConnection cnn = new SQLiteConnection(dbfile);
cnn.Open();
string sql = "delete from Person where id=3;";
SQLiteCommand cmd = new SQLiteCommand(sql, cnn);
cmd.ExecuteNonQuery();
cnn.Close();
Console.WriteLine("Delete row OK");
实现了基本的数据操作,增删改查。在日常应用中稍加修改就即可使用。
边栏推荐
- Little Red Book Keyword Search commodity list API interface (commodity detail page API interface)
- 小程序地理位置接口申请
- After reading this article, I also understand this
- CAD calls mobile command (COM interface)
- Circom 2.0: A Scalable Circuit Compiler
- 【类的组合(在一个类中定义一个类)】
- Codeforces Round #809 (Div. 2)(A~D2)
- Alibaba cloud and parallel cloud launched the cloud XR platform to support the rapid landing of immersive experience applications
- Mathematical derivation in [pumpkin Book ml] (task4) neural network
- Unity & facegood audio2face drives face blendshape with audio
猜你喜欢

使用frp实现内网穿透

From front-line development to technical director, you are almost on the shelf

Redefine analysis - release of eventbridge real-time event analysis platform

Composability and Recursion in snarkyJS

运动控制卡应用开发教程之调用激光振镜控制
![[image processing] pyefd.elliptic_ fourier_ How descriptors are used](/img/72/d2c825ddd95f541b37b98b2d7f6539.png)
[image processing] pyefd.elliptic_ fourier_ How descriptors are used

一键编译安装redis6.2.4

Thread pool learning

Today, there's a power failure for one day.... stop working for another day. Don't forget to study

图像处理笔记(1)图像增强
随机推荐
C# 回看委托与事件
[untitled]
ESP32C3 LED PWM使用和ESP32差异说明
From front-line development to technical director, you are almost on the shelf
Mathematical derivation in [pumpkin Book ml] (task4) neural network
What should I pay attention to when choosing the self built database access method on ECs?
What should I pay attention to when selecting DTS database type?
Can bank financial products be redeemed and transferred out on the same day?
巧妙使用sort(List<T>,Comparator<? super T>)比较器
中移链(基于EOS)测试环境搭建
Little Red Book Keyword Search commodity list API interface (commodity detail page API interface)
How to modify the IP address of kubernetes node?
Unity & facegood audio2face drives face blendshape with audio
You must make good use of MySQL and JSON
【南瓜书ML】(task4)神经网络中的数学推导
Uniqueness and ordering in set
Mysql database commands
Prefabricated financial voucher Bapi
MQ release confirmation
2022 Tsinghua summer school notes L2_ 2 basic introduction of CNN and RNN