LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

如何在C#中實(shí)現(xiàn)SQLite基本操作

admin
2024年12月27日 21:35 本文熱度 437

前言

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 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲(chǔ)管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲欧美中文字幕在线一区一 | 天天视频在线观看免费专区 | 亚洲成高清日本亚洲成高清 | 亚洲欧美国产国产综合一区 | 久久久这里有精品 | 亚洲欧美中文字日韩二区 |