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帮助类的全部內容,希望文章能夠幫你解決所遇到的問題。