博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#封装SQLite数据库
阅读量:4306 次
发布时间:2019-06-06

本文共 9306 字,大约阅读时间需要 31 分钟。

网上有许多介绍关于SQLite数据库的,这里我就不多说了,这里主要介绍SQLite数据库在C#中的应用,它的应用主要依赖于System.Data.SQLite.dll文件,可以点击这里下载。下载完成后直接引用到自己的工程里即可。好了废话不多说,直接上代码。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SQLite;using System.IO;namespace InspectionQueue.CommonHelper{    public class SqliteHelper    {        private static SqliteHelper _instance;        public static SqliteHelper GetInstance()        {            if(null == _instance)            {                _instance = new SqliteHelper();            }            return _instance;        }        ///         /// 数据库连接定义        ///         private SQLiteConnection dbConnection;        ///         /// SQL命令定义        ///         private SQLiteCommand dbCommand;        ///         /// 数据读取定义        ///         private SQLiteDataReader dataReader;        private SqliteHelper() { }          public bool CreateDb(string dbPath)        {            try            {                if(!File.Exists(dbPath))                {                    //如果数据库文件不存在,则创建                    SQLiteConnection.CreateFile(dbPath);                }                          string conDbPath = "Data Source=" + dbPath;                dbConnection = new SQLiteConnection(conDbPath);                if(null == dbConnection)                {                    return false;                }                dbConnection.Open();            }            catch(Exception e)            {                throw e;            }            return true;        }        ///         /// 执行SQL命令        ///         ///         /// 
public SQLiteDataReader ExecuteQuery(string queryString) { try { dbCommand = dbConnection.CreateCommand(); dbCommand.CommandText = queryString; dataReader = dbCommand.ExecuteReader(); } catch(Exception e) { throw e; } return dataReader; } /// /// 关闭数据库连接 /// public void CloseConncetion() { //销毁Command if(dbCommand != null) { dbCommand.Cancel(); } dbCommand = null; //销毁Reader if(dataReader != null) { dataReader.Close(); } dataReader = null; //销毁Connection if(dbConnection != null) { dbConnection.Clone(); } dbConnection = null; } /// /// 读取整张数据表 /// /// ///
private SQLiteDataReader ReadFullTable(string tableName) { string queryString = "SELECT * FROM" + " " + tableName; return ExecuteQuery(queryString); } /// /// 像指定数据表中插入数据 /// /// /// ///
public SQLiteDataReader InsertValue(string tableName, string[] valus) { //获取数据表中字段数目 int fieldCount = ReadFullTable(tableName).FieldCount; //当插入的数据长度不等于字段数据时引异常 if(valus.Length != fieldCount) { throw new SQLiteException("values.Length != fileCount"); } string queryString = "INSERT INTO" + " " + tableName + " " + "VALUES(" + "'" + valus[0] + "'"; for (int i = 1; i < valus.Length; i++) { queryString += "," + "'" + valus[i] + "'"; } queryString += ")"; return ExecuteQuery(queryString); } /// /// 更新指定数据表内的数据 /// /// 数据表名称 /// 字段名 /// 字段名对应的数据 /// 关键字 /// 关键字对应的值 /// 运算符:=,<,> ///
public SQLiteDataReader UpdateValues(string tableName, string[] colNames, string[] colValues, string key, string value, string operation) { //operation="=";//默认 //当字段名称和字段数值不对应时引发异常 if(colNames.Length != colValues.Length) { throw new SQLiteException("colNames.Length != colValues.Length"); } string queryString = "UPDATE" + " "+ tableName + " " + "SET" + " "+ colNames[0] + "=" + "'" + colValues[0] + "'"; for (int i = 1; i < colValues.Length; i++) { queryString += "," + colNames[i] + "=" + "'" + colValues[i] + "'"; } queryString += "WHERE" + " " + key + operation + "'" + value + "'"; return ExecuteQuery(queryString); } /// /// 更新指定数据表中的数据 /// /// /// /// /// /// /// /// /// ///
public SQLiteDataReader UpdateValues(string tableName, string[] colNames, string[] colValues,string key1, string value1, string operation, string key2, string value2) { //operation="=";默认 //当字段名称和字段数值不对应时发生异常 if(colNames.Length != colValues.Length) { throw new SQLiteException("colNames.Length != colValues.Length"); } string queryString = "UPDATE" + " " + tableName + " " + "SET" + colNames[0] + "=" + "'" + colValues[0] + "'"; for(int i = 1; i < colValues.Length; i++) { queryString += "," + colNames[i] + "=" + "'" + colValues[i] + "'"; } //表中已经设置成int类型的不需要再次添加单引号,而字符串类型的数据需要进行添加单引号 queryString += "WHERE" + key1 + operation + "'" + value1 + "'" + "OR" + key2 + operation + "'" + value2 + "'"; return ExecuteQuery(queryString); } /// /// 删除指定数据表内的数据 /// /// 数据表名称 /// 字段名 /// 字段名对应的数据 /// ///
public SQLiteDataReader DeleteValues(string tableName, string[] colNames, string[] colValues, string[] operations) { //当字段名称和字段数值不对应时引发异常 if(colNames.Length != colValues.Length || operations.Length != colNames.Length || operations.Length != colValues.Length) { throw new SQLiteException("colNames.Length != colValues.Length || operations.Length != colNames.Length || operations.Length != colValues.Length"); } string queryString = "DELETE FROM" + " " + tableName + " " + "WHERE" + " " + colNames[0] + operations[0] + "'" + colValues[0] + "'"; for(int i = 1; i < colValues.Length; i++) { queryString += "AND" + " " + colNames[i] + operations[i] + "'" + colValues[i] + "'"; } return ExecuteQuery(queryString); } /// /// 创建数据表 /// /// /// /// ///
public SQLiteDataReader CreateTable(string tableName, string[] colNames, string[] colTypes) { string queryString = "CREATE TABLE IF NOT EXISTS" +" " + tableName + "(" + colNames[0] + " " + colTypes[0]; for(int i = 1; i < colNames.Length; i++) { queryString += "," + colNames[i] + " " + colTypes[i]; } queryString += ")"; return ExecuteQuery(queryString); } /// /// 根据查询条件查询 /// /// /// /// /// /// ///
public List
QueryTable(string tableName, string[] items, string[] colNames, string[] operations, string[] colValues) { string queryString = "SELECT" + " " + items[0] + " "; for(int i = 1; i < items.Length; i++) { queryString += "," + items[i]; } queryString += "FROM" + " " + tableName + " " + "WHERE" + " " + colNames[0] + operations[0] + "'" + colValues[0] + "'"; for(int i = 1; i < colNames.Length; i++) { queryString += "AND" + colNames[i] + " " + operations[i] + " " + "'" + colValues[i] + "'" + " "; } SQLiteDataReader dr = ExecuteQuery(queryString); List
QueryResult = new List
(); if(dr.HasRows) { while(dr.Read()) { string[] queryData = new string[dr.FieldCount]; for (int i = 0; i < dr.FieldCount; i++) { queryData[i] = dr.GetString(i); } QueryResult.Add(queryData); } } return QueryResult; } ///
/// 查询整张数据表 /// ///
///
public List
QueryTable(string tableName) { string queryString = "SELECT" + " "+ "*FROM" + " " + tableName; List
QueryResult = new List
(); if (null == QueryResult) { return null; } SQLiteDataReader dr = ExecuteQuery(queryString); if(dr.HasRows) { while(dr.Read()) { string[] queryData = new string[dr.FieldCount]; for (int i = 0; i < dr.FieldCount; i++) { queryData[i] = dr[i].ToString(); } QueryResult.Add(queryData); } } return QueryResult; } }}

 

转载于:https://www.cnblogs.com/QingYiShouJiuRen/p/10198744.html

你可能感兴趣的文章
git 常用命令 入门
查看>>
关闭selinx nginx无法使用代理
查看>>
shell 脚本部署项目
查看>>
spring cloud zuul网关上传大文件
查看>>
springboot+mybatis日志显示SQL
查看>>
工作流中文乱码问题解决
查看>>
maven打包本地依赖包
查看>>
spring boot jpa 实现拦截器
查看>>
jenkins + maven+ gitlab 自动化部署
查看>>
Pull Request流程
查看>>
Lambda 表达式
查看>>
函数式数据处理(一)--流
查看>>
java 流使用
查看>>
java 用流收集数据
查看>>
java并行流
查看>>
CompletableFuture 组合式异步编程
查看>>
mysql查询某一个字段是否包含中文字符
查看>>
Java中equals和==的区别
查看>>
JVM内存管理及GC机制
查看>>
Java:按值传递还是按引用传递详细解说
查看>>