Archived
Compare commits
6
Commits
50577e1d51
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5f558fae5 | ||
|
|
800cf3004d | ||
|
|
7bac465084 | ||
|
|
d530b35e35 | ||
|
|
4c7c353074 | ||
|
|
8f6f633278 |
No files matched your search
@@ -0,0 +1,138 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProcessManage
|
||||||
|
{
|
||||||
|
/// <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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,8 +33,11 @@
|
|||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="PresentationFramework" />
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Drawing" />
|
||||||
|
<Reference Include="System.Windows.Forms" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
@@ -43,13 +46,24 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="ClsLock.cs" />
|
||||||
<Compile Include="IniFile.cs" />
|
<Compile Include="IniFile.cs" />
|
||||||
<Compile Include="LogGing.cs" />
|
<Compile Include="LogGing.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="SqliteHelper.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="App.config" />
|
<None Include="App.config" />
|
||||||
|
<None Include="ProcessManage.db" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="System.Data.SQLite">
|
||||||
|
<Version>1.0.119</Version>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="sunlight_logo.ico" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
||||||
Binary file not shown.
+106
-20
@@ -1,17 +1,36 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
namespace ProcessManage
|
namespace ProcessManage
|
||||||
{
|
{
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
static extern bool ShowWindow(IntPtr hWnd, int cmdShow);
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll")]
|
||||||
|
static extern IntPtr GetConsoleWindow();
|
||||||
|
|
||||||
|
const int SW_HIDE = 0;
|
||||||
|
const int SW_SHOW = 5;
|
||||||
|
const int SW_MINIMIZE = 6;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static IniFile.IniFiles Configini = new IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "ProcessManageConfigini.ini");
|
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 SQLIP = Configini.IniReadvalue("SQL_SERVER", "SQL1"); //读配置文件
|
||||||
private static string SQLNAME = Configini.IniReadvalue("SQL_SERVER", "SQL2");
|
private static string SQLNAME = Configini.IniReadvalue("SQL_SERVER", "SQL2");
|
||||||
@@ -19,13 +38,36 @@ namespace ProcessManage
|
|||||||
private static string SQLUSER = Configini.IniReadvalue("SQL_SERVER", "SQL4");
|
private static string SQLUSER = Configini.IniReadvalue("SQL_SERVER", "SQL4");
|
||||||
private static string SQLPASWORD = Configini.IniReadvalue("SQL_SERVER", "SQL5");
|
private static string SQLPASWORD = Configini.IniReadvalue("SQL_SERVER", "SQL5");
|
||||||
private static DataTable DyelotsBulkedRecipe = new DataTable();
|
private static DataTable DyelotsBulkedRecipe = new DataTable();
|
||||||
private static Timer _timer;
|
private static System.Threading.Timer _timer;
|
||||||
private static int Count = 0;
|
private static int Count = 0;
|
||||||
private static readonly int _intervalMs = int.Parse( Configini.IniReadvalue("SQL_SERVER", "SQL6"));
|
private static readonly int _intervalMs = int.Parse( Configini.IniReadvalue("SQL_SERVER", "SQL6"));
|
||||||
|
private static DataTable Product = new DataTable();
|
||||||
|
private static DataTable Gram = new DataTable();
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
var handle = GetConsoleWindow();
|
||||||
|
// ShowWindow(handle, SW_MINIMIZE);
|
||||||
|
ShowWindow(handle, SW_HIDE);
|
||||||
|
|
||||||
|
// 托盘图标部分
|
||||||
|
var notifyIcon = new NotifyIcon();
|
||||||
|
notifyIcon.Icon = new System.Drawing.Icon("./sunlight_logo.ico");
|
||||||
|
notifyIcon.Visible = true;
|
||||||
|
notifyIcon.Text = "Process选择";
|
||||||
|
notifyIcon.MouseClick += (s, e) =>
|
||||||
|
{
|
||||||
|
if (e.Button == MouseButtons.Left)
|
||||||
|
{
|
||||||
|
ShowWindow(handle, SW_SHOW);
|
||||||
|
//Process.GetCurrentProcess().MainWindowHandle; // 强制刷新焦点
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine("Process选择功能启动");
|
Console.WriteLine("Process选择功能启动");
|
||||||
_timer = new Timer(
|
_timer = new System.Threading.Timer(
|
||||||
callback: EXTask,
|
callback: EXTask,
|
||||||
state: null,
|
state: null,
|
||||||
dueTime: 100,
|
dueTime: 100,
|
||||||
@@ -40,31 +82,74 @@ namespace ProcessManage
|
|||||||
private static void EXTask(object state)
|
private static void EXTask(object state)
|
||||||
{
|
{
|
||||||
string Connstr_SC;
|
string Connstr_SC;
|
||||||
string DyelotsBulkedRecipe_sql = "SELECT * FROM [dbo].[DyelotsBulkedRecipe] WHERE Created > '"
|
string DyelotsBulkedRecipe_sql =
|
||||||
+ DateTime.Now.AddDays(-1).ToString("yyyy/MM/dd") + "' AND Process IS NULL";
|
"SELECT * FROM [dbo].[DyelotsBulkedRecipe] WHERE Created > '" + DateTime.Now.AddDays(-1).ToString("yyyy/MM/dd") +
|
||||||
|
"' AND ProductType = '1' AND Process IS NULL AND StepNumber IS NOT NULL AND ProductCode IS NOT NULL AND ReDye IS NOT 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);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
DyelotsBulkedRecipe.Clear();
|
DyelotsBulkedRecipe.Clear();
|
||||||
|
|
||||||
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);
|
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");
|
||||||
|
int ReDye = DyelotsBulkedRecipe.Rows[0].Field<int>("ReDye");
|
||||||
|
int StepNumber = DyelotsBulkedRecipe.Rows[0].Field<int>("StepNumber");
|
||||||
|
double gram = 0;
|
||||||
|
int type_id = 0;
|
||||||
|
DataRow[] rowdat = DyelotsBulkedRecipe.Select("Dyelot ='" + Dyelot + "' AND ReDye='" + ReDye + "' AND StepNumber ='" + StepNumber + "'");//行
|
||||||
|
|
||||||
|
foreach (DataRow row in rowdat)//删除指定信息行
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int a = Product.Select("ProductCode = " + row.Field<string>("ProductCode")).First().Field<int>("ProductType");
|
||||||
|
if (a > type_id) type_id = a;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogGing.ERRDATA(ex);
|
||||||
|
Console.Write(ex.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
gram += row.Field<double>("Grams");
|
||||||
|
row.Delete();
|
||||||
|
row.AcceptChanges();
|
||||||
|
}
|
||||||
|
string NAME = "Process";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
NAME = Gram.Select("Type='" + type_id + "' and min<=" + (int)gram + " and max>" + (int)gram + "").First().Field<string>("name");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogGing.ERRDATA(ex);
|
||||||
|
Console.Write(ex.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
Console.Write("\n"+DateTime.Now.ToString("yyyy/MM/dd-HH:mm:ss")+$"转换{Count}条");
|
string DyelotsBulkedRecipe_updata = "UPDATE [dbo].[DyelotsBulkedRecipe] SET Process ='" + NAME +
|
||||||
|
"'WHERE Dyelot ='" + Dyelot + "' AND ReDye='" + ReDye + "' AND StepNumber ='" + StepNumber + "'";
|
||||||
|
SqlCommand DyelotsBulkedRecipe_UP = new SqlCommand(DyelotsBulkedRecipe_updata, conn_SC);
|
||||||
|
Count++;
|
||||||
|
Console.Write("\n" + DateTime.Now.ToString("yyyy/MM/dd-HH:mm:ss") +
|
||||||
|
" 转换工单:" + Dyelot + "-重染:" + ReDye + "-步骤:" + StepNumber + "\n影响行数" +
|
||||||
|
DyelotsBulkedRecipe_UP.ExecuteNonQuery() + "-流程:" + NAME);
|
||||||
|
LogGing.LogGingDATA("转换工单:" + Dyelot + "重染" + ReDye + "步骤" + StepNumber + "流程:" + NAME);
|
||||||
|
}
|
||||||
|
// Console.Write("\n" + DateTime.Now.ToString("yyyy/MM/dd-HH:mm:ss") + $" 转换完成{Count}条");
|
||||||
Count = 0;
|
Count = 0;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -74,9 +159,10 @@ namespace ProcessManage
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
conn_SC.Close();
|
||||||
_timer.Change(
|
_timer.Change(
|
||||||
dueTime: _intervalMs,
|
dueTime: _intervalMs,
|
||||||
period: Timeout.Infinite);//设定下次触发
|
period: Timeout.Infinite);//设定下次触发
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,684 @@
|
|||||||
|
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.Data.SQLite;
|
||||||
|
using System.Drawing;
|
||||||
|
|
||||||
|
|
||||||
|
namespace ProcessManage
|
||||||
|
{
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
Reference in new issue
Block a user