前言
SQLite是一款非常輕量級(jí)的關(guān)系數(shù)據(jù)庫系統(tǒng),以SQL為基礎(chǔ),并支持多數(shù)SQL92標(biāo)準(zhǔn)。由于其輕量、易用和跨平臺(tái)特性而被廣泛使用。使用SQLite時(shí),通過訪問數(shù)據(jù)庫的程序直接從磁盤上的數(shù)據(jù)庫文件進(jìn)行讀寫操作。本文探討如何在C#中實(shí)現(xiàn)操作SQLite數(shù)據(jù)庫,主要通過連接數(shù)據(jù)庫、執(zhí)行增、刪、改和查等基本操作。
實(shí)現(xiàn)操作
1、實(shí)現(xiàn)前提
在C#實(shí)現(xiàn)SQLite數(shù)據(jù)庫操作需要引用System.Data.SQLite,我們可以通過NuGet包管理器安裝引用它。
Install-Package System.Data.SQLite
SQLite是直接訪問磁盤上的數(shù)據(jù)庫文件,因此在執(zhí)行相關(guān)操作前,需要?jiǎng)?chuàng)建好SQLite數(shù)據(jù)庫文件。數(shù)據(jù)庫名的后綴可以直接指定,甚至沒有后綴都可以。
// 創(chuàng)建數(shù)據(jù)庫 方式一
string dbFilename = string.Format("{0}db{1}{2}", AppDomain.CurrentDomain.BaseDirectory, Path.PathSeparator, "test.db");
if (!File.Exists(dbFilename))
{
// 創(chuàng)建數(shù)據(jù)庫文件
FileStream fileStream = File.Create(dbFilename);
}
// 創(chuàng)建數(shù)據(jù)庫 方式二
string dbFilename = string.Format("{0}db{1}{2}", AppDomain.CurrentDomain.BaseDirectory, Path.PathSeparator, "test.db");
if (!File.Exists(dbFilename))
{
// 創(chuàng)建數(shù)據(jù)庫文件
SQLiteConnection.CreateFile(dbFilename);
}
2、連接數(shù)據(jù)庫
下面代碼段演示如何連接SQLite數(shù)據(jù)庫:
// 數(shù)據(jù)庫未設(shè)置密碼
string connectionString =string.Format("Data Source={0}; Version=3; ",dbFilename);
// 連接數(shù)據(jù)庫
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
// 打開數(shù)據(jù)庫連接
connection.Open();
}
// 數(shù)據(jù)庫設(shè)置了密碼
string connectionString =string.Format("Data Source={0}; Version=3; Password={1};",dbFilename,"123456");
// 連接數(shù)據(jù)庫
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
// 打開數(shù)據(jù)庫連接
connection.Open();
}
3、設(shè)置數(shù)據(jù)庫密碼
下面代碼段演示給未設(shè)置密碼的數(shù)據(jù)庫設(shè)置密碼:
// 數(shù)據(jù)庫未設(shè)置密碼
string connectionString = string.Format("Data Source={0};Version=3;",dbFilename);
// 連接數(shù)據(jù)庫
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
// 打開數(shù)據(jù)庫連接
connection.Open();
// 設(shè)置密碼
connection.ChangePassword("123456");
}
4、創(chuàng)建數(shù)據(jù)表
下面代碼段演示在數(shù)據(jù)庫里創(chuàng)建數(shù)據(jù)表,如用戶表:
// 數(shù)據(jù)庫未設(shè)置密碼
string connectionString = string.Format("Data Source={0};Version=3;",dbFilename);
// 連接數(shù)據(jù)庫
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
// 打開數(shù)據(jù)庫連接
connection.Open();
// 執(zhí)行SQL的語句
string commandText = "CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name VARCHAR(100), Code VARCHAR(100),Password VARCHAR(100))";
// 創(chuàng)建 SQLiteCommand
using (SQLiteCommand command = new SQLiteCommand(commandText, connection))
{
// 執(zhí)行語句
command.ExecuteNonQuery();
}
}
5、增加數(shù)據(jù)庫表數(shù)據(jù)
下面代碼段演示往用戶表增加一行數(shù)據(jù):
?// 數(shù)據(jù)庫未設(shè)置密碼
string connectionString = string.Format("Data Source={0};Version=3;",dbFilename);
// 連接數(shù)據(jù)庫
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
// 打開數(shù)據(jù)庫連接
connection.Open();
// 執(zhí)行SQL的語句
string commandText = "insert into Users (Name, Code,Password) values (@name, @code,@password)";
// 創(chuàng)建 SQLiteCommand
using (SQLiteCommand command = new SQLiteCommand(commandText, connection))
{
// 設(shè)置參數(shù)值
command.Parameters.AddWithValue("@name", "管理員");
command.Parameters.AddWithValue("@code", "admin");
command.Parameters.AddWithValue("@password", "pwd123456");
// 執(zhí)行語句
command.ExecuteNonQuery();
}
}
6、修改數(shù)據(jù)庫表數(shù)據(jù)
下面代碼段演示修改數(shù)據(jù)庫表數(shù)據(jù),如修改用戶密碼:
// 數(shù)據(jù)庫未設(shè)置密碼
string connectionString = string.Format("Data Source={0};Version=3;",dbFilename);
// 連接數(shù)據(jù)庫
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
// 打開數(shù)據(jù)庫連接
connection.Open();
// 執(zhí)行SQL的語句
string commandText = "update Users SET Password=@password WHERE Code = @code";
// 創(chuàng)建 SQLiteCommand
using (SQLiteCommand command = new SQLiteCommand(commandText, connection))
{
// 設(shè)置參數(shù)值
command.Parameters.AddWithValue("@code", "admin");
command.Parameters.AddWithValue("@password", "admin123456");
// 執(zhí)行語句
command.ExecuteNonQuery();
}
}
7、查詢數(shù)據(jù)庫表數(shù)據(jù)
下面代碼段演示查詢數(shù)據(jù)庫表數(shù)據(jù),如查詢用戶表數(shù)據(jù):
// 數(shù)據(jù)庫未設(shè)置密碼
string connectionString = string.Format("Data Source={0};Version=3;",dbFilename);
// 連接數(shù)據(jù)庫
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
// 打開數(shù)據(jù)庫連接
connection.Open();
// 執(zhí)行SQL的語句
string commandText = "select * from Users";
// 創(chuàng)建 SQLiteCommand
using (SQLiteCommand command = new SQLiteCommand(commandText, connection))
{
// 執(zhí)行語句 返回查詢數(shù)據(jù)
using (SQLiteDataReader reader = command.ExecuteReader())
{
// 輸出數(shù)據(jù)
while (reader.Read())
{
//
Console.WriteLine($"ID: {reader["Id"]}, 名稱: {reader["Name"]}, 編碼: {reader["Code"]}");
}
}
}
}
8、刪除數(shù)據(jù)庫表數(shù)據(jù)
下面代碼段演示刪除數(shù)據(jù)庫表數(shù)據(jù),如刪除用戶表數(shù)據(jù):
// 數(shù)據(jù)庫未設(shè)置密碼
string connectionString = string.Format("Data Source={0};Version=3;",dbFilename);
// 連接數(shù)據(jù)庫
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
// 打開數(shù)據(jù)庫連接
connection.Open();
// 執(zhí)行SQL的語句
string commandText = "delete from Users where Code = @code";
// 創(chuàng)建 SQLiteCommand
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
// 設(shè)置參數(shù)值
command.Parameters.AddWithValue("@code", "admin");
// 執(zhí)行語句
command.ExecuteNonQuery();
}
}
小結(jié)
通過上述示例,能夠清晰地了解如何在C#中有效地操作SQLite數(shù)據(jù)庫,并快速上手。可在此基礎(chǔ)上擴(kuò)展更復(fù)雜的功能,并在實(shí)際項(xiàng)目中運(yùn)用。
該文章在 2024/12/28 12:08:16 編輯過