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.

379 lines
11 KiB

3 weeks ago
  1. using Dapper;
  2. using Dapper.Contrib;
  3. using Dapper.Contrib.Extensions;
  4. using NFine.Code;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Data.SqlClient;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace NFine.Application.WMS
  16. {
  17. public class DapperData
  18. {
  19. }
  20. /// <summary>
  21. /// Dapper 帮助类 SQL Server
  22. /// </summary>
  23. public class MsSqlData
  24. {
  25. private static string connString = string.Empty;
  26. static MsSqlData()
  27. {
  28. connString = FromMd5(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
  29. // connString = DESEncrypt.Decrypt(connString);
  30. //if (ConStringDESEncrypt == "true")
  31. //{
  32. // connString = DESEncrypt.Decrypt(connString);
  33. //}
  34. }
  35. /// <summary>
  36. /// MD5解密
  37. /// </summary>
  38. /// <param name="str"></param>
  39. /// <returns></returns>
  40. public static string FromMd5(string str)
  41. {
  42. //return str;
  43. return Decrypt(str, "&%#@?,:*_");
  44. }
  45. /// <summary>
  46. /// 解密
  47. /// </summary>
  48. /// <param name="strText"></param>
  49. /// <param name="sDecrKey"></param>
  50. /// <returns></returns>
  51. private static String Decrypt(String strText, String sDecrKey)
  52. {
  53. Byte[] byKey = { };
  54. Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  55. Byte[] inputByteArray = new byte[strText.Length];
  56. try
  57. {
  58. byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
  59. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  60. inputByteArray = Convert.FromBase64String(strText);
  61. MemoryStream ms = new MemoryStream();
  62. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV),
  63. CryptoStreamMode.Write);
  64. cs.Write(inputByteArray, 0, inputByteArray.Length);
  65. cs.FlushFinalBlock();
  66. System.Text.Encoding encoding = System.Text.Encoding.UTF8;
  67. return encoding.GetString(ms.ToArray());
  68. }
  69. catch (Exception ex)
  70. {
  71. return ex.Message;
  72. }
  73. }
  74. /// <summary>
  75. /// 第一行第一列
  76. /// </summary>
  77. /// <param name="connString"></param>
  78. /// <param name="sql"></param>
  79. /// <param name="param"></param>
  80. /// <returns></returns>
  81. /// <exception cref="Exception"></exception>
  82. public static object ExecuteScalar(string sql, object param = null)
  83. {
  84. try
  85. {
  86. using (var connection = new SqlConnection())
  87. {
  88. connection.ConnectionString = connString;
  89. connection.Open();
  90. var name = connection.ExecuteScalar<object>(sql, param);
  91. return name;
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. throw new Exception(ex.Message);
  97. }
  98. }
  99. /// <summary>
  100. /// 执行
  101. /// </summary>
  102. /// <param name="sql"></param>
  103. /// <param name="param"></param>
  104. /// <returns></returns>
  105. public static int Execute(string sql, object param = null)
  106. {
  107. try
  108. {
  109. using (var connection = new SqlConnection())
  110. {
  111. connection.ConnectionString = connString;
  112. connection.Open();
  113. var name = connection.Execute(sql, param);
  114. return name;
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. throw new Exception(ex.Message);
  120. }
  121. }
  122. /// <summary>
  123. /// 查询
  124. /// </summary>
  125. /// <typeparam name="T">模型</typeparam>
  126. /// <param name="connString">连接字符串</param>
  127. /// <param name="sql">sql语句</param>
  128. /// <returns>List<T></returns>
  129. /// <exception cref="Exception"></exception>
  130. public static IEnumerable<T> Query<T>(string sql, object param = null) where T : class, new()
  131. {
  132. try
  133. {
  134. using (var connection = new SqlConnection())
  135. {
  136. connection.ConnectionString = connString;
  137. connection.Open();
  138. var list = connection.Query<T>(sql, param);
  139. return list;
  140. }
  141. }
  142. catch (Exception ex)
  143. {
  144. throw new Exception(ex.Message);
  145. }
  146. }
  147. public static T Get<T>(object num) where T : class
  148. {
  149. try
  150. {
  151. using (var connection = new SqlConnection())
  152. {
  153. connection.ConnectionString = connString;
  154. connection.Open();
  155. var invoice = connection.Get<T>(num);
  156. return invoice;
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. throw new Exception(ex.Message);
  162. }
  163. }
  164. /// <summary>
  165. /// 查询所有
  166. /// </summary>
  167. /// <typeparam name="T"></typeparam>
  168. /// <param name="connString"></param>
  169. /// <returns></returns>
  170. public static IEnumerable<T> GetAll<T>() where T : class, new()
  171. {
  172. try
  173. {
  174. using (var connection = new SqlConnection(connString))
  175. {
  176. connection.Open();
  177. var invoices = connection.GetAll<T>();
  178. return invoices;
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. throw ex;
  184. }
  185. }
  186. /// <summary>
  187. /// 新增
  188. /// </summary>
  189. /// <typeparam name="T"></typeparam>
  190. /// <param name="model"></param>
  191. /// <returns></returns>
  192. public static bool Insert<T>(T model, SqlTransaction sqlTransaction = null) where T : class
  193. {
  194. try
  195. {
  196. using (var connection = new SqlConnection())
  197. {
  198. connection.ConnectionString = connString;
  199. connection.Open();
  200. var identity = connection.Insert<T>(model, sqlTransaction);
  201. return true;
  202. }
  203. }
  204. catch (Exception ex)
  205. {
  206. throw ex;
  207. }
  208. }
  209. /// <summary>
  210. /// 新增
  211. /// </summary>
  212. /// <typeparam name="T"></typeparam>
  213. /// <param name="list"></param>
  214. /// <returns></returns>
  215. public static bool Insert<T>(List<T> list) where T : class
  216. {
  217. var isSuccess = true;
  218. using (var connection = new SqlConnection())
  219. {
  220. connection.ConnectionString = connString;
  221. connection.Open();
  222. using (var transaction = connection.BeginTransaction())
  223. {
  224. try
  225. {
  226. var identity = connection.Insert<List<T>>(list, transaction);
  227. isSuccess = true;
  228. transaction.Commit();
  229. return isSuccess;
  230. }
  231. catch (Exception ex)
  232. {
  233. transaction.Rollback();
  234. throw ex;
  235. }
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// 修改
  241. /// </summary>
  242. /// <typeparam name="T"></typeparam>
  243. /// <param name="model"></param>
  244. /// <returns></returns>
  245. public static bool Update<T>(T model) where T : class
  246. {
  247. try
  248. {
  249. using (var connection = new SqlConnection())
  250. {
  251. connection.ConnectionString = connString;
  252. connection.Open();
  253. return connection.Update<T>(model);
  254. }
  255. }
  256. catch (Exception ex)
  257. {
  258. throw ex;
  259. }
  260. }
  261. /// <summary>
  262. /// 修改
  263. /// </summary>
  264. /// <typeparam name="T"></typeparam>
  265. /// <param name="model"></param>
  266. /// <returns></returns>
  267. public static bool Update<T>(List<T> list) where T : class
  268. {
  269. var isSuccess = true;
  270. using (var connection = new SqlConnection())
  271. {
  272. connection.ConnectionString = connString;
  273. connection.Open();
  274. using (var transaction = connection.BeginTransaction())
  275. {
  276. try
  277. {
  278. var identity = connection.Update<List<T>>(list, transaction);
  279. isSuccess = true;
  280. transaction.Commit();
  281. return isSuccess;
  282. }
  283. catch (Exception ex)
  284. {
  285. transaction.Rollback();
  286. throw ex;
  287. }
  288. }
  289. }
  290. }
  291. /// <summary>
  292. /// 删除
  293. /// </summary>
  294. /// <typeparam name="T">实体</typeparam>
  295. /// <param name="model"></param>
  296. /// <returns></returns>
  297. public static bool Delete<T>(T model) where T : class
  298. {
  299. try
  300. {
  301. using (var connection = new SqlConnection())
  302. {
  303. connection.ConnectionString = connString;
  304. connection.Open();
  305. return connection.Delete<T>(model);
  306. }
  307. }
  308. catch (Exception ex)
  309. {
  310. throw ex;
  311. }
  312. }
  313. /// <summary>
  314. ///
  315. /// </summary>
  316. /// <typeparam name="T"></typeparam>
  317. /// <param name="list"></param>
  318. /// <returns></returns>
  319. public static bool Delete<T>(List<T> list)
  320. {
  321. var isSuccess = true;
  322. using (var connection = new SqlConnection())
  323. {
  324. connection.ConnectionString = connString;
  325. connection.Open();
  326. using (var transaction = connection.BeginTransaction())
  327. {
  328. try
  329. {
  330. isSuccess = connection.Delete<List<T>>(list, transaction);
  331. if (isSuccess)
  332. {
  333. transaction.Commit();
  334. return isSuccess;
  335. }
  336. else
  337. {
  338. throw new Exception("删除失败");
  339. }
  340. }
  341. catch (Exception ex)
  342. {
  343. transaction.Rollback();
  344. throw ex;
  345. }
  346. }
  347. }
  348. }
  349. }
  350. }