日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

PC软件开发技术之三:C#操作SQLite数据库

發布時間:2024/7/23 C# 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PC软件开发技术之三:C#操作SQLite数据库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們在開發應用是經常會需要用到一些數據的存儲,存儲的方式有多種,使用數據庫是一種比較受大家歡迎的方式。但是對于一些小型的應用,如一些移動APP,通常的數據庫過于龐大,而輕便的SQLite則能解決這一問題。不但操作方便,而且只需要要一個文件即可,在這里我們來說一說使用C#語言操作SQLite數據庫。

1、SQLite簡介

SQLite,是一款輕型的數據庫,是遵守ACID的關系型數據庫管理系統,它包含在一個相對小的C庫中。它是D.RichardHipp建立的公有領域項目。它的設計目標是嵌入式的,而且目前已經在很多嵌入式產品中使用了它,它占用資源非常的低,在嵌入式設備中,可能只需要幾百K的內存就夠了。它能夠支持Windows/Linux/Unix等等主流的操作系統,同時能夠跟很多程序語言相結合,比如 Tcl、C#、PHP、Java等,還有ODBC接口,同樣比起MySQL、PostgreSQL這兩款開源的世界著名數據庫管理系統來講,它的處理速度比他們都快。

如果想了解更多關于SQLite的問題,可以訪問它的官方網站:http://www.sqlite.org/

2、開始前的準備

在開始之前我們需要準備必要的開發環境,這次咱們使用的是Visual Studio 2015開發環境,但是我們開發基于SQLite的應用光有VS2015還不夠。我需要到SQLite的官方網站下載并安裝SQLite。

在SQLite官網找到下載,有應用于各種環境的SQLite組件及源碼,我們選擇Precompiled Binaries for .NET,這是應用于.NET開發環境的,點擊進入會看到應用于.NET2.0直至4.6以及32位和64位平臺的各個版本。我們選擇Setups for 32-bit Windows (.NET Framework 4.6)下載安裝即可。

3、C#操作SQLite的封裝

在完成開發環境的準備之后,我們接下來實現對SQLite操作的必要封裝,以進一步降低在具體應用中的使用難度。在這里我們只是封裝一些常用而且必要的功能。???

?public class SQLiteHelper{//創建數據庫文件public static void CreateDBFile(string fileName){string path = System.Environment.CurrentDirectory + @"/Data/";if (!Directory.Exists(path)){Directory.CreateDirectory(path);}string databaseFileName = path + fileName;if (!File.Exists(databaseFileName)){SQLiteConnection.CreateFile(databaseFileName);}}//生成連接字符串private static string CreateConnectionString(){SQLiteConnectionStringBuilder connectionString = new SQLiteConnectionStringBuilder();connectionString.DataSource = @"data/ScriptHelper.db";string conStr = connectionString.ToString();return conStr;}/// <summary>/// 對插入到數據庫中的空值進行處理/// </summary>/// <param name="value"></param>/// <returns></returns>public static object ToDbValue(object value){if (value == null){return DBNull.Value;}else{return value;}}/// <summary>/// 對從數據庫中讀取的空值進行處理/// </summary>/// <param name="value"></param>/// <returns></returns>public static object FromDbValue(object value){if (value == DBNull.Value){return null;}else{return value;}}/// <summary>/// 執行非查詢的數據庫操作/// </summary>/// <param name="sqlString">要執行的sql語句</param>/// <param name="parameters">參數列表</param>/// <returns>返回受影響的條數</returns>public static int ExecuteNonQuery(string sqlString, params SQLiteParameter[] parameters){string connectionString=CreateConnectionString();using (SQLiteConnection conn = new SQLiteConnection(connectionString)){conn.Open();using (SQLiteCommand cmd = conn.CreateCommand()){cmd.CommandText = sqlString;foreach (SQLiteParameter parameter in parameters){cmd.Parameters.Add(parameter);}return cmd.ExecuteNonQuery();}}}/// <summary>/// 執行查詢并返回查詢結果第一行第一列/// </summary>/// <param name="sqlString">SQL語句</param>/// <param name="sqlparams">參數列表</param>/// <returns></returns>public static object ExecuteScalar(string sqlString, params SQLiteParameter[] parameters){string connectionString = CreateConnectionString();using (SQLiteConnection conn = new SQLiteConnection(connectionString)){conn.Open();using (SQLiteCommand cmd = conn.CreateCommand()){cmd.CommandText = sqlString;foreach (SQLiteParameter parameter in parameters){cmd.Parameters.Add(parameter);}return cmd.ExecuteScalar();}}}/// <summary>/// 查詢多條數據/// </summary>/// <param name="sqlString">SQL語句</param>/// <param name="parameters">參數列表</param>/// <returns>返回查詢的數據表</returns>public static DataTable GetDataTable(string sqlString,params SQLiteParameter[] parameters){string connectionString = CreateConnectionString();using (SQLiteConnection conn = new SQLiteConnection(connectionString)){conn.Open();using (SQLiteCommand cmd = conn.CreateCommand()){cmd.CommandText = sqlString;foreach (SQLiteParameter parameter in parameters){cmd.Parameters.Add(parameter);}DataSet ds = new DataSet();SQLiteDataAdapter adapter = new SQLiteDataAdapter(cmd);adapter.Fill(ds);return ds.Tables[0];}}}}

4、應用實例

上面封裝完了之后,我們就是使用上面封裝的函數來實際操作SQLite數據庫。對數據庫的應用實例無非就是對各種對象的增、刪、改、查。我沒列舉一個對源碼類型對象的各種操作實例如下:

????

public class ScriptTypeDAL{public ScriptTypeM ToScriptType(DataRow row){ScriptTypeM type = new ScriptTypeM();type.ScriptTypeId = (Guid)row["ScriptTypeId"];type.ScriptType = (string)row["ScriptType"];type.IsUsing = (bool)row["IsUsing"];return type;}public void Insert(ScriptTypeM Type){SQLiteHelper.ExecuteNonQuery(@"insert into TB_ScriptType(ScriptTypeId,ScriptType,IsUsing)Values(@ScriptTypeId,@ScriptType,1)",new SQLiteParameter("ScriptTypeId", Type.ScriptTypeId),new SQLiteParameter("ScriptType", Type.ScriptType));}public void Update(ScriptTypeM Type){SQLiteHelper.ExecuteNonQuery(@"update TB_ScriptType set ScriptType=@ScriptType,IsUsing=@IsUsing where ScriptTypeId=@ScriptTypeId",new SQLiteParameter("ScriptType", Type.ScriptType),new SQLiteParameter("IsUsing", Type.IsUsing),new SQLiteParameter("ScriptTypeId", Type.ScriptTypeId));}public ScriptTypeM GetbyId(Guid id){DataTable table = SQLiteHelper.GetDataTable("select * from TB_ScriptType where ScriptTypeId=@id",new SQLiteParameter("id", id));if (table.Rows.Count <= 0){return null;}else if (table.Rows.Count > 1){throw new Exception("Id重復!");}else{return ToScriptType(table.Rows[0]);}}public ScriptTypeM GetbyName(string name){DataTable table = SQLiteHelper.GetDataTable("select * from TB_ScriptType where ScriptType=@name",new SQLiteParameter("name", name));if (table.Rows.Count <= 0){return null;}else if (table.Rows.Count > 1){throw new Exception("類型名稱重復!");}else{return ToScriptType(table.Rows[0]);}}public ScriptTypeM[] ListAll(){DataTable table = SQLiteHelper.GetDataTable("select * from TB_ScriptType where IsUsing=1");ScriptTypeM[] type = new ScriptTypeM[table.Rows.Count];for (int i = 0; i < table.Rows.Count; i++){type[i] = ToScriptType(table.Rows[i]);}return type;}public ScriptTypeM[] Search(string sql, List<SQLiteParameter> parameterList){DataTable table = SQLiteHelper.GetDataTable(sql, parameterList.ToArray());ScriptTypeM[] type = new ScriptTypeM[table.Rows.Count];for (int i = 0; i < table.Rows.Count; i++){type[i] = ToScriptType(table.Rows[i]);}return type;}}

SQLite數據庫小巧而且應用方便,在一些小型應用和嵌入式應用中很有優勢,當然如何應用的得心應手就看個人了。

歡迎關注:

總結

以上是生活随笔為你收集整理的PC软件开发技术之三:C#操作SQLite数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。