Commit 8d83f1b8 authored by 潘栩锋's avatar 潘栩锋 🚴

改SQLiteHelper

parent 60903c07
......@@ -68,22 +68,51 @@ namespace Misc
return null;
}
/// <summary>
/// 把 src内的全部属性,复制到 dest
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="src"></param>
/// <param name="dest"></param>
public static void CopyTo<T>(T src, T dest)
public static void CopyTo(object src, object dest)
{
Type t = typeof(T);
Type t = src.GetType();
if (src.GetType() != dest.GetType())
{
throw new Exception("CopyTo src.GetType() != dest.GetType()");
}
foreach (PropertyInfo propertyInfo in t.GetProperties())
{
if (propertyInfo.CanWrite)
{
propertyInfo.SetValue(dest, propertyInfo.GetValue(src, null), null);
}
else
{
//ICopiable
if (propertyInfo.PropertyType.IsSubclassOf(typeof(ICopiable)))
{
ICopiable copiable = propertyInfo.GetValue(dest, null) as ICopiable;
copiable.Copy(propertyInfo.GetValue(src, null));
}
}
}
}
}
/// <summary>
/// 一般情况 用 Misc.PropertiesManager.CopyTo 就能解决问题
/// 但 只有get的属性,是没法复制的
/// 只能通过 继承 ICopiable, 解决 只能get 的属性 复制问题
/// </summary>
public interface ICopiable
{
/// <summary>
/// 从 src 复制全部数据过来!!!
///
/// </summary>
/// <param name="src"></param>
void Copy(object src);
}
}
......@@ -12,5 +12,7 @@ namespace FLY.OBJComponents.Common.SQLite
void Create();
void Init(string connnectionString);
}
}

using System;
namespace FLY.OBJComponents.Common
{
public class IgnoreAttribute : Attribute
{
public IgnoreAttribute()
{
}
}
}
\ No newline at end of file
......@@ -145,6 +145,10 @@ namespace FLY.OBJComponents.Common.SQLite
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
//忽略
if (propertyInfo.GetCustomAttributes(typeof(IgnoreAttribute), false).Count() > 0)
continue;
if (fieldtext != "")
{
fieldtext += ",";
......@@ -200,6 +204,10 @@ namespace FLY.OBJComponents.Common.SQLite
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
//忽略
if (propertyInfo.GetCustomAttributes(typeof(IgnoreAttribute), false).Count() > 0)
continue;
if (fieldtext != "")
{
fieldtext += ",";
......@@ -233,6 +241,13 @@ namespace FLY.OBJComponents.Common.SQLite
string commandText = string.Format("INSERT INTO {0} VALUES({1})", tablename, fieldtext);
return commandText;
}
/// <summary>
/// condition 为 "WHERE ......"
/// </summary>
/// <param name="cell"></param>
/// <param name="condition"></param>
/// <returns></returns>
public static string GetUpdateCommandText(object cell, string condition)
{
//UPDATE table_name
......@@ -246,6 +261,10 @@ namespace FLY.OBJComponents.Common.SQLite
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
//忽略
if (propertyInfo.GetCustomAttributes(typeof(IgnoreAttribute), false).Count() > 0)
continue;
if (fieldtext != "")
{
fieldtext += ",";
......@@ -340,18 +359,18 @@ namespace FLY.OBJComponents.Common.SQLite
public DataTable ExecuteReader(string sql)
{
DataTable data;
using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
{
conn.Open();
SQLiteCommand command = conn.CreateCommand();
command.CommandText = sql;
// 开始读取
SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
data = new DataTable();
adapter.Fill(data);
conn.Close();
using (SQLiteCommand command = new SQLiteCommand(connection))
{
connection.Open();
command.CommandText = sql;
// 开始读取
SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
data = new DataTable();
adapter.Fill(data);
connection.Close();
}
}
return data;
}
......@@ -371,10 +390,32 @@ namespace FLY.OBJComponents.Common.SQLite
connection.Open();
command.CommandText = sql;
command.ExecuteNonQuery();
connection.Close();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sql"></param>
/// <returns>The first column of the first row of the first resultset from the query.</returns>
public object ExecuteScalar(string sql)
{
object obj = null;
using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
{
using (SQLiteCommand command = new SQLiteCommand(connection))
{
connection.Open();
command.CommandText = sql;
obj = command.ExecuteScalar();
connection.Close();
}
}
return obj;
}
/// <summary>
/// 多行执行
/// </summary>
......@@ -382,20 +423,20 @@ namespace FLY.OBJComponents.Common.SQLite
/// <returns></returns>
public bool QueryTran(IEnumerable<string> queryList)
{
using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(conn))
using (SQLiteCommand command = new SQLiteCommand(connection))
{
conn.Open();
connection.Open();
SQLiteTransaction tran = conn.BeginTransaction();
SQLiteTransaction tran = connection.BeginTransaction();
bool check = false;
try
{
foreach (string item in queryList)
{
cmd.CommandText = item;
cmd.ExecuteNonQuery();
command.CommandText = item;
command.ExecuteNonQuery();
}
tran.Commit();
check = true;
......@@ -408,7 +449,7 @@ namespace FLY.OBJComponents.Common.SQLite
}
finally
{
conn.Close();
connection.Close();
}
return check;
}
......@@ -416,5 +457,44 @@ namespace FLY.OBJComponents.Common.SQLite
}
/// <summary>
/// 输入DDLs 判断这些table都是否合法
/// </summary>
/// <param name="DDLs">key=tablename, value=DDL</param>
/// <returns></returns>
public bool IsTableValid(Dictionary<string,string> DDLs)
{
//检测 table 是否合法
DataTable data = ExecuteReader("SELECT name,sql FROM sqlite_master WHERE type = 'table'");
//任意一个表不对,或者不存在,都必须重建
bool isVaild = true;
foreach (var kv in DDLs)
{
string tablename = kv.Key;
string createtable_sql = kv.Value;
var sqls = from r in data.AsEnumerable() where (string)r["name"] == tablename select r["sql"];
if (sqls.Count() == 0)
{
//不存在该表,创建
isVaild = false;
break;
}
else
{
string sql = (string)sqls.First();
if (sql != createtable_sql)
{
isVaild = false;
break;
}
}
}
return isVaild;
}
}
}
......@@ -79,6 +79,7 @@
<Compile Include="Common\PropertiesManager.cs" />
<Compile Include="Common\SQLite\DBTable.cs" />
<Compile Include="Common\SQLite\IDBTable.cs" />
<Compile Include="Common\SQLite\IgnoreAttribute.cs" />
<Compile Include="Common\SQLite\SQLiteHelper.cs" />
<Compile Include="IService\IBuffer.cs" />
<Compile Include="IService\IPLCProxySystemService.cs" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment