You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
3.2 KiB
103 lines
3.2 KiB
using System.Data; |
|
using System.Reflection; |
|
using System.Security.Cryptography; |
|
using System.Text; |
|
using Newtonsoft.Json; |
|
|
|
namespace SparkClient.Model.Helper; |
|
|
|
public class Common |
|
{ |
|
|
|
public const string DatabasePwd = "DyumL4FvF6wVHWw"; |
|
|
|
public const string AesKey = "JbI36Snd3s+QjlOcz5dytgkL5W6OnFEV5ZNRg/OYp58="; |
|
|
|
public const string AesIV = "QRYL9P9NcV9Uny29DqEBqg=="; |
|
|
|
public static readonly string BasePath = AppDomain.CurrentDomain.BaseDirectory; |
|
|
|
public const string DataBaseFileName = "SparkDB.db"; |
|
public const string DataBaseTempFileName = "SparkDBtemp.db"; |
|
|
|
public const string DataBaseName = "SparkDB"; |
|
|
|
/// <summary> |
|
/// 将 DataTable 转换为 JSON 字符串 |
|
/// </summary> |
|
/// <param name="table">要转换的 DataTable</param> |
|
/// <returns>JSON 字符串</returns> |
|
public static string DataTableToJson(DataTable table) |
|
{ |
|
if (table == null) |
|
{ |
|
throw new ArgumentNullException(nameof(table), "DataTable cannot be null."); |
|
} |
|
|
|
return JsonConvert.SerializeObject(table, Formatting.Indented); |
|
} |
|
|
|
/// <summary> |
|
/// 将 JSON 字符串转换为 DataTable |
|
/// </summary> |
|
/// <param name="json">要转换的 JSON 字符串</param> |
|
/// <returns>还原的 DataTable</returns> |
|
public static DataTable JsonToDataTable(string json) |
|
{ |
|
if (string.IsNullOrEmpty(json)) |
|
{ |
|
throw new ArgumentNullException(nameof(json), "JSON string cannot be null or empty."); |
|
} |
|
|
|
return JsonConvert.DeserializeObject<DataTable>(json); |
|
} |
|
|
|
/// <summary> |
|
/// 将 DataTable 转换为实体对象列表 |
|
/// </summary> |
|
/// <typeparam name="T">目标实体类型</typeparam> |
|
/// <param name="table">要转换的 DataTable</param> |
|
/// <returns>实体对象列表</returns> |
|
public static List<T> ConvertToEntityList<T>(DataTable table) where T : new() |
|
{ |
|
if (table == null) |
|
{ |
|
throw new ArgumentNullException(nameof(table), "DataTable cannot be null."); |
|
} |
|
|
|
var entityList = new List<T>(); |
|
|
|
// 获取实体类型的所有属性 |
|
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); |
|
|
|
foreach (DataRow row in table.Rows) |
|
{ |
|
// 创建实体对象 |
|
var entity = new T(); |
|
|
|
foreach (var property in properties) |
|
{ |
|
// 检查 DataTable 是否包含与属性名匹配的列 |
|
if (table.Columns.Contains(property.Name) && row[property.Name] != DBNull.Value) |
|
{ |
|
// 设置实体属性值 |
|
property.SetValue(entity, Convert.ChangeType(row[property.Name], property.PropertyType)); |
|
} |
|
} |
|
|
|
entityList.Add(entity); |
|
} |
|
|
|
return entityList; |
|
} |
|
|
|
public static string GenerateMd5Hash(string input) |
|
{ |
|
using (var md5 = MD5.Create()) |
|
{ |
|
var inputBytes = Encoding.UTF8.GetBytes(input); |
|
var hashBytes = md5.ComputeHash(inputBytes); |
|
return string.Concat(hashBytes.Select(b => b.ToString("X2"))); |
|
} |
|
} |
|
} |