C#操作SQLite數據庫方法小結
一、SQLite介紹
1、SQLite 簡介
SQLite是一個開源、免費的小型RDBMS(關系型數據庫),能獨立運行、無服務器、零配置、支持事物,用C實現,內存占用較小,支持絕大數的SQL92標準。
這意味著與其他數據庫一樣,您不需要在系統(tǒng)中配置。SQLite 引擎不是一個獨立的進程,可以按應用程序需求進行靜態(tài)或動態(tài)連接。SQLite 直接訪問其存儲文件。
SQLite 源代碼不受版權限制。SQLite數據庫官方主頁:http://www.sqlite.org/index.html

2、為什么要用 SQLite?
- 不需要一個單獨的服務器進程或操作的系統(tǒng)(無服務器的)。
- 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

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();
}
//創(chuàng)建一個空的數據庫
void createNewDatabase()
{
SQLiteConnection.CreateFile("MyDatabase.db");//可以不要此句
}
//創(chuàng)建一個連接到指定數據庫
void connectToDatabase()
{
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.db;Version=3;");//沒有數據庫則自動創(chuàng)建
m_dbConnection.Open();
}
//在指定數據庫中創(chuàng)建一個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數據庫的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

