C#操作SQLite数据库方法小结

2023年 8月 13日 58.4k 0

一、sqlite介绍

1、SQLite 简介

SQLite是一个开源、免费的小型RDBMS(关系型数据库),能独立运行、无服务器、零配置、支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准。

这意味着与其他数据库一样,您不需要在系统中配置。SQLite 引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite 直接访问其存储文件。

SQLite 源代码不受版权限制。SQLite数据库官方主页:Http://www.sqlite.org/index.html

2、为什么要用 SQLite?

  • 不需要一个单独的服务器进程或操作的系统(无服务器的)。
  • SQLite 不需要配置,这意味着不需要安装或管理。
  • 一个完整的 SQLite 数据库是存储在一个单一的跨平台的磁盘文件。
  • SQLite 是非常小的,是轻量级的,完全配置时小于 400KiB,省略可选功能配置时小于250KiB。
  • SQLite 是自给自足的,这意味着不需要任何外部的依赖。
  • SQLite 事务是完全兼容 ACID 的,允许从多个进程或线程安全访问。
  • SQLite 支持 SQL92(SQL2)标准的大多数查询语言的功能。
  • SQLite 使用 ANSI-C 编写的,并提供了简单和易于使用的 api。
  • SQLite 可在 UNIX(linux, Mac OS-X, Android, iOS)和 windows(Win32, WinCE, WinRT)中运行。

3、SQLite 局限性

在 SQLite 中,SQL92 不支持的特性如下所示:

4、SQLite GUI客户端列表:

  • SQLite Expert :SQLite administration | SQLite Expert
  • SQLite Administrator:SQLite Administrator v0.8.3.2 绿色中文版
  • SQLiteStudio:SQLite数据库管理工具(SQLiteStudio) v3.1.1 多语中文绿色免费版
  • SQLite Database Browser:sqlite database browser(可视化数据库浏览器) v3.7.0 最新中文安装版
  • Navicat Premium :Navicat | 下载 Navicat for SQLite 14 天免费 Windows、macOS 和 Linux 的试用版

二、C#操作数据库

1、关于SQLite的connection string:

http://www.connectionstrings.com/sqlite/

基本方式:Data Source=c:mydb.db;Version=3;

2、C#下SQLite操作

驱动dll下载:System.Data.SQLite

也Nuget 直接搜索安装:System.Data.SQLite.Core

NuGet Gallery | SQLite

C#使用SQLite步骤:

(1)新建一个project

(2)添加SQLite操作驱动dll引用:System.Data.SQLite.dll

(3)使用API操作SQLite DataBase

using System;
using System.Data.SQLite;

namespace SQLiteSamples
{
class Program
{
//数据库连接
SQLiteConnection m_dbConnection;

static void Main(string[] args)
{
Program p = new Program();
}

public Program()
{
createNewDatabase();
connectToDatabase();
createTable();
fillTable();
printHighscores();
}

//创建一个空的数据库
void createNewDatabase()
{
SQLiteConnection.CreateFile("MyDatabase.db");//可以不要此句
}

//创建一个连接到指定数据库
void connectToDatabase()
{
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.db;Version=3;");//没有数据库则自动创建
m_dbConnection.Open();
}

//在指定数据库中创建一个table
void createTable()
{
string sql = "create table if not exists highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}

//插入一些数据
void fillTable()
{
string sql = "insert into highscores (name, score) values ('Me', 3000)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();

sql = "insert into highscores (name, score) values ('Myself', 6000)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();

sql = "insert into highscores (name, score) values ('And I', 9001)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}

//使用sql查询语句,并显示结果
void printHighscores()
{
string sql = "select * from highscores order by score desc";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
Console.WriteLine("Name: " + reader["name"] + "tScore: " + reader["score"]);
Console.ReadLine();
}
}
}

到此这篇关于C#操作SQLite数据库的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论