using Dapper; using Dapper.Contrib; using Dapper.Contrib.Extensions; using NFine.Code; using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace NFine.Application.WMS { public class DapperData { } /// /// Dapper 帮助类 SQL Server /// public class MsSqlData { private static string connString = string.Empty; static MsSqlData() { connString = FromMd5(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString); // connString = DESEncrypt.Decrypt(connString); //if (ConStringDESEncrypt == "true") //{ // connString = DESEncrypt.Decrypt(connString); //} } /// /// MD5解密 /// /// /// public static string FromMd5(string str) { //return str; return Decrypt(str, "&%#@?,:*_"); } /// /// 解密 /// /// /// /// private static String Decrypt(String strText, String sDecrKey) { Byte[] byKey = { }; Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; Byte[] inputByteArray = new byte[strText.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return encoding.GetString(ms.ToArray()); } catch (Exception ex) { return ex.Message; } } /// /// 第一行第一列 /// /// /// /// /// /// public static object ExecuteScalar(string sql, object param = null) { try { using (var connection = new SqlConnection()) { connection.ConnectionString = connString; connection.Open(); var name = connection.ExecuteScalar(sql, param); return name; } } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 执行 /// /// /// /// public static int Execute(string sql, object param = null) { try { using (var connection = new SqlConnection()) { connection.ConnectionString = connString; connection.Open(); var name = connection.Execute(sql, param); return name; } } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 查询 /// /// 模型 /// 连接字符串 /// sql语句 /// List /// public static IEnumerable Query(string sql, object param = null) where T : class, new() { try { using (var connection = new SqlConnection()) { connection.ConnectionString = connString; connection.Open(); var list = connection.Query(sql, param); return list; } } catch (Exception ex) { throw new Exception(ex.Message); } } public static T Get(object num) where T : class { try { using (var connection = new SqlConnection()) { connection.ConnectionString = connString; connection.Open(); var invoice = connection.Get(num); return invoice; } } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 查询所有 /// /// /// /// public static IEnumerable GetAll() where T : class, new() { try { using (var connection = new SqlConnection(connString)) { connection.Open(); var invoices = connection.GetAll(); return invoices; } } catch (Exception ex) { throw ex; } } /// /// 新增 /// /// /// /// public static bool Insert(T model, SqlTransaction sqlTransaction = null) where T : class { try { using (var connection = new SqlConnection()) { connection.ConnectionString = connString; connection.Open(); var identity = connection.Insert(model, sqlTransaction); return true; } } catch (Exception ex) { throw ex; } } /// /// 新增 /// /// /// /// public static bool Insert(List list) where T : class { var isSuccess = true; using (var connection = new SqlConnection()) { connection.ConnectionString = connString; connection.Open(); using (var transaction = connection.BeginTransaction()) { try { var identity = connection.Insert>(list, transaction); isSuccess = true; transaction.Commit(); return isSuccess; } catch (Exception ex) { transaction.Rollback(); throw ex; } } } } /// /// 修改 /// /// /// /// public static bool Update(T model) where T : class { try { using (var connection = new SqlConnection()) { connection.ConnectionString = connString; connection.Open(); return connection.Update(model); } } catch (Exception ex) { throw ex; } } /// /// 修改 /// /// /// /// public static bool Update(List list) where T : class { var isSuccess = true; using (var connection = new SqlConnection()) { connection.ConnectionString = connString; connection.Open(); using (var transaction = connection.BeginTransaction()) { try { var identity = connection.Update>(list, transaction); isSuccess = true; transaction.Commit(); return isSuccess; } catch (Exception ex) { transaction.Rollback(); throw ex; } } } } /// /// 删除 /// /// 实体 /// /// public static bool Delete(T model) where T : class { try { using (var connection = new SqlConnection()) { connection.ConnectionString = connString; connection.Open(); return connection.Delete(model); } } catch (Exception ex) { throw ex; } } /// /// /// /// /// /// public static bool Delete(List list) { var isSuccess = true; using (var connection = new SqlConnection()) { connection.ConnectionString = connString; connection.Open(); using (var transaction = connection.BeginTransaction()) { try { isSuccess = connection.Delete>(list, transaction); if (isSuccess) { transaction.Commit(); return isSuccess; } else { throw new Exception("删除失败"); } } catch (Exception ex) { transaction.Rollback(); throw ex; } } } } } }