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
379 lines
11 KiB
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.BBWMS
|
|
{
|
|
public class DapperData
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dapper 帮助类 SQL Server
|
|
/// </summary>
|
|
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);
|
|
//}
|
|
}
|
|
|
|
/// <summary>
|
|
/// MD5解密
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static string FromMd5(string str)
|
|
{
|
|
//return str;
|
|
return Decrypt(str, "&%#@?,:*_");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解密
|
|
/// </summary>
|
|
/// <param name="strText"></param>
|
|
/// <param name="sDecrKey"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 第一行第一列
|
|
/// </summary>
|
|
/// <param name="connString"></param>
|
|
/// <param name="sql"></param>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public static object ExecuteScalar(string sql, object param = null)
|
|
{
|
|
try
|
|
{
|
|
using (var connection = new SqlConnection())
|
|
{
|
|
connection.ConnectionString = connString;
|
|
connection.Open();
|
|
|
|
var name = connection.ExecuteScalar<object>(sql, param);
|
|
|
|
return name;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行
|
|
/// </summary>
|
|
/// <param name="sql"></param>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询
|
|
/// </summary>
|
|
/// <typeparam name="T">模型</typeparam>
|
|
/// <param name="connString">连接字符串</param>
|
|
/// <param name="sql">sql语句</param>
|
|
/// <returns>List<T></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public static IEnumerable<T> Query<T>(string sql, object param = null) where T : class, new()
|
|
{
|
|
try
|
|
{
|
|
using (var connection = new SqlConnection())
|
|
{
|
|
connection.ConnectionString = connString;
|
|
connection.Open();
|
|
var list = connection.Query<T>(sql, param);
|
|
return list;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
|
|
public static T Get<T>(object num) where T : class
|
|
{
|
|
try
|
|
{
|
|
using (var connection = new SqlConnection())
|
|
{
|
|
connection.ConnectionString = connString;
|
|
connection.Open();
|
|
|
|
var invoice = connection.Get<T>(num);
|
|
return invoice;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询所有
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="connString"></param>
|
|
/// <returns></returns>
|
|
public static IEnumerable<T> GetAll<T>() where T : class, new()
|
|
{
|
|
try
|
|
{
|
|
using (var connection = new SqlConnection(connString))
|
|
{
|
|
connection.Open();
|
|
|
|
var invoices = connection.GetAll<T>();
|
|
return invoices;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public static bool Insert<T>(T model, SqlTransaction sqlTransaction = null) where T : class
|
|
{
|
|
try
|
|
{
|
|
using (var connection = new SqlConnection())
|
|
{
|
|
connection.ConnectionString = connString;
|
|
connection.Open();
|
|
var identity = connection.Insert<T>(model, sqlTransaction);
|
|
return true;
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="list"></param>
|
|
/// <returns></returns>
|
|
public static bool Insert<T>(List<T> 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<T>>(list, transaction);
|
|
isSuccess = true;
|
|
|
|
transaction.Commit();
|
|
return isSuccess;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public static bool Update<T>(T model) where T : class
|
|
{
|
|
try
|
|
{
|
|
using (var connection = new SqlConnection())
|
|
{
|
|
connection.ConnectionString = connString;
|
|
connection.Open();
|
|
return connection.Update<T>(model);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public static bool Update<T>(List<T> 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<T>>(list, transaction);
|
|
isSuccess = true;
|
|
|
|
transaction.Commit();
|
|
return isSuccess;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除
|
|
/// </summary>
|
|
/// <typeparam name="T">实体</typeparam>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public static bool Delete<T>(T model) where T : class
|
|
{
|
|
try
|
|
{
|
|
using (var connection = new SqlConnection())
|
|
{
|
|
connection.ConnectionString = connString;
|
|
connection.Open();
|
|
return connection.Delete<T>(model);
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="list"></param>
|
|
/// <returns></returns>
|
|
public static bool Delete<T>(List<T> list)
|
|
{
|
|
var isSuccess = true;
|
|
using (var connection = new SqlConnection())
|
|
{
|
|
connection.ConnectionString = connString;
|
|
connection.Open();
|
|
using (var transaction = connection.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
isSuccess = connection.Delete<List<T>>(list, transaction);
|
|
if (isSuccess)
|
|
{
|
|
transaction.Commit();
|
|
return isSuccess;
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("删除失败");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|