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

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

主站蜘蛛池模板: av一片 | 在线播放视频高清在线观看 | 欧美午夜精品久久久久久人妖 | 熟女丰满老熟女熟妇 | 亚洲一区二区三区欧美 | 欧美日本韩国一区二区 | 啦啦啦视频在线观看 | 老头吃奶性行交 | a色网站| 亚洲午夜激情视频 | 四虎久久久 | 在线v| 久久综合久久综合久久综合 | 中文字幕视频观看 | 久久成人免费电影 | 日本乱淫视频 | 成人欧美一区二区三区在线观看 | 午夜爱爱影院 | 怡红院精品视频 | 亚洲小说图片区 | 美女免费黄视频 | 爱情岛论坛永久入址测速 | 日韩精品人妻中文字幕 | 美女视频黄a视频全免费观看 | 色撸撸av | 男女爽爽视频 | 日日夜夜操av | 无码人妻精品一区二区三应用大全 | 中文字幕日韩一级 | 免费观看理伦片在线播放视频软件 | 美女扒开内看个够网站 | 91在线观看成人 | 区一区二在线观看 | 成人香蕉网 | 国产精品视频久久久久 | 8mav在线| 国产精品久久久久久久天堂 | 国产精品国色综合久久 | 91美女片黄在线观看 | 日韩乱码人妻无码系列中文字幕 | av动漫免费观看 | 91sex国产 | 中文字幕一区二区三区又粗 | 娇小的粉嫩xxx极品 国产精品人人爽人人爽 | 欧美在线视频一区 | 天天射综合 | 亚洲aaaaaaa | 夜夜爽夜夜叫夜夜高潮漏水 | 亚洲拍拍视频 | 干干日日| 日韩精品久久久久久久 | 久久久久亚洲国产 | 干b视频在线观看 | 日本大胆裸体做爰视频 | 国产在线播放91 | 国产乱国产 | 日本一区二区三区免费看 | 欧美日韩成人一区二区三区 | 国产资源免费 | 初尝人妻少妇中文字幕 | 五月天中文字幕av | 亚洲激情网址 | 国模私拍大尺度裸体av | 亚洲精品尤物 | 国产主播在线播放 | 超碰在线色 | 亚洲午夜无码久久久久 | 久久久一二三四 | 日韩精品人妻中文字幕 | 强行无套内谢大学生初次 | 精品伦精品一区二区三区视频 | 色多多视频在线 | 成人h动漫精品一区二区 | 成人性生交大片免费看 | 福利在线看 | 亚洲黄色免费 | 亚洲人在线播放 | 女人一区二区 | 人妻精品无码一区二区三区 | 激情啪啪网 | 亚洲欧美日韩国产精品 | 深夜福利网站在线观看 | 日日干夜夜爽 | 亲切的金子餐桌片段的金子 | 午夜久久久久久噜噜噜噜 | 国产伦精品一区二区三区妓女下载 | 秋霞午夜网 | 免费黄色av | 青青草国产在线视频 | 4hu最新网址 | 国产精品高清网站 | 加勒比一区二区三区 | 欧美日韩国产在线观看 | 金8天国av | 欧美日本精品 | 免费成人深夜小野草 | 老司机在线观看视频 | 狠狠爱亚洲 | 中文字幕人妻熟女在线 |