5 Star 19 Fork 0

littleMIS/开源物料清单软件OpenBOMS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
DalSQLite.cs 20.50 KB
一键复制 编辑 原始数据 按行查看 历史
dinid 提交于 4年前 . v1.0.0发布
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows;
namespace OpenBOMS
{
/// <summary>
/// SQLite数据库操作类
/// </summary>
public class DalSQLite
{
/// <summary>
/// 软件启动数据检测
/// </summary>
/// <returns></returns>
public static bool CheckDb()
{
bool result = false;
if (File.Exists(BaseDbSQLiteClass.DbFilePath) == true)//数据文件存在
{
if (GetSoftName() == DalDataConfig.SoftName)
{
result = true;
}
else
{
RepairDB();
}
}
else//数据文件不存在
{
if ((System.Windows.MessageBox.Show("新的软件安装、是否创建新的数据文件?", "数据检测", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes))//
{
result = BaseDbSQLiteClass.CreateDB("my.sql");
}
}
return result;
}
private static void RepairDB()
{
try
{
if (MessageBox.Show("删除现有数据库并新建数据结构吗?", "数据库异常提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
if (File.Exists(BaseDbSQLiteClass.DbFilePath) == true)
{
File.Delete(BaseDbSQLiteClass.DbFilePath);
}
BaseDbSQLiteClass.CreateDB("my.sql");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 返回软件名
/// </summary>
/// <param name="connString">连接字符串</param>
/// <returns>版本号</returns>
public static string GetSoftName()
{
object obj = BaseDbSQLiteClass.GetSingle("SELECT SoftName FROM sys_config");
if (obj != null)
return obj.ToString();
else
return string.Empty;
}
/// <summary>
/// 返回应用程序对应数据库的数据结构版本号
/// </summary>
/// <param name="connString">连接字符串</param>
/// <returns>版本号</returns>
public static bool GetVersion(ref string version)
{
bool result = false;
double ver = 0.0;
try
{
object obj = BaseDbSQLiteClass.GetSingle("SELECT DbVersion FROM sys_config");
if (obj != null)
{
if (double.TryParse(obj.ToString(), out ver) == true)
{
if (ver > 0)
{
if(ver==DalDataConfig.DbVerion)
{
version = obj.ToString();
result = true;
}
else if(ver > DalDataConfig.DbVerion)
{
version = obj.ToString();
result = true;
MessageBox.Show("当前数据库版本高于软件要求版本,请检查软件、数据正确性!");
}
else
{
if (MessageBox.Show("当前数据库版本低于软件要求版本,是否升级数据库结构?", "数据库异常", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
if(BaseDbSQLiteClass.UpdateDB(obj.ToString()) ==true)
{
System.Environment.Exit(0);
}
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return result;
}
/// <summary>
/// 返回数据集合
/// </summary>
/// <returns>IList</returns>
public static IList<T> GetIList<T>(string sqlString)
{
return BaseDataTable.DataTableToIList<T>(BaseDbSQLiteClass.GetDataTable(sqlString));
}
/// <summary>
/// 获取某个表查询结果的数量
/// </summary>
/// <param name="field">主键</param>
/// <param name="tableName">表名</param>
/// <param name="where">条件字符串</param>
/// <returns></returns>
public static int GetDataRecordCount(string sqlString)
{
object obj = BaseDbSQLiteClass.GetSingle(sqlString);
if (obj == null)
{
return 0;
}
else
{
return int.Parse(obj.ToString());
}
}
/// <summary>
/// 插入数据
/// </summary>
/// <param name="sqlString">sql语句</param>
/// <returns>bool</returns>
public static bool Insert(string sqlString)
{
bool result = false;
try
{
if (BaseDbSQLiteClass.ExecuteSql(sqlString) > 0)
{
MessageBox.Show(DalPrompt.SaveOK);
return true;
}
else
{
MessageBox.Show(DalPrompt.SaveFail);
result = false;
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
return result;
}
/// <summary>
/// 批量插入数据
/// </summary>
/// <param name="l">sql语句集合</param>
/// <returns>bool</returns>
public static bool Insert(List<string> l)
{
bool result = false;
try
{
if (BaseDbSQLiteClass.ExecuteSqlTran(l) > 0)
{
MessageBox.Show(DalPrompt.SaveOK);
result = true;
}
else
{
MessageBox.Show(DalPrompt.SaveFail);
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
return result;
}
/// <summary>
/// 批量完成数据
/// </summary>
/// <param name="l">sql语句集合</param>
/// <returns>bool</returns>
public static bool Complete(List<string> l)
{
bool result = false;
try
{
if (BaseDbSQLiteClass.ExecuteSqlTran(l) > 0)
{
MessageBox.Show(DalPrompt.CompleteOK);
result = true;
}
else
{
MessageBox.Show(DalPrompt.CompleteFail);
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
return result;
}
/// <summary>
/// 批量插入数据
/// </summary>
/// <param name="l">sql语句集合</param>
/// <returns>bool</returns>
public static bool Import(List<string> l)
{
bool result = false;
try
{
if (BaseDbSQLiteClass.ExecuteSqlTran(l) > 0)
{
MessageBox.Show(DalPrompt.ImportOK);
result = true;
}
else
{
MessageBox.Show(DalPrompt.ImportFail);
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
return result;
}
/// <summary>
/// 保存数据
/// </summary>
/// <param name="sqlString">sql语句</param>
/// <returns>bool</returns>
public static bool Save(string sqlString)
{
bool result = false;
try
{
if (BaseDbSQLiteClass.ExecuteSql(sqlString) == 1)
{
MessageBox.Show(DalPrompt.SaveOK);
result = true;
}
else
{
MessageBox.Show(DalPrompt.SaveFail);
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
return result;
}
/// <summary>
/// 批量插入数据
/// </summary>
/// <param name="l">sql语句集合</param>
/// <returns>bool</returns>
public static bool Save(List<string> l)
{
bool result = false;
try
{
if (BaseDbSQLiteClass.ExecuteSqlTran(l) > 0)
{
MessageBox.Show(DalPrompt.SaveOK);
result = true;
}
else
{
MessageBox.Show(DalPrompt.SaveFail);
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
return result;
}
/// <summary>
/// 更新数据
/// </summary>
/// <param name="sqlString">sql语句</param>
/// <returns>bool</returns>
public static bool Update(string sqlString)
{
bool result = false;
try
{
if (BaseDbSQLiteClass.ExecuteSql(sqlString) == 1)
{
MessageBox.Show(DalPrompt.UpdateOK);
result = true;
}
else
{
MessageBox.Show(DalPrompt.UpdateFail);
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
return result;
}
/// <summary>
/// 批量更新数据
/// </summary>
/// <param name="l">sql语句集合</param>
/// <returns>bool</returns>
public static bool Update(List<string> l)
{
bool result = false;
if (l != null)
{
try
{
if (BaseDbSQLiteClass.ExecuteSqlTran(l) > 0)
{
MessageBox.Show(DalPrompt.UpdateOK);
result = true;
}
else
{
MessageBox.Show("更新失败!");
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
}
return result;
}
/// <summary>
/// 删除标记
/// </summary>
/// <param name="sqlStr">sql语句</param>
/// <returns>bool</returns>
public static bool DeleteMark(string sqlStr)
{
bool result = false;
try
{
if (BaseDbSQLiteClass.ExecuteSql(sqlStr) > 0)
{
MessageBox.Show("作废成功!");
result = true;
}
else
{
MessageBox.Show("作废失败!");
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
return result;
}
/// <summary>
/// 还原标记
/// </summary>
/// <param name="sqlStr">sql语句</param>
/// <returns>bool</returns>
public static bool ReturnMark(string sqlStr)
{
bool result = false;
try
{
if (BaseDbSQLiteClass.ExecuteSql(sqlStr) > 0)
{
MessageBox.Show("还原成功!");
result = true;
}
else
{
MessageBox.Show("还原失败!");
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
return result;
}
/// <summary>
/// /// <summary>
/// 追加查询条件拼接SQL语句
/// </summary>
/// <typeparam name="T">数据对象</typeparam>
/// <param name="s">被拼接的数据对象实例</param>
/// <param name="sqlString">被追加的SQL字符串</param>
public static void AppendConditionString<T>(T s, StringBuilder sqlString)
{
Type t = s.GetType();//获得该类的Type
//再用Type.GetProperties获得PropertyInfo[],然后就可以用foreach 遍历了
foreach (PropertyInfo pi in t.GetProperties())
{
string name = pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
if (pi.PropertyType == typeof(int))
{
int num = Convert.ToInt32(pi.GetValue(s, null));
if (num != 0)
{
if (num > 0)
{
sqlString.Append(" AND " + name + " = " + num + " ");
}
}
}
else if (pi.PropertyType == typeof(string))
{
string str = Convert.ToString(pi.GetValue(s, null));
if (str != null)
{
if (str != "")
{
string n = pi.Name.ToString();
sqlString.Append(" AND " + n + " LIKE '%" + str + "%' ");
}
}
}
else if (pi.PropertyType == typeof(Guid))//属性类型GUID
{
Guid g = (Guid)(pi.GetValue(s, null));//获取条件值
if (g != (new Guid()))
{
sqlString.Append(" AND " + name + " = '" + g + "' ");//增加SQL条件语句:与条件值相等
}
}
}
}
/// <summary>
/// 追加查询条件拼接SQL语句(FROM 表名——别名=a)
/// </summary>
/// <typeparam name="T">数据对象</typeparam>
/// <param name="s">被拼接的数据对象实例</param>
/// <param name="sqlString">被追加的SQL语句</param>
public static void AppendAliasString<T>(T s, StringBuilder sqlString, string alias)
{
Type t = s.GetType();//获得该类的Type
foreach (PropertyInfo pi in t.GetProperties()) //遍历Type.GetProperties获得的PropertyInfo[]
{
string name = pi.Name;//获取属性名
//根据属性类型条件判断是否拼接SQL语句
if (pi.PropertyType == typeof(int))
{
int num = Convert.ToInt32(pi.GetValue(s, null));
if (num != 0)
{
if (num > 0)
{
sqlString.Append(" AND " + alias + "." + name + " = " + num + " ");
}
}
}
else if (pi.PropertyType == typeof(string))//属性类型字符串
{
string str = Convert.ToString(pi.GetValue(s, null));//获取条件值
if (str != null)
{
if (str != "")
{
sqlString.Append(" AND " + alias + "." + name + " LIKE '%" + str + "%' ");//增加SQL条件语句:包含条件字符
}
}
}
else if (pi.PropertyType == typeof(Guid))//属性类型GUID
{
Guid g = (Guid)(pi.GetValue(s, null));//获取条件值
if (g != (new Guid()))
{
sqlString.Append(" AND " + alias + "." + name + " = '" + g + "' ");//增加SQL条件语句:与条件值相等
}
}
}
}
/// <summary>
/// 备份当前数据库文件到所选路径(命名为年月日时分秒.bak)
/// </summary>
/// <param name="filePath">备份文件绝对路径</param>
/// <returns></returns>
public static bool Backup(string filePath)
{
bool result = false;
string dbPath = BaseDbSQLiteClass.DbFilePath;//数据文件绝对路径
try
{
if (File.Exists(dbPath) == true)//有数据文件
{
if (File.Exists(filePath) == false)//绝对路径不存在备份文件,可以直接备份
{
File.Copy(dbPath, filePath);
System.Windows.MessageBox.Show(DalPrompt.DbBackupOK + @filePath);
result = true;
}
else//已有同名备份文件存在
{
if ((System.Windows.MessageBox.Show("备份文件已存在,是否覆盖?", "数据备份", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes))//
{
File.Delete(filePath);//删除已存在的备份文件
File.Copy(dbPath, filePath);
System.Windows.MessageBox.Show(DalPrompt.DbBackupOK + @filePath);
result = true;
}
}
}
else//无数据文件
{
MessageBox.Show("不存在需要备份的数据文件!");
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
return result;
}
/// <summary>
/// 还原所选择的备份文件到当前数据库
/// </summary>
/// <param name="dbPath"></param>
/// <returns></returns>
public static bool Restore(string filePath)
{
bool result = false;
string dbPath = BaseDbSQLiteClass.DbFilePath;//数据文件绝对路径
try
{
if (File.Exists(filePath) == true)//有备份文件
{
if (File.Exists(dbPath))//有现有数据文件存在
{
if ((System.Windows.MessageBox.Show("备份数据将会覆盖现有数据,是否继续?", "数据恢复", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes))//
{
File.Delete(dbPath);//删除已存在的备份文件
File.Copy(filePath, dbPath);
System.Windows.MessageBox.Show(DalPrompt.DbRestoreOK);
result = true;
}
}
else//无现有数据文件
{
File.Copy(filePath, dbPath);
System.Windows.MessageBox.Show(DalPrompt.DbRestoreOK);
result = true;
}
}
else//无数据文件
{
MessageBox.Show("请选择一个需要恢复的数据备份文件!");
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
return result;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/little-mis/open-boms.git
git@gitee.com:little-mis/open-boms.git
little-mis
open-boms
开源物料清单软件OpenBOMS
master

搜索帮助