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

C#操作SQLite數(shù)據(jù)庫方法小結(jié)

 更新時間:2022年06月08日 09:44:09   作者:springsnow  
這篇文章介紹了C#操作SQLite數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、SQLite介紹

1、SQLite 簡介

SQLite是一個開源、免費(fèi)的小型RDBMS(關(guān)系型數(shù)據(jù)庫),能獨(dú)立運(yùn)行、無服務(wù)器、零配置、支持事物,用C實現(xiàn),內(nèi)存占用較小,支持絕大數(shù)的SQL92標(biāo)準(zhǔn)。

這意味著與其他數(shù)據(jù)庫一樣,您不需要在系統(tǒng)中配置。SQLite 引擎不是一個獨(dú)立的進(jìn)程,可以按應(yīng)用程序需求進(jìn)行靜態(tài)或動態(tài)連接。SQLite 直接訪問其存儲文件。

SQLite 源代碼不受版權(quán)限制。SQLite數(shù)據(jù)庫官方主頁:http://www.sqlite.org/index.html

2、為什么要用 SQLite?

  • 不需要一個單獨(dú)的服務(wù)器進(jìn)程或操作的系統(tǒng)(無服務(wù)器的)。
  • SQLite 不需要配置,這意味著不需要安裝或管理。
  • 一個完整的 SQLite 數(shù)據(jù)庫是存儲在一個單一的跨平臺的磁盤文件。
  • SQLite 是非常小的,是輕量級的,完全配置時小于 400KiB,省略可選功能配置時小于250KiB。
  • SQLite 是自給自足的,這意味著不需要任何外部的依賴。
  • SQLite 事務(wù)是完全兼容 ACID 的,允許從多個進(jìn)程或線程安全訪問。
  • SQLite 支持 SQL92(SQL2)標(biāo)準(zhǔn)的大多數(shù)查詢語言的功能。
  • SQLite 使用 ANSI-C 編寫的,并提供了簡單和易于使用的 API。
  • SQLite 可在 UNIX(Linux, Mac OS-X, Android, iOS)和 Windows(Win32, WinCE, WinRT)中運(yùn)行。

3、SQLite 局限性

在 SQLite 中,SQL92 不支持的特性如下所示:

4、SQLite GUI客戶端列表:

二、C#操作數(shù)據(jù)庫

1、關(guān)于SQLite的connection string:

http://www.connectionstrings.com/sqlite/

基本方式:Data Source=c:\mydb.db;Version=3;

2、C#下SQLite操作

驅(qū)動dll下載:System.Data.SQLite

也Nuget 直接搜索安裝:System.Data.SQLite.Core

NuGet Gallery | SQLite

C#使用SQLite步驟:

(1)新建一個project

(2)添加SQLite操作驅(qū)動dll引用:System.Data.SQLite.dll

(3)使用API操作SQLite DataBase

using System;
using System.Data.SQLite;

namespace SQLiteSamples
{
    class Program
    {
        //數(shù)據(jù)庫連接
        SQLiteConnection m_dbConnection;

        static void Main(string[] args)
        {
            Program p = new Program();
        }

        public Program()
        {
            createNewDatabase();
            connectToDatabase();
            createTable();
            fillTable();
            printHighscores();
        }

        //創(chuàng)建一個空的數(shù)據(jù)庫
        void createNewDatabase()
        {
            SQLiteConnection.CreateFile("MyDatabase.db");//可以不要此句
        }

        //創(chuàng)建一個連接到指定數(shù)據(jù)庫
        void connectToDatabase()
        {
            m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.db;Version=3;");//沒有數(shù)據(jù)庫則自動創(chuàng)建
            m_dbConnection.Open();
        }

        //在指定數(shù)據(jù)庫中創(chuàng)建一個table
        void createTable()
        {
            string sql = "create table  if not exists highscores (name varchar(20), score int)";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }

        //插入一些數(shù)據(jù)
        void fillTable()
        {
            string sql = "insert into highscores (name, score) values ('Me', 3000)";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql = "insert into highscores (name, score) values ('Myself', 6000)";
            command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql = "insert into highscores (name, score) values ('And I', 9001)";
            command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }

        //使用sql查詢語句,并顯示結(jié)果
        void printHighscores()
        {
            string sql = "select * from highscores order by score desc";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            SQLiteDataReader reader = command.ExecuteReader();
            while (reader.Read())
                Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);
            Console.ReadLine();
        }
    }
}

到此這篇關(guān)于C#操作SQLite數(shù)據(jù)庫的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論