日韩欧美人妻无码精品白浆,www.大香蕉久久网,狠狠的日狠狠的操,日本好好热在线观看

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

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

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

前言

SQLite是一款非常輕量級的關(guān)系數(shù)據(jù)庫系統(tǒng),以SQL為基礎(chǔ),并支持多數(shù)SQL92標準。由于其輕量、易用和跨平臺特性而被廣泛使用。使用SQLite時,通過訪問數(shù)據(jù)庫的程序直接從磁盤上的數(shù)據(jù)庫文件進行讀寫操作。本文探討如何在C#中實現(xiàn)操作SQLite數(shù)據(jù)庫,主要通過連接數(shù)據(jù)庫、執(zhí)行增、刪、改和查等基本操作。

實現(xiàn)操作

1、實現(xiàn)前提

C#實現(xiàn)SQLite數(shù)據(jù)庫操作需要引用System.Data.SQLite,我們可以通過NuGet包管理器安裝引用它。

Install-Package System.Data.SQLite

SQLite是直接訪問磁盤上的數(shù)據(jù)庫文件,因此在執(zhí)行相關(guān)操作前,需要創(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ǔ)上擴展更復(fù)雜的功能,并在實際項目中運用。


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

AV少妇偷人| 欧美日韩一区二区激情影院| 黄色午夜精品电影院| 国产日韩欧美在线淋| 天天躁月 躁狠狠躁| 欧每日视频精品一级视频| 在线色综合| 日本一区四区不卡视频| 久久蜜人人爽| 亚洲精品九九九九九九久| 一区二区精品一区| 日日夜夜操天天操狠狠操天天操| 国产吹高潮麻豆| 农村妇女挨操视频| 在线观看免费黄小说| 人人人人人人妻妻色| 人人操人人爽| 一区 二区 三区中文字幕| 99 9三级| bl床戏欧美国产一区二区| 欧美在线一区二区三区四| 久久蜜网| 成人超碰在| 扶沟县| 日韩AVSm伦理| 日韩少妇换夫| 熟女自拍日韩第6页| 精品 中文字幕 一区二区三区| 国欧美日韩综合| 国产欧美日韩成人在线专区| 亚洲AV电影精品一区二区| 91久久久精品国产一区二区欧K| 有码影片中文字幕日韩 | 毛片午夜激情小视频| 妖精视频一区二区三区在线观看| 91天天奭福利姬| 91从后入直接观看| 自拍偷拍亚洲州欧美熟女人妻| 欧美二区性爱| 又粗又大又长又黄在线观看 | 99热这里只有精品国产16|