添加数据库
This commit is contained in:
1 parent
14beadc848
commit
ad39eae8ad
17 files changed
+1574
No files matched your search
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.14.36202.13 d17.14
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProcessManageUI", "ProcessManageUI\ProcessManageUI.csproj", "{6DA7C6F9-C096-481A-B9E1-C19E72C35E58}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{6DA7C6F9-C096-481A-B9E1-C19E72C35E58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6DA7C6F9-C096-481A-B9E1-C19E72C35E58}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6DA7C6F9-C096-481A-B9E1-C19E72C35E58}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6DA7C6F9-C096-481A-B9E1-C19E72C35E58}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {DFE05529-79C8-4D27-B69B-CEFF2B5C8F5C}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
|
||||
</startup>
|
||||
</configuration>
|
||||
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="ProcessManageUI.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:ProcessManageUI"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace ProcessManageUI
|
||||
{
|
||||
/// <summary>
|
||||
/// App.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProcessManageUI
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用using代替lock操作的对象,可指定写入和读取锁定模式
|
||||
/// </summary>
|
||||
public sealed class ClsLock
|
||||
{
|
||||
#region 内部类
|
||||
|
||||
/// <summary>
|
||||
/// 利用IDisposable的using语法糖方便的释放锁定操作内部类
|
||||
/// </summary>
|
||||
private struct Lock : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// 读写锁对象
|
||||
/// </summary>
|
||||
private readonly ReaderWriterLockSlim _Lock;
|
||||
/// <summary>
|
||||
/// 是否为写入模式
|
||||
/// </summary>
|
||||
private bool _IsWrite;
|
||||
/// <summary>
|
||||
/// 利用IDisposable的using语法糖方便的释放锁定操作构造函数
|
||||
/// </summary>
|
||||
/// <param name="rwl">读写锁</param>
|
||||
/// <param name="isWrite">写入模式为true,读取模式为false</param>
|
||||
public Lock(ReaderWriterLockSlim rwl, bool isWrite)
|
||||
{
|
||||
_Lock = rwl;
|
||||
_IsWrite = isWrite;
|
||||
}
|
||||
/// <summary>
|
||||
/// 释放对象时退出指定锁定模式
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_IsWrite)
|
||||
{
|
||||
if (_Lock.IsWriteLockHeld)
|
||||
{
|
||||
_Lock.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_Lock.IsReadLockHeld)
|
||||
{
|
||||
_Lock.ExitReadLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 空的可释放对象,免去了调用时需要判断是否为null的问题内部类
|
||||
/// </summary>
|
||||
private class Disposable : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// 空的可释放对象
|
||||
/// </summary>
|
||||
public static readonly Disposable Empty = new Disposable();
|
||||
/// <summary>
|
||||
/// 空的释放方法
|
||||
/// </summary>
|
||||
public void Dispose() { }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 读写锁
|
||||
/// </summary>
|
||||
private readonly ReaderWriterLockSlim _LockSlim = new ReaderWriterLockSlim();
|
||||
/// <summary>
|
||||
/// 使用using代替lock操作的对象,可指定写入和读取锁定模式构造函数
|
||||
/// </summary>
|
||||
public ClsLock()
|
||||
{
|
||||
Enabled = true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否启用,当该值为false时,Read()和Write()方法将返回 Disposable.Empty
|
||||
/// </summary>
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 进入读取锁定模式,该模式下允许多个读操作同时进行,
|
||||
/// 退出读锁请将返回对象释放,建议使用using语块,
|
||||
/// Enabled为false时,返回Disposable.Empty,
|
||||
/// 在读取或写入锁定模式下重复执行,返回Disposable.Empty;
|
||||
/// </summary>
|
||||
public IDisposable Read()
|
||||
{
|
||||
if (Enabled == false || _LockSlim.IsReadLockHeld || _LockSlim.IsWriteLockHeld)
|
||||
{
|
||||
return Disposable.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
_LockSlim.EnterReadLock();
|
||||
return new Lock(_LockSlim, false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入写入锁定模式,该模式下只允许同时执行一个读操作,
|
||||
/// 退出读锁请将返回对象释放,建议使用using语块,
|
||||
/// Enabled为false时,返回Disposable.Empty,
|
||||
/// 在写入锁定模式下重复执行,返回Disposable.Empty
|
||||
/// </summary>
|
||||
/// <exception cref="NotImplementedException">读取模式下不能进入写入锁定状态</exception>
|
||||
public IDisposable Write()
|
||||
{
|
||||
if (Enabled == false || _LockSlim.IsWriteLockHeld)
|
||||
{
|
||||
return Disposable.Empty;
|
||||
}
|
||||
else if (_LockSlim.IsReadLockHeld)
|
||||
{
|
||||
throw new NotImplementedException("读取模式下不能进入写入锁定状态");
|
||||
}
|
||||
else
|
||||
{
|
||||
_LockSlim.EnterWriteLock();
|
||||
return new Lock(_LockSlim, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace ProcessManageUI
|
||||
{
|
||||
internal class IniFile
|
||||
{
|
||||
public class IniFiles
|
||||
{
|
||||
public string path;
|
||||
[DllImport("kernel32")] //返回0表示失败,非0为成功
|
||||
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
|
||||
[DllImport("kernel32")] //返回取得字符串缓冲区的长度
|
||||
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
|
||||
/// <summary>
|
||||
/// 保存ini文件的路径
|
||||
/// 调用示例:var ini = IniFiles("C:\file.ini");
|
||||
/// </summary>
|
||||
/// <param name="INIPath"></param>
|
||||
public IniFiles(string iniPath)
|
||||
{
|
||||
this.path = iniPath;
|
||||
}
|
||||
/// <summary>
|
||||
/// 写Ini文件
|
||||
/// 调用示例:ini.IniWritevalue("Server","name","localhost");
|
||||
/// </summary>
|
||||
/// <param name="Section">[缓冲区]</param>
|
||||
/// <param name="Key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
public void IniWritevalue(string Section, string Key, string value)
|
||||
{
|
||||
WritePrivateProfileString(Section, Key, value, this.path);
|
||||
}
|
||||
/// <summary>
|
||||
/// 读Ini文件
|
||||
/// 调用示例:ini.IniWritevalue("Server","name");
|
||||
/// </summary>
|
||||
/// <param name="Section">[缓冲区]</param>
|
||||
/// <param name="Key">键</param>
|
||||
/// <returns>值</returns>
|
||||
public string IniReadvalue(string Section, string Key)
|
||||
{
|
||||
StringBuilder temp = new StringBuilder(255);
|
||||
|
||||
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
|
||||
return temp.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
|
||||
namespace ProcessManageUI
|
||||
{
|
||||
public class LogGing
|
||||
{
|
||||
public static void LogGingDATA(string dat)
|
||||
{
|
||||
string logpath = System.Environment.CurrentDirectory + "\\Log";//日志文件目录
|
||||
string logPath = "" + System.Environment.CurrentDirectory + "\\Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";//日志文件
|
||||
string Log_time = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]:";
|
||||
|
||||
if (Directory.Exists(logpath))//检查日志路径
|
||||
{
|
||||
if (!File.Exists(logPath))//检查日志文件并写入启动日志
|
||||
{
|
||||
FileStream fs = new FileStream(logPath, FileMode.CreateNew, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine(Log_time + dat);
|
||||
wr.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
FileStream fs = new FileStream(logPath, FileMode.Append, FileAccess.Write);
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine(Log_time + dat);
|
||||
wr.Close();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(logpath);
|
||||
directoryInfo.Create();//创建日志路径
|
||||
}
|
||||
}
|
||||
public static void ERRDATA(System.Exception dat)
|
||||
{
|
||||
string Log_time = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
string logpath = System.Environment.CurrentDirectory + "\\ERR";//日志文件目录
|
||||
// string logPathtxt = "" + System.Environment.CurrentDirectory + "\\Log\\"+ Log_time + "Log.txt";//日志文件
|
||||
// System.IO.DirectoryInfo log = new System.IO.DirectoryInfo();//生成日志文件目录
|
||||
string log_path = logpath + "\\ERR" + Log_time + ".txt";
|
||||
string Log_timehms = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
if (Directory.Exists(logpath))//检查日志路径
|
||||
{
|
||||
if (!File.Exists(log_path))//检查文件并写入
|
||||
{
|
||||
// FileStream fs = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建文件
|
||||
// StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
// wr.Close();
|
||||
FileStream fil = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wfil = new StreamWriter(fil);//创建文件
|
||||
wfil.WriteLine("[" + Log_timehms + "];[Error] ||" + Environment.NewLine.ToString());
|
||||
wfil.WriteLine("[" + Log_timehms + "];[Error source] ||" + dat.Source.ToString() + Environment.NewLine.ToString());
|
||||
wfil.WriteLine("[" + Log_timehms + "];[Error message] ||" + dat.Message.ToString() + Environment.NewLine.ToString());
|
||||
wfil.WriteLine("[" + Log_timehms + "];[Error area] ||" + dat.StackTrace.ToString());
|
||||
wfil.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
FileStream fs = new FileStream(log_path, FileMode.Append, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine("[" + Log_timehms + "];[Error] ||" + Environment.NewLine.ToString());
|
||||
wr.WriteLine("[" + Log_timehms + "];[Error source] ||" + dat.ToString() + Environment.NewLine.ToString());
|
||||
wr.WriteLine("[" + Log_timehms + "];[Error message] ||" + dat.Message.ToString() + Environment.NewLine.ToString());
|
||||
wr.WriteLine("[" + Log_timehms + "];[Error area] ||" + dat.ToString());
|
||||
wr.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(logpath);
|
||||
directoryInfo.Create();
|
||||
}
|
||||
}
|
||||
public static void ExchangeDATA(string dat)
|
||||
{
|
||||
string Log_time = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
string logpath = System.Environment.CurrentDirectory + "\\Exchange";//日志文件目录
|
||||
// string logPathtxt = "" + System.Environment.CurrentDirectory + "\\Log\\"+ Log_time + "Log.txt";//日志文件
|
||||
// System.IO.DirectoryInfo log = new System.IO.DirectoryInfo();//生成日志文件目录
|
||||
string log_path = logpath + "\\Exchange" + Log_time + ".txt";
|
||||
string Log_timehms = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
if (Directory.Exists(logpath))//检查日志路径
|
||||
{
|
||||
if (!File.Exists(log_path))//检查文件并写入
|
||||
{
|
||||
// FileStream fs = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建文件
|
||||
// StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
// wr.Close();
|
||||
FileStream fil = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wfil = new StreamWriter(fil);//创建文件
|
||||
wfil.WriteLine("[" + Log_timehms + "];[Exchange] ||" + dat);
|
||||
wfil.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
FileStream fs = new FileStream(log_path, FileMode.Append, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine("[" + Log_timehms + "];[Exchange] ||" + dat);
|
||||
wr.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(logpath);
|
||||
directoryInfo.Create();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Window x:Class="ProcessManageUI.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:ProcessManageUI"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="450" Width="800">
|
||||
<Grid>
|
||||
<TextBox x:Name="Datalog" Margin="30,30,300,30"/>
|
||||
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using System.Windows.Threading;
|
||||
using static ProcessManageUI.SqliteHelper;
|
||||
using static System.Net.WebRequestMethods;
|
||||
|
||||
namespace ProcessManageUI
|
||||
{
|
||||
/// <summary>
|
||||
/// MainWindow.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
private static IniFile.IniFiles Configini = new IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "ProcessManageConfigini.ini");
|
||||
private static string SQLIP = Configini.IniReadvalue("SQL_SERVER", "SQL1"); //读配置文件
|
||||
private static string SQLNAME = Configini.IniReadvalue("SQL_SERVER", "SQL2");
|
||||
private static string SQMOD = Configini.IniReadvalue("SQL_SERVER", "SQL3");
|
||||
private static string SQLUSER = Configini.IniReadvalue("SQL_SERVER", "SQL4");
|
||||
private static string SQLPASWORD = Configini.IniReadvalue("SQL_SERVER", "SQL5");
|
||||
private static DataTable DyelotsBulkedRecipe = new DataTable();
|
||||
private static DataTable Product = new DataTable();
|
||||
private static DataTable Gram = new DataTable();
|
||||
private static int Count = 0;
|
||||
private static readonly int _intervalMs = int.Parse(Configini.IniReadvalue("SQL_SERVER", "SQL6"));
|
||||
private static SQLiteHelper SQLiteHelpers = null; //定义数据库
|
||||
private readonly static string DBAddress = Environment.CurrentDirectory + "\\ProcessManage.db"; //数据库路径
|
||||
|
||||
//设置定时器
|
||||
DispatcherTimer disTimer = new DispatcherTimer
|
||||
{
|
||||
Interval = TimeSpan.FromMilliseconds(_intervalMs) //毫秒
|
||||
};
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
|
||||
SQLiteHelpers.Open(); //打开数据库
|
||||
Gram = SQLiteHelpers.ExecuteDataSet("select * from Gram", null).Tables[0]; //读取表写入缓存
|
||||
Product = SQLiteHelpers.ExecuteDataSet("select * from ProcessManage", null).Tables[0];
|
||||
SQLiteHelpers.Close();
|
||||
|
||||
disTimer.Tick += EXTask;
|
||||
disTimer.Start();//计时开始
|
||||
}
|
||||
|
||||
|
||||
private void EXTask(object sender, EventArgs e)
|
||||
{
|
||||
disTimer.Stop();//停止计时
|
||||
string Connstr_SC;
|
||||
string DyelotsBulkedRecipe_sql = "SELECT * FROM [dbo].[DyelotsBulkedRecipe] WHERE Created > '"
|
||||
+ DateTime.Now.AddDays(-1).ToString("yyyy/MM/dd") + "' AND ProductType = '1' AND Process IS NULL";
|
||||
try
|
||||
{
|
||||
DyelotsBulkedRecipe.Clear();
|
||||
Datalog.Text = null;
|
||||
|
||||
if (SQMOD == "Windows Authentication")
|
||||
{
|
||||
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";Trusted_Connection=SSPI";
|
||||
}
|
||||
else
|
||||
{
|
||||
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";User ID=" + SQLUSER + ";Password=" + SQLPASWORD;
|
||||
}
|
||||
SqlConnection conn_SC = new SqlConnection(Connstr_SC);
|
||||
conn_SC.OpenAsync(); //连接数据库
|
||||
SqlDataAdapter DyelotsBulkedRecipe_ = new SqlDataAdapter(DyelotsBulkedRecipe_sql, Connstr_SC);
|
||||
conn_SC.Close();
|
||||
DyelotsBulkedRecipe_.Fill(DyelotsBulkedRecipe);
|
||||
|
||||
for (int i = DyelotsBulkedRecipe.Rows.Count; i > 0; i--)
|
||||
{
|
||||
if (DyelotsBulkedRecipe.Rows.Count <= 0) { break; }//跳出循环
|
||||
|
||||
string Dyelot = DyelotsBulkedRecipe.Rows[0].Field<string>("Dyelot");
|
||||
double gram = 0;
|
||||
int type_id = 0;
|
||||
DataRow[] rowdat = DyelotsBulkedRecipe.Select("Dyelot ='" + Dyelot + "'");//行
|
||||
|
||||
foreach (DataRow row in rowdat)//删除指定信息行
|
||||
{
|
||||
row.Field<string>("ProductCode");
|
||||
gram += row.Field<double>("Grams");
|
||||
row.Delete();
|
||||
row.AcceptChanges();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Count++;
|
||||
Datalog.AppendText("\n" + DateTime.Now.ToString("yyyy/MM/dd-HH:mm:ss") + " 转换工单:" + Dyelot + "流程:");
|
||||
LogGing.LogGingDATA( "转换工单:" + Dyelot + "流程:");
|
||||
}
|
||||
Datalog.AppendText("\n" + DateTime.Now.ToString("yyyy/MM/dd-HH:mm:ss") + $" 转换{Count}条");
|
||||
Count = 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{//错误处理
|
||||
LogGing.ERRDATA(ex);
|
||||
Datalog.AppendText(ex.ToString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
disTimer.Start();//计时开始
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,108 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{6DA7C6F9-C096-481A-B9E1-C19E72C35E58}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<RootNamespace>ProcessManageUI</RootNamespace>
|
||||
<AssemblyName>ProcessManageUI</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xaml">
|
||||
<RequiredTargetFramework>4.0</RequiredTargetFramework>
|
||||
</Reference>
|
||||
<Reference Include="WindowsBase" />
|
||||
<Reference Include="PresentationCore" />
|
||||
<Reference Include="PresentationFramework" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ApplicationDefinition Include="App.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="ClsLock.cs" />
|
||||
<Compile Include="SqliteHelper.cs" />
|
||||
<Page Include="MainWindow.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
<Compile Include="App.xaml.cs">
|
||||
<DependentUpon>App.xaml</DependentUpon>
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="IniFile.cs" />
|
||||
<Compile Include="LogGing.cs" />
|
||||
<Compile Include="MainWindow.xaml.cs">
|
||||
<DependentUpon>MainWindow.xaml</DependentUpon>
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DesignTime>True</DesignTime>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Properties\Settings.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Settings.settings</DependentUpon>
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
</Compile>
|
||||
<EmbeddedResource Include="Properties\Resources.resx">
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
</EmbeddedResource>
|
||||
<None Include="ProcessManage.db" />
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Data.SQLite">
|
||||
<Version>1.0.119</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("ProcessManageUI")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("ProcessManageUI")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2025")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
//若要开始生成可本地化的应用程序,请设置
|
||||
//.csproj 文件中的 <UICulture>CultureYouAreCodingWith</UICulture>
|
||||
//在 <PropertyGroup> 中。例如,如果你使用的是美国英语。
|
||||
//使用的是美国英语,请将 <UICulture> 设置为 en-US。 然后取消
|
||||
//对以下 NeutralResourceLanguage 特性的注释。 更新
|
||||
//以下行中的“en-US”以匹配项目文件中的 UICulture 设置。
|
||||
|
||||
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
|
||||
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //主题特定资源词典所处位置
|
||||
//(未在页面中找到资源时使用,
|
||||
//或应用程序资源字典中找到时使用)
|
||||
ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置
|
||||
//(未在页面中找到资源时使用,
|
||||
//、应用程序或任何主题专用资源字典中找到时使用)
|
||||
)]
|
||||
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 此代码由工具生成。
|
||||
// 运行时版本: 4.0.30319.42000
|
||||
//
|
||||
// 对此文件的更改可能导致不正确的行为,如果
|
||||
// 重新生成代码,则所做更改将丢失。
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace ProcessManageUI.Properties
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 强类型资源类,用于查找本地化字符串等。
|
||||
/// </summary>
|
||||
// 此类是由 StronglyTypedResourceBuilder
|
||||
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
|
||||
// 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
|
||||
// (以 /str 作为命令选项),或重新生成 VS 项目。
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
internal class Resources
|
||||
{
|
||||
|
||||
private static global::System.Resources.ResourceManager resourceMan;
|
||||
|
||||
private static global::System.Globalization.CultureInfo resourceCulture;
|
||||
|
||||
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
internal Resources()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回此类使用的缓存 ResourceManager 实例。
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Resources.ResourceManager ResourceManager
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((resourceMan == null))
|
||||
{
|
||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ProcessManageUI.Properties.Resources", typeof(Resources).Assembly);
|
||||
resourceMan = temp;
|
||||
}
|
||||
return resourceMan;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重写当前线程的 CurrentUICulture 属性,对
|
||||
/// 使用此强类型资源类的所有资源查找执行重写。
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Globalization.CultureInfo Culture
|
||||
{
|
||||
get
|
||||
{
|
||||
return resourceCulture;
|
||||
}
|
||||
set
|
||||
{
|
||||
resourceCulture = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by a tool.
|
||||
// Runtime Version:4.0.30319.42000
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace ProcessManageUI.Properties
|
||||
{
|
||||
|
||||
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
|
||||
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
|
||||
{
|
||||
|
||||
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
|
||||
|
||||
public static Settings Default
|
||||
{
|
||||
get
|
||||
{
|
||||
return defaultInstance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<SettingsFile xmlns="uri:settings" CurrentProfile="(Default)">
|
||||
<Profiles>
|
||||
<Profile Name="(Default)" />
|
||||
</Profiles>
|
||||
<Settings />
|
||||
</SettingsFile>
|
||||
@@ -0,0 +1,686 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Data.Common;
|
||||
using System.Windows.Media.Animation;
|
||||
using System.Data.SQLite;
|
||||
using System.Windows.Shapes;
|
||||
using System.Drawing;
|
||||
|
||||
|
||||
namespace ProcessManageUI
|
||||
{
|
||||
public class SqliteHelper
|
||||
{
|
||||
|
||||
public class SQLiteHelper
|
||||
{
|
||||
#region 字段
|
||||
|
||||
/// <summary>
|
||||
/// 事务的基类
|
||||
/// </summary>
|
||||
private DbTransaction DBtrans;
|
||||
/// <summary>
|
||||
/// 使用静态变量字典解决多线程实例本类,实现一个数据库对应一个clslock
|
||||
/// </summary>
|
||||
private static readonly Dictionary<string, ClsLock> RWL = new Dictionary<string, ClsLock>();
|
||||
/// <summary>
|
||||
/// 数据库地址
|
||||
/// </summary>
|
||||
private readonly string mdataFile;
|
||||
/// <summary>
|
||||
/// 数据库密码
|
||||
/// </summary>
|
||||
private readonly string mPassWord;
|
||||
private readonly string LockName = null;
|
||||
/// <summary>
|
||||
/// 数据库连接定义
|
||||
/// </summary>
|
||||
private SQLiteConnection mConn;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 构造函数
|
||||
|
||||
/// <summary>
|
||||
/// 根据数据库地址初始化
|
||||
/// </summary>
|
||||
/// <param name="dataFile">数据库地址</param>
|
||||
public SQLiteHelper(string dataFile)
|
||||
{
|
||||
this.mdataFile = dataFile ?? throw new ArgumentNullException("dataFile=null");
|
||||
//this.mdataFile = AppDomain.CurrentDomain.BaseDirectory + dataFile;
|
||||
this.mdataFile = dataFile;
|
||||
if (!RWL.ContainsKey(dataFile))
|
||||
{
|
||||
LockName = dataFile;
|
||||
RWL.Add(dataFile, new ClsLock());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用密码打开数据库
|
||||
/// </summary>
|
||||
/// <param name="dataFile">数据库地址</param>
|
||||
/// <param name="PassWord">数据库密码</param>
|
||||
public SQLiteHelper(string dataFile, string PassWord)
|
||||
{
|
||||
this.mdataFile = dataFile ?? throw new ArgumentNullException("dataFile is null");
|
||||
this.mPassWord = PassWord ?? throw new ArgumentNullException("PassWord is null");
|
||||
//this.mdataFile = AppDomain.CurrentDomain.BaseDirectory + dataFile;
|
||||
this.mdataFile = dataFile;
|
||||
if (!RWL.ContainsKey(dataFile))
|
||||
{
|
||||
LockName = dataFile;
|
||||
RWL.Add(dataFile, new ClsLock());
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 打开/关闭 数据库
|
||||
|
||||
/// <summary>
|
||||
/// 打开 SQLiteManager 使用的数据库连接
|
||||
/// </summary>
|
||||
public void Open()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mPassWord))
|
||||
{
|
||||
mConn = OpenConnection(this.mdataFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
mConn = OpenConnection(this.mdataFile, mPassWord);
|
||||
}
|
||||
Console.WriteLine("The database was opened successfully");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭连接
|
||||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
if (this.mConn != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.mConn.Close();
|
||||
if (RWL.ContainsKey(LockName))
|
||||
{
|
||||
RWL.Remove(LockName);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("Shutdown failed");
|
||||
}
|
||||
}
|
||||
Console.WriteLine("The database was shut down successfully");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 事务
|
||||
|
||||
/// <summary>
|
||||
/// 开始事务
|
||||
/// </summary>
|
||||
public void BeginTrain()
|
||||
{
|
||||
EnsureConnection();
|
||||
DBtrans = mConn.BeginTransaction();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 提交事务
|
||||
/// </summary>
|
||||
public void DBCommit()
|
||||
{
|
||||
try
|
||||
{
|
||||
DBtrans.Commit();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
DBtrans.Rollback();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 工具
|
||||
|
||||
/// <summary>
|
||||
/// 打开一个SQLite数据库文件,如果文件不存在,则创建(无密码)
|
||||
/// </summary>
|
||||
/// <param name="dataFile"></param>
|
||||
/// <returns>SQLiteConnection 类</returns>
|
||||
private SQLiteConnection OpenConnection(string dataFile)
|
||||
{
|
||||
if (dataFile == null)
|
||||
{
|
||||
throw new ArgumentNullException("dataFiledataFile=null");
|
||||
}
|
||||
if (!File.Exists(dataFile))
|
||||
{
|
||||
SQLiteConnection.CreateFile(dataFile);
|
||||
}
|
||||
SQLiteConnection conn = new SQLiteConnection();
|
||||
SQLiteConnectionStringBuilder conStr = new SQLiteConnectionStringBuilder
|
||||
{
|
||||
DataSource = dataFile
|
||||
};
|
||||
conn.ConnectionString = conStr.ToString();
|
||||
conn.Open();
|
||||
return conn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开一个SQLite数据库文件,如果文件不存在,则创建(有密码)
|
||||
/// </summary>
|
||||
/// <param name="dataFile"></param>
|
||||
/// <param name="Password"></param>
|
||||
/// <returns>SQLiteConnection 类</returns>
|
||||
private SQLiteConnection OpenConnection(string dataFile, string Password)
|
||||
{
|
||||
if (dataFile == null)
|
||||
{
|
||||
throw new ArgumentNullException("dataFile=null");
|
||||
}
|
||||
if (!File.Exists(Convert.ToString(dataFile)))
|
||||
{
|
||||
SQLiteConnection.CreateFile(dataFile);
|
||||
}
|
||||
try
|
||||
{
|
||||
SQLiteConnection conn = new SQLiteConnection();
|
||||
SQLiteConnectionStringBuilder conStr = new SQLiteConnectionStringBuilder
|
||||
{
|
||||
DataSource = dataFile,
|
||||
Password = Password
|
||||
};
|
||||
conn.ConnectionString = conStr.ToString();
|
||||
conn.Open();
|
||||
return conn;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取 或 设置 SQLiteManager 使用的数据库连接
|
||||
/// </summary>
|
||||
public SQLiteConnection Connection
|
||||
{
|
||||
get
|
||||
{
|
||||
return mConn;
|
||||
}
|
||||
private set
|
||||
{
|
||||
mConn = value ?? throw new ArgumentNullException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 确保数据库是连接状态
|
||||
/// </summary>
|
||||
/// <exception cref="Exception"></exception>
|
||||
protected void EnsureConnection()
|
||||
{
|
||||
if (this.mConn == null)
|
||||
{
|
||||
throw new Exception("SQLiteManager.Connection=null");
|
||||
}
|
||||
if (mConn.State != ConnectionState.Open)
|
||||
{
|
||||
mConn.Open();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据库文件的路径
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetDataFile()
|
||||
{
|
||||
return this.mdataFile;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断表 table 是否存在
|
||||
/// </summary>
|
||||
/// <param name="table"></param>
|
||||
/// <returns>存在返回true,否则返回false</returns>
|
||||
public bool TableExists(string table)
|
||||
{
|
||||
if (table == null)
|
||||
{
|
||||
throw new ArgumentNullException("table=null");
|
||||
}
|
||||
EnsureConnection();
|
||||
SQLiteDataReader reader = ExecuteReader("SELECT count(*) as c FROM sqlite_master WHERE type='table' AND name=@tableName ", new SQLiteParameter[] { new SQLiteParameter("tableName", table) });
|
||||
if (reader == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
reader.Read();
|
||||
int c = reader.GetInt32(0);
|
||||
reader.Close();
|
||||
reader.Dispose();
|
||||
//return false;
|
||||
return c == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// VACUUM 命令(通过复制主数据库中的内容到一个临时数据库文件,然后清空主数据库,并从副本中重新载入原始的数据库文件)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool Vacuum()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (SQLiteCommand Command = new SQLiteCommand("VACUUM", Connection))
|
||||
{
|
||||
Command.ExecuteNonQuery();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (System.Data.SQLite.SQLiteException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 执行SQL
|
||||
|
||||
/// <summary>
|
||||
/// 执行SQL, 并返回 SQLiteDataReader 对象结果
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
/// <param name="paramArr">null 表示无参数</param>
|
||||
/// <returns></returns>
|
||||
public SQLiteDataReader ExecuteReader(string sql, SQLiteParameter[] paramArr)
|
||||
{
|
||||
if (sql == null)
|
||||
{
|
||||
throw new ArgumentNullException("sql=null");
|
||||
}
|
||||
EnsureConnection();
|
||||
using (RWL[LockName].Read())
|
||||
{
|
||||
using (SQLiteCommand cmd = new SQLiteCommand(sql, Connection))
|
||||
{
|
||||
if (paramArr != null)
|
||||
{
|
||||
cmd.Parameters.AddRange(paramArr);
|
||||
}
|
||||
try
|
||||
{
|
||||
SQLiteDataReader reader = cmd.ExecuteReader();
|
||||
cmd.Parameters.Clear();
|
||||
return reader;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行查询,并返回dataset对象
|
||||
/// </summary>
|
||||
/// <param name="sql">SQL查询语句</param>
|
||||
/// <param name="paramArr">参数数组</param>
|
||||
/// <returns></returns>
|
||||
public DataSet ExecuteDataSet(string sql, SQLiteParameter[] paramArr)
|
||||
{
|
||||
if (sql == null)
|
||||
{
|
||||
throw new ArgumentNullException("sql=null");
|
||||
}
|
||||
this.EnsureConnection();
|
||||
using (RWL[LockName].Read())
|
||||
{
|
||||
using (SQLiteCommand cmd = new SQLiteCommand(sql, this.Connection))
|
||||
{
|
||||
if (paramArr != null)
|
||||
{
|
||||
cmd.Parameters.AddRange(paramArr);
|
||||
}
|
||||
try
|
||||
{
|
||||
SQLiteDataAdapter da = new SQLiteDataAdapter();
|
||||
DataSet ds = new DataSet();
|
||||
da.SelectCommand = cmd;
|
||||
da.Fill(ds);
|
||||
cmd.Parameters.Clear();
|
||||
da.Dispose();
|
||||
return ds;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行SQL查询,并返回dataset对象。
|
||||
/// </summary>
|
||||
/// <param name="strTable">映射源表的名称</param>
|
||||
/// <param name="sql">SQL语句</param>
|
||||
/// <param name="paramArr">SQL参数数组</param>
|
||||
/// <returns></returns>
|
||||
public DataSet ExecuteDataSet(string strTable, string sql, SQLiteParameter[] paramArr)
|
||||
{
|
||||
if (sql == null)
|
||||
{
|
||||
throw new ArgumentNullException("sql=null");
|
||||
}
|
||||
this.EnsureConnection();
|
||||
using (RWL[LockName].Read())
|
||||
{
|
||||
using (SQLiteCommand cmd = new SQLiteCommand(sql, this.Connection))
|
||||
{
|
||||
if (paramArr != null)
|
||||
{
|
||||
cmd.Parameters.AddRange(paramArr);
|
||||
}
|
||||
try
|
||||
{
|
||||
SQLiteDataAdapter da = new SQLiteDataAdapter();
|
||||
DataSet ds = new DataSet();
|
||||
da.SelectCommand = cmd;
|
||||
da.Fill(ds, strTable);
|
||||
cmd.Parameters.Clear();
|
||||
da.Dispose();
|
||||
return ds;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行SQL,返回受影响的行数,可用于执行表创建语句,paramArr == null 表示无参数
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns></returns>
|
||||
public int ExecuteNonQuery(string sql, SQLiteParameter[] paramArr)
|
||||
{
|
||||
if (sql == null)
|
||||
{
|
||||
throw new ArgumentNullException("sql=null");
|
||||
}
|
||||
this.EnsureConnection();
|
||||
using (RWL[LockName].Read())
|
||||
{
|
||||
try
|
||||
{
|
||||
using (SQLiteCommand cmd = new SQLiteCommand(sql, Connection))
|
||||
{
|
||||
if (paramArr != null)
|
||||
{
|
||||
foreach (SQLiteParameter p in paramArr)
|
||||
{
|
||||
cmd.Parameters.Add(p);
|
||||
}
|
||||
}
|
||||
int c = cmd.ExecuteNonQuery();
|
||||
cmd.Parameters.Clear();
|
||||
return c;
|
||||
}
|
||||
}
|
||||
catch (SQLiteException)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行SQL,返回结果集第一行,如果结果集为空,那么返回空 List(List.Count=0),
|
||||
/// rowWrapper = null 时,使用 WrapRowToDictionary
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
/// <param name="paramArr"></param>
|
||||
/// <returns></returns>
|
||||
public object ExecuteScalar(string sql, SQLiteParameter[] paramArr)
|
||||
{
|
||||
if (sql == null)
|
||||
{
|
||||
throw new ArgumentNullException("sql=null");
|
||||
}
|
||||
this.EnsureConnection();
|
||||
using (RWL[LockName].Read())
|
||||
{
|
||||
using (SQLiteCommand cmd = new SQLiteCommand(sql, Connection))
|
||||
{
|
||||
if (paramArr != null)
|
||||
{
|
||||
cmd.Parameters.AddRange(paramArr);
|
||||
}
|
||||
try
|
||||
{
|
||||
object reader = cmd.ExecuteScalar();
|
||||
cmd.Parameters.Clear();
|
||||
cmd.Dispose();
|
||||
return reader;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询一行记录,无结果时返回 null,conditionCol = null 时将忽略条件,直接执行 select * from table
|
||||
/// </summary>
|
||||
/// <param name="table">表名</param>
|
||||
/// <param name="conditionCol"></param>
|
||||
/// <param name="conditionVal"></param>
|
||||
/// <returns></returns>
|
||||
public object QueryOne(string table, string conditionCol, object conditionVal)
|
||||
{
|
||||
if (table == null)
|
||||
{
|
||||
throw new ArgumentNullException("table=null");
|
||||
}
|
||||
this.EnsureConnection();
|
||||
string sql = "select * from " + table;
|
||||
if (conditionCol != null)
|
||||
{
|
||||
sql += " where " + conditionCol + "=@" + conditionCol;
|
||||
}
|
||||
object result = ExecuteScalar(sql, new SQLiteParameter[] { new SQLiteParameter(conditionCol, conditionVal) });
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 增 删 改
|
||||
|
||||
/// <summary>
|
||||
/// 执行 insert into 语句
|
||||
/// </summary>
|
||||
/// <param name="table"></param>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
public int InsertData(string table, Dictionary<string, object> entity)
|
||||
{
|
||||
if (table == null)
|
||||
{
|
||||
throw new ArgumentNullException("table=null");
|
||||
}
|
||||
this.EnsureConnection();
|
||||
string sql = BuildInsert(table, entity);
|
||||
return this.ExecuteNonQuery(sql, BuildParamArray(entity));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行 update 语句,注意:如果 where = null,那么 whereParams 也为 null,
|
||||
/// </summary>
|
||||
/// <param name="table">表名</param>
|
||||
/// <param name="entity">要修改的列名和列名的值</param>
|
||||
/// <param name="where">查找符合条件的列</param>
|
||||
/// <param name="whereParams">where条件中参数的值</param>
|
||||
/// <returns></returns>
|
||||
public int Update(string table, Dictionary<string, object> entity, string where, SQLiteParameter[] whereParams)
|
||||
{
|
||||
if (table == null)
|
||||
{
|
||||
throw new ArgumentNullException("table=null");
|
||||
}
|
||||
this.EnsureConnection();
|
||||
string sql = BuildUpdate(table, entity);
|
||||
SQLiteParameter[] parameter = BuildParamArray(entity);
|
||||
if (where != null)
|
||||
{
|
||||
sql += " where " + where;
|
||||
if (whereParams != null)
|
||||
{
|
||||
SQLiteParameter[] newArr = new SQLiteParameter[(parameter.Length + whereParams.Length)];
|
||||
Array.Copy(parameter, newArr, parameter.Length);
|
||||
Array.Copy(whereParams, 0, newArr, parameter.Length, whereParams.Length);
|
||||
parameter = newArr;
|
||||
}
|
||||
}
|
||||
return this.ExecuteNonQuery(sql, parameter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行 delete from table 语句,where不必包含'where'关键字,where = null 时将忽略 whereParams
|
||||
/// </summary>
|
||||
/// <param name="table"></param>
|
||||
/// <param name="where"></param>
|
||||
/// <param name="whereParams"></param>
|
||||
/// <returns></returns>
|
||||
public int Delete(string table, string where, SQLiteParameter[] whereParams)
|
||||
{
|
||||
if (table == null)
|
||||
{
|
||||
throw new ArgumentNullException("table=null");
|
||||
}
|
||||
this.EnsureConnection();
|
||||
string sql = "delete from " + table + " ";
|
||||
if (where != null)
|
||||
{
|
||||
sql += "where " + where;
|
||||
}
|
||||
return ExecuteNonQuery(sql, whereParams);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 Dictionary 类型数据 转换为 SQLiteParameter[] 类型
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
private SQLiteParameter[] BuildParamArray(Dictionary<string, object> entity)
|
||||
{
|
||||
List<SQLiteParameter> list = new List<SQLiteParameter>();
|
||||
foreach (string key in entity.Keys)
|
||||
{
|
||||
list.Add(new SQLiteParameter(key, entity[key]));
|
||||
}
|
||||
if (list.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return list.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 Dictionary 类型数据 转换为 插入数据 的 SQL语句
|
||||
/// </summary>
|
||||
/// <param name="table">表名</param>
|
||||
/// <param name="entity">字典</param>
|
||||
/// <returns></returns>
|
||||
private string BuildInsert(string table, Dictionary<string, object> entity)
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.Append("insert into ").Append(table);
|
||||
buf.Append(" (");
|
||||
foreach (string key in entity.Keys)
|
||||
{
|
||||
buf.Append(key).Append(",");
|
||||
}
|
||||
buf.Remove(buf.Length - 1, 1); // 移除最后一个,
|
||||
buf.Append(") ");
|
||||
buf.Append("values(");
|
||||
foreach (string key in entity.Keys)
|
||||
{
|
||||
buf.Append("@").Append(key).Append(","); // 创建一个参数
|
||||
}
|
||||
buf.Remove(buf.Length - 1, 1);
|
||||
buf.Append(") ");
|
||||
|
||||
return buf.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 Dictionary 类型数据 转换为 修改数据 的 SQL语句
|
||||
/// </summary>
|
||||
/// <param name="table">表名</param>
|
||||
/// <param name="entity">字典</param>
|
||||
/// <returns></returns>
|
||||
private string BuildUpdate(string table, Dictionary<string, object> entity)
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.Append("update ").Append(table).Append(" set ");
|
||||
foreach (string key in entity.Keys)
|
||||
{
|
||||
buf.Append(key).Append("=").Append("@").Append(key).Append(",");
|
||||
}
|
||||
buf.Remove(buf.Length - 1, 1);
|
||||
buf.Append(" ");
|
||||
return buf.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 DataTable 转换为 Dictionary 类型数据
|
||||
/// </summary>
|
||||
public Dictionary<string, object> DataTableToDictionary(DataTable dataTable)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
if (dataTable != null)
|
||||
{
|
||||
foreach (DataRow dataRow in dataTable.Rows)
|
||||
{
|
||||
foreach (DataColumn dataColumn in dataTable.Columns)
|
||||
{
|
||||
result.Add(dataColumn.ColumnName, dataRow[dataColumn].ToString());
|
||||
//result = Console.WriteLine(dataRow[dataColumn].ToString());
|
||||
//result.Add(dataColumn.ColumnName, dataRow[dataColumn].ToString())(new RepeatDictionaryComparer());
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user