欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

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

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

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

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

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í)行語句
        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í)行語句
        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í)行語句
        command.ExecuteNonQuery();
    }
}

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

相關(guān)文章

  • C#生成Code39條形碼而非條形碼字體的方法

    C#生成Code39條形碼而非條形碼字體的方法

    由于Code39編譯簡單、能夠?qū)θ我忾L度的數(shù)據(jù)進(jìn)行編碼、支持設(shè)備比較廣泛所以被廣泛的采用,下面介紹下C#生成Code39條形碼而非條形碼字體的方法,需要的朋友可以參考下
    2015-07-07
  • 采用easyui tree編寫簡單角色權(quán)限代碼的方法

    采用easyui tree編寫簡單角色權(quán)限代碼的方法

    本文主要介紹了如何采用easyui tree編寫簡單角色權(quán)限代碼,文章思路清晰,需要的朋友可以參考下
    2015-07-07
  • 關(guān)于ASP網(wǎng)頁無法打開的解決方案

    關(guān)于ASP網(wǎng)頁無法打開的解決方案

    asp網(wǎng)頁實(shí)際上就是動(dòng)態(tài)網(wǎng)頁,是在服務(wù)端執(zhí)行和解析的。有時(shí)也很奇怪,經(jīng)常遇到asp網(wǎng)頁無法打開的情況,下面小編給大家整理些關(guān)于asp網(wǎng)頁無法打開的解決方案,需要的朋友可以參考下
    2015-08-08
  • C#使用Aforge調(diào)用攝像頭拍照的方法

    C#使用Aforge調(diào)用攝像頭拍照的方法

    這篇文章主要為大家詳細(xì)介紹了C#使用Aforge調(diào)用攝像頭拍照的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • C#使用DevExpress中的SplashScreenManager控件實(shí)現(xiàn)啟動(dòng)閃屏和等待信息窗口

    C#使用DevExpress中的SplashScreenManager控件實(shí)現(xiàn)啟動(dòng)閃屏和等待信息窗口

    這篇文章介紹了C#使用DevExpress中的SplashScreenManager控件實(shí)現(xiàn)啟動(dòng)閃屏和等待信息窗口的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • 在Winform框架界面中改變并存儲(chǔ)界面皮膚樣式的方法

    在Winform框架界面中改變并存儲(chǔ)界面皮膚樣式的方法

    下面小編就為大家分享一篇在Winform框架界面中改變并存儲(chǔ)界面皮膚樣式的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2017-11-11
  • c++換行符知識(shí)點(diǎn)總結(jié)

    c++換行符知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于c++換行符知識(shí)點(diǎn)總結(jié),需要的朋友們可以參考學(xué)習(xí)下。
    2020-03-03
  • C#多態(tài)詳解

    C#多態(tài)詳解

    這篇文章主要介紹了C#中的多態(tài),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-10-10
  • c#二叉樹存儲(chǔ)介紹

    c#二叉樹存儲(chǔ)介紹

    大家好,本篇文章主要講的是c#二叉樹存儲(chǔ)介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • vs2005中總是保留最近打開的項(xiàng)目和文件的記錄

    vs2005中總是保留最近打開的項(xiàng)目和文件的記錄

    這篇文章主要介紹了vs2005中總是保留最近打開的項(xiàng)目和文件的記錄,需要的朋友可以參考下
    2016-06-06

最新評(píng)論