最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#連接SQLite數(shù)據(jù)庫(kù)并實(shí)現(xiàn)基本操作

 更新時(shí)間:2024年12月31日 09:21:26   作者:我曾經(jīng)是個(gè)程序員  
本文介紹了SQLite,一個(gè)輕量級(jí)的跨平臺(tái)數(shù)據(jù)庫(kù)管理系統(tǒng),以及如何在C#中使用System.Data.SQLite庫(kù)進(jìn)行操作,包括創(chuàng)建、修改和查詢數(shù)據(jù)庫(kù),以及使用SQLiteHelper類簡(jiǎn)化SQL使用,此外,還提到了DB文件查看工具SQLiteSpy的應(yīng)用,需要的朋友可以參考下

1.安裝并引用System.Data.SQLite

通過(guò)NuGet包管理器安裝,Install-Package System.Data.SQLite

2.創(chuàng)建數(shù)據(jù)庫(kù)

string dbFilename =  "test.db";
if (!File.Exists(dbFilename))
{
    SQLiteConnection.CreateFile(dbFilename);
}

3.設(shè)置數(shù)據(jù)庫(kù)密碼

string connectionString = string.Format("Data Source={0};Version=3;",dbFilename);
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
    connection.Open();//打開(kāi)數(shù)據(jù)庫(kù)
    connection.ChangePassword("123456");//設(shè)置密碼
}

4.連接數(shù)據(jù)庫(kù)

string connectionString =string.Format("Data Source={0}; Version=3; Password={1};",dbFilename,"123456");
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
    connection.Open();
}

5.創(chuàng)建表

using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
    connection.Open();
string commandText = "CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name VARCHAR(100), Code VARCHAR(100),Password VARCHAR(100))";

using (SQLiteCommand command = new SQLiteCommand(commandText, connection))
    {
        command.ExecuteNonQuery();//執(zhí)行sql
    }
}

6.添加數(shù)據(jù)

using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
    connection.Open();
string commandText = "insert into Users (Name, Code,Password) values (@name, @code,@password)";
using (SQLiteCommand command = new SQLiteCommand(commandText, connection))
    {
        // 設(shè)置參數(shù)值
        command.Parameters.AddWithValue("@name", "管理員");
        command.Parameters.AddWithValue("@code", "admin");
        command.Parameters.AddWithValue("@password", "123456");
        // 執(zhí)行語(yǔ)句
        command.ExecuteNonQuery();
    }
}

7.修改數(shù)據(jù)

using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
    connection.Open();
    string commandText = "update Users SET Password=@password WHERE Code = @code";
    using (SQLiteCommand command = new SQLiteCommand(commandText, connection))
    {
        // 設(shè)置參數(shù)值
        command.Parameters.AddWithValue("@code", "admin");
        command.Parameters.AddWithValue("@password", "admin123456");
        // 執(zhí)行語(yǔ)句
        command.ExecuteNonQuery();
    }
}

8.查詢數(shù)據(jù)

using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
    connection.Open();
    string commandText  = "select * from Users";
    using (SQLiteCommand command = new SQLiteCommand(commandText, connection))
    {
        using (SQLiteDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine($"ID: {reader["Id"]}, 名稱: {reader["Name"]}, 編碼: {reader["Code"]}");
            }
        }
    }
}

9.刪除數(shù)據(jù)

using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
    connection.Open();
    string commandText = "delete from  Users where Code = @code";
    using (SQLiteCommand command = new SQLiteCommand(sql, connection))
    {
        // 設(shè)置參數(shù)值
        command.Parameters.AddWithValue("@code", "admin");
        // 執(zhí)行語(yǔ)句
        command.ExecuteNonQuery();
    }
}

以上就是C#連接SQLite數(shù)據(jù)庫(kù)并實(shí)現(xiàn)基本操作的詳細(xì)內(nèi)容,更多關(guān)于C#連接SQLite的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 異步/多線程/任務(wù)/并行編程之一:如何選擇合適的多線程模型?

    異步/多線程/任務(wù)/并行編程之一:如何選擇合適的多線程模型?

    本篇文章小編為大家介紹,異步/多線程/任務(wù)/并行編程之一:如何選擇合適的多線程模型?需要的朋友參考下
    2013-04-04
  • C#使用隨機(jī)數(shù)編寫班級(jí)點(diǎn)名器的示例代碼

    C#使用隨機(jī)數(shù)編寫班級(jí)點(diǎn)名器的示例代碼

    本文主要介紹了C#使用隨機(jī)數(shù)編寫班級(jí)點(diǎn)名器的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 利用C#實(shí)現(xiàn)在Word中更改字體顏色

    利用C#實(shí)現(xiàn)在Word中更改字體顏色

    在日常工作中,我們有時(shí)會(huì)需要修改字體的顏色來(lái)突出文本重點(diǎn),讓讀者更容易抓住文章要點(diǎn)。在今天這篇文章中,我將為大家介紹如何以編程方式,在Word更改字體顏色,感興趣的可以了解一下
    2023-02-02
  • c#動(dòng)態(tài)改變webservice的url訪問(wèn)地址

    c#動(dòng)態(tài)改變webservice的url訪問(wèn)地址

    這篇文章主要介紹了c#動(dòng)態(tài)改變webservice的url訪問(wèn)地址,需要的朋友可以參考下
    2014-03-03
  • Unity UGUI的RectMask2D遮罩組件的介紹使用

    Unity UGUI的RectMask2D遮罩組件的介紹使用

    這篇文章主要為大家介紹了Unity UGUI的RectMask2D遮罩組件的介紹使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • C#畫圓角矩形的方法

    C#畫圓角矩形的方法

    這篇文章主要介紹了C#畫圓角矩形的方法,涉及C#繪圖的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-05-05
  • C#使用jQuery實(shí)現(xiàn)無(wú)刷新評(píng)論提交的方法

    C#使用jQuery實(shí)現(xiàn)無(wú)刷新評(píng)論提交的方法

    這篇文章主要介紹了C#使用jQuery實(shí)現(xiàn)無(wú)刷新評(píng)論提交的方法,涉及C#結(jié)合jQuery進(jìn)行Ajax操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-05-05
  • C# 讀寫ini文件操作實(shí)現(xiàn)

    C# 讀寫ini文件操作實(shí)現(xiàn)

    本文主要介紹了C# 讀寫ini文件操作實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • c#生成自定義圖片方法代碼實(shí)例

    c#生成自定義圖片方法代碼實(shí)例

    在本篇文章中我們給大家分享了關(guān)于c#生成自定義圖片方法的相關(guān)內(nèi)容,有需要的朋友們可以參考下。
    2018-10-10
  • C#中dynamic的使用方法及應(yīng)用場(chǎng)景

    C#中dynamic的使用方法及應(yīng)用場(chǎng)景

    在 C# 編程中,dynamic 類型是一個(gè)非常特殊的類型,它在編譯時(shí)并不會(huì)進(jìn)行類型檢查,而是在運(yùn)行時(shí)才進(jìn)行類型解析,本文將詳細(xì)講解 dynamic 的使用方法、優(yōu)缺點(diǎn)以及一些實(shí)際應(yīng)用場(chǎng)景,需要的朋友可以參考下
    2024-08-08

最新評(píng)論

诸暨市| 南汇区| 天长市| 郸城县| 新建县| 麻阳| 那曲县| 兴山县| 彰武县| 无为县| 保山市| 广南县| 崇阳县| 银川市| 尉氏县| 宁河县| 大丰市| 海伦市| 浦江县| 遵化市| 徐州市| 康平县| 西青区| 开平市| 华宁县| 临澧县| 阿拉善右旗| 达孜县| 新建县| 新闻| 灵台县| 荔波县| 二连浩特市| 神农架林区| 中阳县| 于都县| 沿河| 铜川市| 郓城县| 铁力市| 丰镇市|