添加项目文件。

This commit is contained in:
sc committed 2026-05-01 14:42:51 +08:00
1 parent 15313a5f75
commit dd43cf0135
30 files changed
+3002

No files matched your search

+208
View File
@@ -0,0 +1,208 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text;
using TouchSocket.Core;
using TouchSocket.Sockets;
using SunlightAggregationManager.ViewModel;
using System.Security.Cryptography.Pkcs;
namespace SunlightAggregationManager.UserClass
{
public class AsyncTcpServer
{
public class Person
{
public required string User { get; set; }
public required string Password { get; set; }
public required string Terminal { get; set; }
public required string Dat { get; set; }
}
public static TcpService service = new TcpService();
public static async Task TcpMain(int port1 ,int port2)
{
NetFwManger.AllowPort(port1, "TCP");//开放7790端口
NetFwManger.AllowPort(port2, "TCP");//开放7790端口
//TcpService service = new TcpService();
service.Connecting = (client, e) => { return EasyTask.CompletedTask; };//有客户端正在连接
service.Connected = (client, e) => {return EasyTask.CompletedTask;};//有客户端成功连接
service.Closing = (client, e) => { return EasyTask.CompletedTask; };//有客户端正在断开连接,只有当主动断开时才有效。
service.Closed = (client, e) => {
//离线记录
Dictionary<string, object> myDictOff = new Dictionary<string, object>();
myDictOff.Add("State", 15);
myDictOff.Add("LastActionTime", DateTime.Now);
myDictOff.Add("LastOfflineTime", DateTime.Now);
myDictOff.Add("Note", "Offline");
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "LinkID='" + client.Id + "'", myDictOff);
Console.WriteLine("[Offline]IP[" + client.IP + "]ID["+ client.Id + "]");
return EasyTask.CompletedTask;
};//有客户端断开连接
service.Received = (client, e) =>{
string DAT = e.Memory.Span.ToString(Encoding.UTF8);
try
{
Person? deserializedPerson = JsonSerializer.Deserialize<Person>(DAT);
var _user = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData, "State", "User='" + deserializedPerson?.User +
"' and Password ='" + deserializedPerson?.Password + "'");
if (_user != null)
{
string _dat = _user?.ToString() ?? "0";
if (_dat != "99")
{
if (_dat == "20")
{//行为
Dictionary<string, object> myDict = new Dictionary<string, object>();
myDict.Add("LinkID", client.Id);
myDict.Add("LastLoginIP", client.IP);
myDict.Add("LastActionTime", DateTime.Now);
myDict.Add("Note", deserializedPerson?.Dat ?? "unknown");
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" + deserializedPerson?.User + "'", myDict);
Console.WriteLine("[Log on]User[" + deserializedPerson?.User + "]Note[" + (deserializedPerson?.Dat ?? "unknown") + "]");
}
else
{//首次登录记录
Dictionary<string, object> myDict = new Dictionary<string, object>();
myDict.Add("State", 20);
myDict.Add("LinkID", client.Id);
myDict.Add("LastLoginIP", client.IP);
myDict.Add("LastLoginTime", DateTime.Now);
myDict.Add("LastLoginTerminal", deserializedPerson?.Terminal ?? "unknown device");
myDict.Add("LastActionTime", DateTime.Now);
myDict.Add("Note", deserializedPerson?.Dat ?? "unknown");
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" + deserializedPerson?.User + "'", myDict);
Console.WriteLine("[Log on]User[" + deserializedPerson?.User + "]IP[" + client.IP +
"]ID[" + client.Id + "]Terminal[" + (deserializedPerson?.Terminal ?? "unknown device") + "]");
}
}
else
{//停用账号关闭连接
client.SendAsync("Account Deactivated");
foreach (var item in AsyncTcpServer.service.Clients)
{
if (item.Id == client.Id)
{
item.CloseAsync();
//在断开连接后,释放对象。
item.Dispose();
}
}
}
}
else
{ //无效账号关闭连接
client.SendAsync("Invalid Account");
foreach (var item in AsyncTcpServer.service.Clients)
{
if (item.Id == client.Id)
{
item.CloseAsync();
item.Dispose();
}
}
}
}
catch (Exception ex)
{//错误
Console.WriteLine("Tcp:" + ex.ToString());
client.SendAsync("Target instruction parsing error manager will close connection");
foreach (var item in AsyncTcpServer.service.Clients)
{
if (item.Id == client.Id)
{
item.CloseAsync();
item.Dispose();
}
}
}
//string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8).Substring(5);
//string SYSDAT = "";// = e.ByteBlock.Span.ToString(Encoding.ASCII).Substring(5);
// string SYSKEY = "";
// if (DAT.Length >= 16) SYSKEY = DAT.Substring(0, 16);
// if (DAT.Length > 16) SYSDAT = DAT.Substring(16);
return EasyTask.CompletedTask;
};
await service.SetupAsync(new TouchSocketConfig()//载入配置
// .SetMaxBufferSize(1024 * 1024 * 100)
//.SetMinBufferSize(1024 * 1024)
.SetListenIPHosts(new IPHost[] { new IPHost(port1), new IPHost(port2) })//同时监听两个地址
.ConfigureContainer(a =>//容器的配置顺序应该在最前面
{
})
.ConfigurePlugins(a =>
{
})
);
await service.StartAsync();//启动
Console.WriteLine("TCP Port:"+ port1.ToString());
Console.WriteLine("TCP Port:"+ port2.ToString());
Console.WriteLine("TCP SERVER:START");
}
}
class MyTcpService : TcpService<MyTcpSessionClient>
{
protected override MyTcpSessionClient NewClient()
{
return new MyTcpSessionClient();
}
}
class MyTcpSessionClient : TcpSessionClient
{
internal void SetDataHandlingAdapter(SingleStreamDataHandlingAdapter adapter)
{
base.SetAdapter(adapter);
}
}
/// <summary>
/// 此插件实现,按照不同端口,使用不同适配器。
/// <list type="bullet">
/// <item>7789端口:使用"**"结尾的数据</item>
/// <item>7790端口:使用"##"结尾的数据</item>
/// </list>
/// </summary>
internal class DifferentProtocolPlugin : PluginBase, ITcpConnectingPlugin, ITcpReceivedPlugin
{
public async Task OnTcpConnecting(ITcpSession client, ConnectingEventArgs e)
{
if (client is MyTcpSessionClient sessionClient)
{
if (sessionClient.ServicePort == 7789)
{
sessionClient.SetDataHandlingAdapter(new TerminatorPackageAdapter("**"));
}
else
{
sessionClient.SetDataHandlingAdapter(new TerminatorPackageAdapter("##"));
}
}
await e.InvokeNext();
}
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
{
//如果是自定义适配器,此处解析时,可以判断e.RequestInfo的类型
if (client is ITcpSessionClient sessionClient)
{
sessionClient.Logger.Info($"{sessionClient.GetIPPort()}收到数据,服务器端口:{sessionClient.ServicePort},数据:{e.Memory.Span.ToString(Encoding.UTF8)}");
}
await e.InvokeNext();
}
}
}
+79
View File
@@ -0,0 +1,79 @@
using CommunityToolkit.Mvvm.Input;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using TouchSocket.Sockets;
namespace SunlightAggregationManager.UserClass
{
public class DataBase
{
private string DB_NAME = null!;
private string DB_TYPE = null!;
private string DB_ID = null!;
private string DB_IP = null!;
private string DB_PASSWORD = null!;
public void Config(string ip, string name, string type, string id, string password)
{
DB_IP = ip;
DB_ID = id;
DB_NAME = name;
DB_TYPE = type;
DB_PASSWORD = password;
_= ConfigAsync();
}
public async Task ConfigAsync()
{
await Task.Delay(3000);
//进入后台线程操作
await Task.Run(() =>
{
try
{
// 构建连接字符串
var builder = new SqlConnectionStringBuilder
{
DataSource = DB_IP,
InitialCatalog = DB_NAME,
IntegratedSecurity = DB_TYPE == "Windows Authentication",
TrustServerCertificate = true,
ConnectTimeout = 5
};
if (!builder.IntegratedSecurity)
{
builder.UserID = DB_ID;
builder.Password = DB_PASSWORD;
}
//连接数据库
using var conn_SC = new SqlConnection(builder.ConnectionString);
conn_SC.Open();
using (var cmd = new SqlCommand("SELECT 1", conn_SC))
{
cmd.ExecuteReader();
}
}
catch (Exception ex)
{
Console.WriteLine("DataBase Link timeout\n"+ex.ToString());
// 失败处理
return; // 直接返回,不执行后续代码
}
Console.WriteLine("DataBase Link succeed");
});
}
public void Database(string dat)
{
}
}
}
+83
View File
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Input;
namespace SunlightAggregationManager.UserClass
{
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)
{
// 确保路径有效
if (!File.Exists(path))
{
// 如果文件不存在,创建一个空的
using (FileStream fs = File.Create(path)) { }
}
// 处理 null 值,避免 API 调用出现问题
string safeValue = value ?? string.Empty;
long result = WritePrivateProfileString(Section, Key, safeValue, path);
if (result==0)
{
// 可以选择抛出异常或记录日志
// throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
/// <summary>
/// 读Ini文件
/// 调用示例:ini.IniWritevalue("Server","name");
/// </summary>
/// <param name="Section">[缓冲区]</param>
/// <param name="Key">键</param>
/// <returns>值</returns>
public string IniReadvalue(string Section, string Key)
{
try
{
if (!File.Exists(path))
{
return string.Empty;
}
var sb = new StringBuilder(4096);
GetPrivateProfileString(Section, Key, string.Empty, sb, sb.Capacity, path);
return sb.ToString();
}
catch (Exception)
{//错误返回0
return "0";
}
}
}
}
}
+75
View File
@@ -0,0 +1,75 @@
using NetFwTypeLib;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace SunlightAggregationManager.UserClass
{
public class NetFwManger
{
private static string GetName(int port, string protocol)
{
return "port-" + protocol + "-" + port;
}
public static void AllowPort(int port, string protocol)
{
DelPort(port, protocol);
//创建一个INetFwRule对象
Type type = Type.GetTypeFromProgID("HNetCfg.FwRule") ?? throw new
InvalidOperationException("Failed to retrieve HNetCfg.FwRule type. Ensure the COM component is registered.");
INetFwRule? rule = (INetFwRule?)Activator.CreateInstance(type);
//设置规则的属性
rule?.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; //允许连接
rule?.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN; //入站规则
rule?.Enabled = true; //启用规则
rule?.InterfaceTypes = "All"; //适用于所有网络接口
rule?.Name = GetName(port, protocol); //规则名称
if (protocol.ToLower() == "tcp")
{
rule?.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; //TCP协议
}
else
{
rule?.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; //UDP协议
}
rule?.LocalPorts = "" + port; //本地端口号
//获取FirewallPolicy对象
Type policyType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2") ?? throw new
InvalidOperationException("Failed to retrieve HNetCfg.FwRule type. Ensure the COM component is registered.");
INetFwPolicy2? policy = (INetFwPolicy2?)Activator.CreateInstance(policyType);
//将规则添加到防火墙策略中
policy?.Rules.Add(rule);
}
public static void DelPort(int port, string protocol)
{
//获取FirewallPolicy对象
Type policyType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2") ?? throw new
InvalidOperationException("Failed to retrieve HNetCfg.FwRule type. Ensure the COM component is registered.");
INetFwPolicy2? policy = (INetFwPolicy2?)Activator.CreateInstance(policyType);
//获取现有的规则集合
INetFwRules? rules = policy?.Rules;
if (rules != null)
{
//查找名称的规则并删除它
foreach (INetFwRule rule in rules)
{
if (rule.Name == GetName(port, protocol))
{
rules.Remove(rule.Name);
Console.WriteLine(@"Firewall rule deleted successfully.");
break;
}
}
}
}
}
}
+510
View File
@@ -0,0 +1,510 @@
using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SunlightAggregationManager.UserClass
{
// 辅助锁类 (代码中引用了 ClsLock,这里提供一个简单的实现或占位)
// 请根据实际情况替换为你项目中的 ClsLock,或使用 ReaderWriterLockSlim
public class ClsLock : IDisposable
{
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
public IDisposable Read()
{
_lock.EnterReadLock();
return new DisposableAction(() => _lock.ExitReadLock());
}
public void Dispose() => _lock?.Dispose();
private class DisposableAction : IDisposable
{
private readonly Action _action;
public DisposableAction(Action action) => _action = action;
public void Dispose() => _action?.Invoke();
}
}
public class SqliteHelper
{
#region 字段
private SqliteTransaction? dbTrans = null!;
private static readonly Dictionary<string, ClsLock> RWL = new Dictionary<string, ClsLock>();
private readonly string mDataFile;
private readonly string mPassWord = "";
private readonly string lockName = "";
private SqliteConnection mConn = null!;
#endregion
#region 构造函数
public SqliteHelper(string dataFile)
{
this.mDataFile = dataFile ?? throw new ArgumentNullException(nameof(dataFile));
this.mDataFile = dataFile; // 注意:原代码有两次赋值,保留逻辑
if (!RWL.ContainsKey(dataFile))
{
lockName = dataFile;
RWL.Add(dataFile, new ClsLock());
}
}
public SqliteHelper(string dataFile, string PassWord)
{
this.mDataFile = dataFile ?? throw new ArgumentNullException(nameof(dataFile));
this.mPassWord = PassWord ?? throw new ArgumentNullException(nameof(PassWord));
this.mDataFile = dataFile;
if (!RWL.ContainsKey(dataFile))
{
lockName = dataFile;
RWL.Add(dataFile, new ClsLock());
}
}
#endregion
#region 打开/关闭 数据库
// 注意:Microsoft.Data.Sqlite.Core 不支持连接字符串构建器,需手动拼接
// 加密支持通常需要 Microsoft.Data.Sqlite.Core + SQLitePCLRaw.bundle_green (且加密功能可能受限)
public void Open()
{
// 构建连接字符串
var connectionString = $"Data Source={mDataFile}";
if (!string.IsNullOrWhiteSpace(mPassWord))
{
connectionString += $";Password={mPassWord}"; // 注意:原生 Microsoft.Data.Sqlite 不支持加密
// 如果需要加密,通常需要使用 SQLitePCLRaw 的特定提供程序或第三方库
}
mConn = new SqliteConnection(connectionString);
// 如果文件不存在,Open() 会自动创建数据库文件
// 但表结构需要你自己执行 CREATE TABLE
mConn.Open();
Console.WriteLine("The database was opened successfully");
}
public void Close()
{
if (this.mConn != null)
{
try
{
this.mConn.Close();
// 注意:不要在这里移除 RWL,因为这是静态的,可能影响其他实例
// if (RWL.ContainsKey(LockName)) { RWL.Remove(LockName); }
}
catch
{
Console.WriteLine("Shutdown failed");
}
}
Console.WriteLine("The database was shut down successfully");
}
#endregion
#region 事务
public void BeginTrain()
{
EnsureConnection();
dbTrans = mConn.BeginTransaction();
}
public void DBCommit()
{
try
{
dbTrans?.Commit();
dbTrans?.Dispose();
dbTrans = null; // 提交后置空
}
catch (Exception)
{
dbTrans?.Rollback();
dbTrans?.Dispose();
dbTrans = null;
}
}
#endregion
#region 工具
// OpenConnection 逻辑已合并到 Open() 方法中,因为 SqliteConnection 构造函数只接受字符串
public SqliteConnection Connection
{
get { return mConn; }
private set { mConn = value ?? throw new ArgumentNullException(); }
}
protected void EnsureConnection()
{
if (this.mConn == null)
{
throw new Exception("SQLiteManager.Connection=null");
}
if (mConn.State != ConnectionState.Open)
{
mConn.Open();
}
}
public string GetDataFile() => this.mDataFile;
public bool TableExists(string table)
{
if (string.IsNullOrEmpty(table)) return false;
EnsureConnection();
// 注意:参数化查询在表名/列名上无效,这里使用字符串拼接(需确保table变量安全)
// 或者使用 PRAGMA table_info(table_name)
var sql = $"SELECT count(*) FROM sqlite_master WHERE type='table' AND name='{table}'";
using (var cmd = new SqliteCommand(sql, Connection))
{
var result = cmd.ExecuteScalar();
return Convert.ToInt32(result) > 0;
}
}
public bool Vacuum()
{
try
{
using (var Command = new SqliteCommand("VACUUM", Connection))
{
Command.ExecuteNonQuery();
}
return true;
}
catch (Exception) // SqliteException
{
return false;
}
}
/// <summary>
/// 将 IDataReader 转换为 DataSet
/// </summary>
/// <param name="reader">已经执行了查询的 DataReader</param>
/// <returns>包含数据的 DataSet</returns>
public DataSet ReaderToDataSet(IDataReader reader)
{
DataSet ds = new DataSet();
do
{
DataTable schemaTable = reader.GetSchemaTable()!;
DataTable dataTable = new DataTable();
if (schemaTable != null)
{
// 创建列
foreach (DataRow schemaRow in schemaTable.Rows)
{
string colName = schemaRow["ColumnName"].ToString()!;
Type dataType = (Type)schemaRow["DataType"];
// 防止列名重复
string uniqueColName = colName;
int i = 1;
while (dataTable.Columns.Contains(uniqueColName))
{
uniqueColName = colName + i++;
}
DataColumn column = new DataColumn(uniqueColName, dataType);
dataTable.Columns.Add(column);
}
// 读取行
while (reader.Read())
{
object[] values = new object[dataTable.Columns.Count];
for (int i = 0; i < dataTable.Columns.Count; i++)
{
values[i] = reader.IsDBNull(i) ? DBNull.Value : reader.GetValue(i);
}
dataTable.Rows.Add(values);
}
}
else
{
// 如果没有 Schema (例如 "SELECT 1" 这种标量查询)
// 需要手动创建列
for (int i = 0; i < reader.FieldCount; i++)
{
dataTable.Columns.Add("Column" + i);
}
while (reader.Read())
{
object[] values = new object[reader.FieldCount];
reader.GetValues(values);
dataTable.Rows.Add(values);
}
}
ds.Tables.Add(dataTable);
} while (reader.NextResult()); // 处理多个结果集
return ds;
}
#endregion
#region 执行SQL (Reader & Scalar)
public SqliteDataReader? ExecuteReader(string sql, SqliteParameter[]? paramArr)
{
if (string.IsNullOrEmpty(sql)) throw new ArgumentNullException(nameof(sql));
EnsureConnection();
// 注意:using 语句会立即释放 Reader,导致无法读取
// 这里的 RWL 锁在 using 外部处理,或者调用方自己处理锁
// 这里简化逻辑,直接返回 Reader,调用方需自行管理连接状态
var cmd = new SqliteCommand(sql, Connection);
if (paramArr != null)
{
cmd.Parameters.AddRange(paramArr);
}
// CommandBehavior.CloseConnection 确保 Reader 关闭时连接也关闭
// 但这里我们管理连接,所以不加
try
{
// 注意:不要在这里 Dispose Command,否则 Reader 会失效
// 返回 Reader,由调用方在读取完毕后关闭
return cmd.ExecuteReader();
}
catch(Exception ex)
{
Console.WriteLine("[SQLITE_ExecuteReader:err]" + ex.ToString());
cmd?.Dispose();
return null;
}
}
public DataSet? ExecuteDataSet(string sql, SqliteParameter[]? paramArr)
{
var dat = ExecuteReader(sql, paramArr);
if (dat == null)
{
return null;
}
return ReaderToDataSet(dat);
}
// ExecuteScalar
public object? ExecuteScalar(string sql, SqliteParameter[]? paramArr)
{
if (string.IsNullOrEmpty(sql)) throw new ArgumentNullException(nameof(sql));
EnsureConnection();
using (RWL[lockName].Read())
{
using (var cmd = new SqliteCommand(sql, Connection))
{
if (paramArr != null)
{
cmd.Parameters.AddRange(paramArr);
}
try
{
return cmd.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine("[SQLITE_ExecuteScalar:err]" + ex.ToString());
return null;
}
}
}
}
// QueryOne (返回字典或对象)
public Dictionary<string, object?>? QueryOne(string table, string conditionCol, object conditionVal)
{
if (string.IsNullOrEmpty(table)) return null;
EnsureConnection();
string sql = $"SELECT * FROM [{table}]"; // 使用 [] 防止关键字冲突
var parameters = new List<SqliteParameter>();
if (!string.IsNullOrEmpty(conditionCol))
{
sql += $" WHERE [{conditionCol}] = @{conditionCol}";
parameters.Add(new SqliteParameter($"@{conditionCol}", conditionVal));
}
using (var cmd = new SqliteCommand(sql, Connection))
{
cmd.Parameters.AddRange(parameters.ToArray());
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
var dict = new Dictionary<string, object?>();
for (int i = 0; i < reader.FieldCount; i++)
{
var value = reader.GetValue(i);
if (value == DBNull.Value) value = null;
dict[reader.GetName(i)] = value;
}
return dict;
}
}
}
return null;
}
#endregion
#region 增 删 改
// 注意:参数前缀统一为 @
public int InsertData(string table, Dictionary<string, object> entity)
{
if (string.IsNullOrEmpty(table)) throw new ArgumentNullException(nameof(table));
EnsureConnection();
string sql = BuildInsert(table, entity);
var parameters = BuildParamArray(entity);
return ExecuteNonQuery(sql, parameters);
}
public int Update(string table, Dictionary<string, object> entity, string where, SqliteParameter[]? whereParams)
{
if (string.IsNullOrEmpty(table)) throw new ArgumentNullException(nameof(table));
EnsureConnection();
string sql = BuildUpdate(table, entity);
var parameters = BuildParamArray(entity);
if (!string.IsNullOrEmpty(where))
{
sql += " WHERE " + where;
if (whereParams != null)
{
var combined = new SqliteParameter[parameters.Length + whereParams.Length];
parameters.CopyTo(combined, 0);
whereParams.CopyTo(combined, parameters.Length);
parameters = combined;
}
}
return ExecuteNonQuery(sql, parameters);
}
public int Delete(string table, string where, SqliteParameter[]? whereParams)
{
if (string.IsNullOrEmpty(table)) return 0;
EnsureConnection();
string sql = $"DELETE FROM [{table}]";
if (!string.IsNullOrEmpty(where))
{
sql += " WHERE " + where;
}
return ExecuteNonQuery(sql, whereParams);
}
public int ExecuteNonQuery(string sql, SqliteParameter[]? paramArr)
{
if (string.IsNullOrEmpty(sql)) throw new ArgumentNullException(nameof(sql));
EnsureConnection();
using (RWL[lockName].Read()) // 注意:写操作通常建议用 Write 锁
{
using (var cmd = new SqliteCommand(sql, Connection))
{
if (paramArr != null)
{
cmd.Parameters.AddRange(paramArr);
}
try
{
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("[SQLITE_ExecuteScalar:err]" + ex.ToString());
return 0;
}
}
}
}
// 构建 SQL 和 参数的方法保持不变,仅泛型类型变更
private SqliteParameter[] BuildParamArray(Dictionary<string, object> entity)
{
var list = new List<SqliteParameter>();
foreach (string key in entity.Keys)
{
// 注意:参数名必须包含 @ 前缀
list.Add(new SqliteParameter($"@{key}", entity[key]));
}
return list.ToArray();
}
private string BuildInsert(string table, Dictionary<string, object> entity)
{
var buf = new StringBuilder();
buf.Append("INSERT INTO ").Append(table).Append(" (");
var cols = new List<string>();
var vals = new List<string>();
foreach (string key in entity.Keys)
{
cols.Add(key);
vals.Add($"@{key}");
}
buf.Append(string.Join(",", cols)).Append(") VALUES (");
buf.Append(string.Join(",", vals)).Append(")");
return buf.ToString();
}
private string BuildUpdate(string table, Dictionary<string, object> entity)
{
var buf = new StringBuilder();
buf.Append("UPDATE ").Append(table).Append(" SET ");
var sets = new List<string>();
foreach (string key in entity.Keys)
{
sets.Add($"{key} = @{key}");
}
buf.Append(string.Join(",", sets));
return buf.ToString();
}
// 辅助方法:DataRow 转 Dictionary (由于没有 DataTable,通常直接从 Reader 转)
// 这里保留原方法签名,但实际使用中可能需要调整
public Dictionary<string, object?> DataTableToDictionary(DataTable dataTable)
{
var result = new Dictionary<string, object?>();
if (dataTable.Rows.Count > 0)
{
var row = dataTable.Rows[0];
foreach (DataColumn column in dataTable.Columns)
{
var value = row[column];
if (value == DBNull.Value) value = null;
result[column.ColumnName] = value;
}
}
return result;
}
#endregion
}
}
+62
View File
@@ -0,0 +1,62 @@
using System;
using System.IO;
using System.Text;
using System.Windows; // WPF 的核心命名空间
using System.Windows.Controls; // WPF TextBox 所在的命名空间
using System.Windows.Threading; // 用于 Dispatcher
namespace SunlightAggregationManager.UserClass
{
public class ControlWriter : TextWriter
{
private readonly TextBox _textbox;
public ControlWriter(TextBox textbox)
{
_textbox = textbox;
}
public override void Write(char value) => AppendText(DateTime.Now + ": " + value.ToString());
public override void Write(string? value) => AppendText(DateTime.Now + ": " + value);
public override void WriteLine(string? value) => AppendText(DateTime.Now + ": "+value + Environment.NewLine);
private void AppendText(string text)
{
if (!_textbox.Dispatcher.CheckAccess())
{
_textbox.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => AppendText(text)));
return;
}
_textbox.AppendText(text);
_textbox.ScrollToEnd();
}
public override Encoding Encoding => Encoding.UTF8;
}
//全局日志管理器
public static class GlobalLogManager
{
private static ControlWriter? _currentWriter;
// 【核心功能】注册目标 TextBox
// 子页面加载时调用这个方法
public static void RegisterTarget(TextBox logBox)
{
// 创建新的 Writer
_currentWriter = new ControlWriter(logBox);
// 【关键】将 Console 的输出重定向到这个 Writer
// 这样 Console.WriteLine("...") 就会自动写入到 logBox 中
Console.SetOut(_currentWriter);
Console.WriteLine("Start");
}
//提供一个直接写入的方法(如果不使用 Console.WriteLine)
public static void Write(string message)
{
_currentWriter?.WriteLine(message);
}
}
}