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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQLiteHelper帮助类

發布時間:2023/12/18 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQLiteHelper帮助类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、作用

實現SQLite小型數據的操作,包含:創建、讀取、修改、寫入。如果你想傳入臨時表(DataTable)、臨時數數據集(DataSet)、范式類型(IList)達到更新目的,需要添加 DataOperation類支持,個人也已經封裝。

2、引入組件

System.Data.SQLite.dll

SQLite.Interop.dll(該組件如果沒有注冊可能引入不進去,可以直接放在bin目錄即可)

3、幫助類,可以直接創建類SQLiteHelper.cs,復制以下代碼:

using AutoPlayer.Common;
using System;
using System.Data;
using System.Data.SQLite;

namespace AutoPlayer.DAL
{
? ? public class SQLiteHelper
? ? {
? ? ? ? //從配置文本中讀取連接字符串
? ? ? ? private static string connectionString = XmlHelper.GetAppConfig("dbcon");

? ? ? ? /// <summary>
? ? ? ? /// 創建一個數據庫文件。如果存在同名數據庫文件,則會覆蓋。
? ? ? ? /// </summary>
? ? ? ? /// <param name="dbName"></param>
? ? ? ? public static void CreateDB(string dbName)
? ? ? ? {
? ? ? ? ? ? string l_strdbName = dbName;
? ? ? ? ? ? if (!dbName.Contains("."))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? l_strdbName = dbName + ".sqlite3";
? ? ? ? ? ? }

? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? SQLiteConnection.CreateFile(l_strdbName);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? throw;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 創建連接到指定數據庫
? ? ? ? /// </summary>
? ? ? ? /// <param name="datasource"></param>
? ? ? ? /// <param name="password"></param>
? ? ? ? /// <param name="version"></param>
? ? ? ? public static void SetConnectionString(string datasource, string password = "", int version = 3)
? ? ? ? {
? ? ? ? ? ? connectionString = string.Format("Data Source={0};password={1},Version={2};",
? ? ? ? ? ? ? ? datasource, password, version);
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 執行命令的方法:insert,update,delete
? ? ? ? /// </summary>
? ? ? ? /// <param name="sql"></param>
? ? ? ? /// <param name="parameters">可變參數,目的是省略了手動構造數組的過程,直接指定對象,編譯器會幫助我們構造數組,并將對象加入數組中,傳遞過來</param>
? ? ? ? /// <returns></returns>
? ? ? ? public static int ExecuteNonQuery(string sql, params SQLiteParameter[] parameters)
? ? ? ? {
? ? ? ? ? ? using (SQLiteConnection connection = new SQLiteConnection(connectionString))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? using (SQLiteCommand command = new SQLiteCommand(connection))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? connection.Open();
? ? ? ? ? ? ? ? ? ? ? ? command.CommandText = sql;
? ? ? ? ? ? ? ? ? ? ? ? if (parameters.Length > 0)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? command.Parameters.AddRange(parameters);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? return command.ExecuteNonQuery();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? catch (Exception) { throw; }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 執行查詢語句,并返回第一個結果。
? ? ? ? /// </summary>
? ? ? ? /// <param name="sql"></param>
? ? ? ? /// <param name="parameters"></param>
? ? ? ? /// <returns></returns>
? ? ? ? public static object ExecuteScalar(string sql, params SQLiteParameter[] parameters)
? ? ? ? {
? ? ? ? ? ? using (SQLiteConnection conn = new SQLiteConnection(connectionString))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? using (SQLiteCommand cmd = new SQLiteCommand(conn))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? conn.Open();
? ? ? ? ? ? ? ? ? ? ? ? cmd.CommandText = sql;
? ? ? ? ? ? ? ? ? ? ? ? if (parameters.Length != 0)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? cmd.Parameters.AddRange(parameters);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? return cmd.ExecuteScalar();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? catch (Exception) { throw; }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? /// <summary>
? ? ? ? /// 執行一個查詢語句,返回一個包含查詢結果的DataTable。?
? ? ? ? /// </summary>
? ? ? ? /// <param name="sql"></param>
? ? ? ? /// <param name="parameters"></param>
? ? ? ? /// <returns></returns>
? ? ? ? public static DataTable ExecuteQuery(string sql, params SQLiteParameter[] parameters)
? ? ? ? {
? ? ? ? ? ? using (SQLiteConnection connection = new SQLiteConnection(connectionString))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? using (SQLiteCommand command = new SQLiteCommand(sql, connection))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (parameters.Length != 0)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? command.Parameters.AddRange(parameters);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
? ? ? ? ? ? ? ? ? ? DataTable data = new DataTable();
? ? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? adapter.Fill(data);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? catch (Exception)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? throw;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return data;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? /// <summary>
? ? ? ? /// 執行一個查詢語句,返回一個關聯的SQLiteDataReader實例。?
? ? ? ? /// </summary>
? ? ? ? /// <param name="sql"></param>
? ? ? ? /// <param name="parameters"></param>
? ? ? ? /// <returns></returns>
? ? ? ? public static SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] parameters)
? ? ? ? {
? ? ? ? ? ? SQLiteConnection connection = new SQLiteConnection(connectionString);
? ? ? ? ? ? SQLiteCommand command = new SQLiteCommand(sql, connection);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (parameters.Length != 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? command.Parameters.AddRange(parameters);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? connection.Open();
? ? ? ? ? ? ? ? return command.ExecuteReader(CommandBehavior.CloseConnection);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception) { throw; }
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 查詢表字段類型
? ? ? ? /// </summary>
? ? ? ? /// <returns></returns>
? ? ? ? public static DataTable GetSchema()
? ? ? ? {
? ? ? ? ? ? using (SQLiteConnection connection = new SQLiteConnection(connectionString))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? connection.Open();
? ? ? ? ? ? ? ? ? ? return connection.GetSchema("TABLES");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (Exception) { throw; }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
?

總結

以上是生活随笔為你收集整理的SQLiteHelper帮助类的全部內容,希望文章能夠幫你解決所遇到的問題。

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