1. 程式人生 > >【C#】使用ADO.NET訪問Sqlite資料庫,SqliteHelper幫助類

【C#】使用ADO.NET訪問Sqlite資料庫,SqliteHelper幫助類

這個就比較簡單了,用過sqlserver的人都知道訪問資料庫的時候一般都會有個SqlHelper的幫助類,這裡就依葫蘆畫瓢寫了個SqliteHelper,很簡單:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Thumbnail
{
    public class
SqliteHelper { string dbPath = "E:\\thumbnail.db"; string conStr = "Data Source=E:\\thumbnail.db"; static SqliteHelper _instance; public static SqliteHelper Instance { get { if (_instance == null) _instance = new
SqliteHelper(); return _instance; } } string cmdCreateTable = "CREATE TABLE IF NOT EXISTS Thumbnails (Id integer PRIMARY KEY, OrginFilePath nvarchar NOT NULL UNIQUE, ThumbnailPath nvarchar NOT NULL,LastUpdateTime datetime NOT NULL);"; string createIndex = "create index if not exists originIndex on Thumbnails (OrginFilePath)"
; private SqliteHelper() { } //如果需要初始化資料庫可呼叫此方法 public void Init() { if (!File.Exists(dbPath)) { //如果資料庫檔案不存在,則建立 SQLiteConnection.CreateFile(dbPath); } List<TransModel> models = new List<TransModel>(); models.Add(new TransModel { CmdText = cmdCreateTable }); models.Add(new TransModel { CmdText = createIndex }); bool res = ExecTransaction(models);//一個事務:如果表不存在則建立,如果索引不存在則建立 } //執行非查詢的sql語句,返回受影響的行數 public int ExecuteNonQuery(string cmdText, params SQLiteParameter[] paramters) { using (SQLiteConnection con = new SQLiteConnection(conStr)) { try { con.Open(); using (SQLiteCommand cmd = new SQLiteCommand(con)) { cmd.CommandText = cmdText; if (paramters != null) cmd.Parameters.AddRange(paramters); return cmd.ExecuteNonQuery(); } } catch (SQLiteException ex) { //_log.E(ex); } return -1; } } //執行非查詢的sql語句,返回第一行第一列的值 public object ExecuteScalar(string cmdText, params SQLiteParameter[] parameters) { using (SQLiteConnection conn = new SQLiteConnection(conStr)) { try { conn.Open(); using (SQLiteCommand cmd = new SQLiteCommand(conn)) { cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } catch (SQLiteException ex) { //_log.E(ex); } return null; } } //執行查詢語句,返回查詢到的結果 public DataTable GetDataTable(string cmdText, params SQLiteParameter[] parameters) { using (SQLiteConnection conn = new SQLiteConnection(conStr)) { try { conn.Open(); using (SQLiteCommand cmd = new SQLiteCommand(conn)) { DataTable dt = new DataTable(); cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); SQLiteDataAdapter da = new SQLiteDataAdapter(cmd); da.Fill(dt); return dt; } } catch (SQLiteException ex) { //_log.E(ex); } return null; } } //執行事務,如果出現異常則回滾 public bool ExecTransaction(List<TransModel> models) { using (SQLiteConnection con = new SQLiteConnection(conStr)) { try { con.Open(); using (SQLiteTransaction trans = con.BeginTransaction()) { using (SQLiteCommand cmd = new SQLiteCommand(con)) { cmd.Transaction = trans; try { foreach (var model in models) { cmd.CommandText = model.CmdText; if (model.Paras != null) cmd.Parameters.AddRange(model.Paras); cmd.ExecuteNonQuery(); } trans.Commit(); return true; } catch (SQLiteException ex) { trans.Rollback(); //_log.E(ex); } } } } catch (SQLiteException ex) { // _log.E(ex); } } return false; } } public class TransModel { public string CmdText { get; set; } public SQLiteParameter[] Paras { get; set; } } }

這裡有幾點需要注意,所有之前的SqlConnection都換成SQLiteConnection,command也要用SQLiteCommand,即一切與SQL相關到都要改為SQLite對應的,也包括要用SQLiteParameter。最後用try-catch捕獲異常時一定要catch SQLiteExcepiton這個異常,不要捕獲Exception否則你拿到的異常物件就會是null。

相關推薦

C#使用ADO.NET訪問Sqlite資料庫SqliteHelper幫助

這個就比較簡單了,用過sqlserver的人都知道訪問資料庫的時候一般都會有個SqlHelper的幫助類,這裡就依葫蘆畫瓢寫了個SqliteHelper,很簡單: using System; using System.Collections.Generic

C#使用EF訪問Sqlite資料庫

1. 先上Nuget下載對應的包 如圖,搜尋System.Data.SQLite下載安裝即可,下載完之後帶上依賴一共有這麼幾個: EntityFramework System.Data.SQLite System.Data.SQLite.C

SQLite資料庫SQLiteHelper幫助

using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.Common; using System.Data

ObjectARX--建立和訪問圖形資料庫(DwgDatabase)

(1)使用ObjectARX建立新工程DwgDatabase,選擇MFC支援。 (2)註冊一個命令CreateDwg建立一個新的圖形檔案,並儲存在AutoCAD的安裝路徑中. 實現函式為: static void AAAMyGroupCreateDwg() {

在.NET中利用SQLite ADO.NET使用SQLite資料庫

System.Data.SQLite 是一個原始SQLite的加強版. 它將是一個原版的sqlite3.dll完全替代品 (你甚至就可以把它重新命名為sqlite3.dll). 它不需要連結.NET 執行時,所以可以脫離.NET獨立釋出, 然而它內嵌了一個完整的 ADO.NET 2.0 引擎,為開發提供了

C#54. .Net中的併發集合——ConcurrentDictionary

轉載自《C#多執行緒程式設計例項》 對於平行計算,我們需要使用適當的資料結構。這些結構具備可伸縮性,儘可能地避免鎖,並且還能提供執行緒安全的訪問。.Net Framework 引入了System.Collections.Concurrent 名稱空間,包含了一些資料結構。

C#用ado.net訪問EXCEL(含EXCEL2010)的常見問題及解決方法

C#用ado.net訪問EXCEL的常見問題及解決方法,除了像sql server,access常見的資料庫,其實Excel檔案也可以做為資料庫訪問。 ado.net訪問excel的例項: OleDbConnection cn = new OleDbConnectio

C++如何提高Cache的命中率示例

參考連結     https://stackoverflow.com/questions/16699247/what-is-a-cache-friendly-code   只是堆積:快取不友好與快取友好程式碼的典型例子是矩陣乘法的“快取阻塞”。 樸素矩陣乘法看起來

c檔案操作函式:fprintffreadfwritefseekftellfopenfclosefflush以及獲取檔案長度示例

Date: 2018.9.20 1、參考 2、 fprintf 3、fread 作用:從一個檔案流中讀取資料。 Read block of data from stream Reads an ar

面試常見問題C++指標和引用的區別有哪些不同點細細道1

首先咱們弄清楚複合型別(Compound type)這個概念,因為指標和引用是Compound type中的兩種! 引用(reference): 何謂“引用”,這麼說吧,相信每個人都有個乳名,後來等到你升學入職啥的發現乳名B格很Low ! 這時候需要為自己起另外一個名字

C#解決MouseHook捕獲滑鼠動作在有些電腦上SetWindowsHookEx失敗返回0的問題

最近在debug滑鼠位置捕獲的功能時發現在其中的一臺開發電腦上,SetWindowsHookEx一直返回0,導致Hook設定失敗,有時候調成Release模式又是正常的。程式碼如下: hMouseHook = SetWindowsHookEx(WH_MOU

PATC++讀入一個自然數n計算其各位數字之和用漢語拼音寫出和的每一位數字。

讀入一個自然數n,計算其各位數字之和,用漢語拼音寫出和的每一位數字。 輸入格式:每個測試輸入包含1個測試用例,即給出自然數n的值。這裡保證n小於10100。 輸出格式:在一行內輸出n的各位數字之和的每一位,拼音數字間有1 空格,但一行中最後一個拼音數字後沒有空格。 輸

更新FastReport.Net v2019.3釋出新增數字簽名|附下載

FastReport.Net是適用於Windows Forms,ASP.NET,MVC和.NET Core的全功能報表解決方案。

C#C#訪問和操作MYSQL資料庫

這裡介紹下比較簡單的方式,引用MySql.Data.dll然後新增一個MySqlHelper類來對MySql資料庫進行訪問和操作。 1.將MySql.Data.dll引用到你的專案中 新增引用後在你的資料庫操作類(如MySqlHelper.cs)中新增引用宣告

ADO.NET基礎-SessionSession的基本應用

ear 重定向 session 存儲 null tran 過去 方式 string 在服務端存儲狀態的對象:Session和Application 在客戶端存儲狀態的對象:Cookie 1.Session:每個獨立的瀏覽器都會創建一個獨立的Session,不是一臺電腦一個S

C#EF學習<二> DbFirst (先建立資料庫表及其關聯關係)

工程壓縮檔案放到百度雲盤---20181019001資料夾   1. 建立表的指令碼   create table Teacher ( TID char(12) primary key, Tname char(6) not null ) create table

Asp.net——ADO.NET介紹

前言        ADO.NET是對Microsoft ActiveX Data Object(ADO)一個跨時代的改進,它提供了平臺互用性和可伸縮性的資料訪問。由於傳送的資料都是XML格式的,因此任何能夠讀取XML格式的應用程式

C#xUnitMoq.NET單元測試Mock框架Moq初探!

在TDD開發模型中,經常是在編碼的同時進行單元測試的編寫,由於現代軟體開發不可能是一個人完成的工作,所以在定義好介面的時候我們就可以進行自己功能的開發(介面不能經常變更),而我們呼叫他人的功能時只需要使用介面即可。 但我們在編寫自己的單元測試並進行功能驗證的時候,如果介面的實現人還沒有完成程式碼怎麼

c#連線資料庫相關知識

知識回顧 1、儲存過程: 是在大型資料庫系統中,一組為了完成特定功能的SQL 語句集,儲存在資料庫中,經過第一次編譯後再次呼叫不需要再次編譯,使用者通過指定儲存過程的名字並給出引數(如果該儲存過程

使用ADO.NET訪問資料庫 使用ADO.NET訪問資料庫

使用ADO.NET訪問資料庫   Program using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks