From a98340ed2e63fd6de28e05d43f2f91f8178d2a01 Mon Sep 17 00:00:00 2001 From: 1764564459 <1764564459@qq.com> Date: Fri, 12 Apr 2019 15:28:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0SQLiteHelper=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.config | 11 +- EntityFrameWork.Server.csproj | 12 ++ Helper/SQLiteHelper.cs | 255 ++++++++++++++++++++++++++++++++++ packages.config | 4 + 4 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 Helper/SQLiteHelper.cs diff --git a/App.config b/App.config index 66eb476..141194e 100644 --- a/App.config +++ b/App.config @@ -1,9 +1,9 @@  -
- + + @@ -15,6 +15,7 @@ + @@ -34,4 +35,10 @@ + + + + + + \ No newline at end of file diff --git a/EntityFrameWork.Server.csproj b/EntityFrameWork.Server.csproj index 0eb9db8..b9eecd6 100644 --- a/EntityFrameWork.Server.csproj +++ b/EntityFrameWork.Server.csproj @@ -67,6 +67,15 @@ + + packages\System.Data.SQLite.Core.1.0.110.0\lib\net46\System.Data.SQLite.dll + + + packages\System.Data.SQLite.EF6.1.0.110.0\lib\net46\System.Data.SQLite.EF6.dll + + + packages\System.Data.SQLite.Linq.1.0.110.0\lib\net46\System.Data.SQLite.Linq.dll + packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll True @@ -90,6 +99,7 @@ + @@ -116,5 +126,7 @@ 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + \ No newline at end of file diff --git a/Helper/SQLiteHelper.cs b/Helper/SQLiteHelper.cs new file mode 100644 index 0000000..f26f55f --- /dev/null +++ b/Helper/SQLiteHelper.cs @@ -0,0 +1,255 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SQLite; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFrameWork.Server.Helper +{ + //需要NutGet System.Data.SQLite + public class SQLiteHelper : IDisposable + { + + private static SQLiteConnection _SQLiteConnect { get; set; } + //库文件夹地址 + private static string SQLite_Path = $"{Directory.GetCurrentDirectory()}//SQLite_Data"; + //库名字 + static string SQLite_Name = $"Vehicle_History.sqlite"; + //库的准确地址 + static string SQLite_File = $"{SQLite_Path}//{SQLite_Name}"; + //连接字符串 + private static readonly string str = $"Data Source ={SQLite_File}; Version=3"; //ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; + /// + /// 建库 + /// + public SQLiteHelper() + { + _SQLiteConnect = new SQLiteConnection(str); + //文件夹是否存在 + if (!Directory.Exists(SQLite_Path)) + Directory.CreateDirectory(SQLite_Path); + //判断数据库是否存在 + if (!File.Exists(SQLite_File)) + SQLiteConnection.CreateFile(SQLite_File); + + createTable(); + } + + /// + /// 打开SQLite链接 + /// + public static void Open() + { + if (_SQLiteConnect.State == ConnectionState.Closed) + _SQLiteConnect.Open(); + } + + //在指定数据库中创建一个table + void createTable() + { + Open(); + + //检查表是否存在 + string sql = "select name from sqlite_master where name='GPS_History'"; + DataTable data = ExecuteTable(sql); + if (data.Rows.Count > 0) + return; + + sql = "create table GPS_History (Id GUID, Last_Time DATETIME,Data NVARCHAR,Is_Update BOOL)"; + + SQLiteCommand command = new SQLiteCommand(sql, _SQLiteConnect); + + command.ExecuteNonQuery(); + } + /// + /// 增删改 + /// 20180723 + /// + /// sql语句 + /// sql参数 + /// 受影响的行数 + public int ExecuteNonQuery(string sql, params SQLiteParameter[] param) + { + Open(); + using (SQLiteCommand cmd = new SQLiteCommand(sql, _SQLiteConnect)) + { + if (param != null) + { + cmd.Parameters.AddRange(param); + } + + string sql2 = cmd.CommandText; + //con.Close(); + return cmd.ExecuteNonQuery(); + } + + } + + /// + /// 查询 + /// 20180723 + /// + /// sql语句 + /// sql参数 + /// 首行首列 + public object ExecuteScalar(string sql, params SQLiteParameter[] param) + { + Open(); + using (SQLiteCommand cmd = new SQLiteCommand(sql, _SQLiteConnect)) + { + if (param != null) + { + cmd.Parameters.AddRange(param); + } + + return cmd.ExecuteScalar(); + } + } + + /// + /// 多行查询 + /// 20180723 + /// + /// sql语句 + /// sql参数 + /// SQLiteDateReader + public SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] param) + { + Open(); + using (SQLiteCommand cmd = new SQLiteCommand(sql, _SQLiteConnect)) + { + if (param != null) + { + cmd.Parameters.AddRange(param); + } + try + { + return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); + } + catch (Exception ex) + { + _SQLiteConnect.Close(); + _SQLiteConnect.Dispose(); + throw ex; + } + } + + } + + /// + /// 查询多行数据 + /// 20180723 + /// + /// sql语句 + /// sql参数 + /// 一个表 + public DataTable ExecuteTable(string sql, params SQLiteParameter[] param) + { + DataTable dt = new DataTable(); + using (SQLiteDataAdapter sda = new SQLiteDataAdapter(sql, str)) + { + if (param != null) + { + sda.SelectCommand.Parameters.AddRange(param); + } + sda.Fill(dt); + } + return dt; + } + + /// + /// 查询封装 + /// 20180725 + /// + /// 表名 + /// 查询需要的字段名:"id, name, age" + /// 查询条件:"id = 1" + /// 排序:"id desc" + /// 分页:"0,10" + /// sql参数 + /// 受影响行数 + public DataTable QueryTable(string tbName, string fields = "*", string where = "1", string orderBy = "", string limit = "", params SQLiteParameter[] param) + { + //排序 + if (orderBy != "") + { + orderBy = "ORDER BY " + orderBy;//Deom: ORDER BY id desc + } + + //分页 + if (limit != "") + { + limit = "LIMIT " + limit;//Deom: LIMIT 0,10 + } + + string sql = string.Format("SELECT {0} FROM `{1}` WHERE {2} {3} {4}", fields, tbName, where, orderBy, limit); + + //return sql; + return ExecuteTable(sql, param); + + } + + /// + /// 数据插入 + /// 20180725 + /// + /// 表名 + /// 需要插入的数据字典 + /// 受影响行数 + public int ExecuteInsert(string tbName, Dictionary insertData) + { + string point = "";//分隔符号(,) + string keyStr = "";//字段名拼接字符串 + string valueStr = "";//值的拼接字符串 + + List param = new List(); + foreach (string key in insertData.Keys) + { + keyStr += string.Format("{0} `{1}`", point, key); + valueStr += string.Format("{0} @{1}", point, key); + param.Add(new SQLiteParameter("@" + key, insertData[key])); + point = ","; + } + string sql = string.Format("INSERT INTO `{0}`({1}) VALUES({2})", tbName, keyStr, valueStr); + + //return sql; + return ExecuteNonQuery(sql, param.ToArray()); + + } + + /// + /// 执行Update语句 + /// 20180725 + /// + /// 表名 + /// 更新条件:id=1 + /// 需要更新的数据 + /// 受影响行数 + public int ExecuteUpdate(string tbName, string where, Dictionary insertData) + { + string point = "";//分隔符号(,) + string kvStr = "";//键值对拼接字符串(Id=@Id) + + List param = new List(); + foreach (string key in insertData.Keys) + { + kvStr += string.Format("{0} {1}=@{2}", point, key, key); + param.Add(new SQLiteParameter("@" + key, insertData[key])); + point = ","; + } + string sql = string.Format("UPDATE `{0}` SET {1} WHERE {2}", tbName, kvStr, where); + + return ExecuteNonQuery(sql, param.ToArray()); + + } + + public void Dispose() + { + if (_SQLiteConnect != null) + _SQLiteConnect.Dispose(); + } + } +} diff --git a/packages.config b/packages.config index 51fb55e..d8ce223 100644 --- a/packages.config +++ b/packages.config @@ -9,5 +9,9 @@ + + + + \ No newline at end of file