DbHelperMySQL.cs
using Dapper;
using Dapper.Contrib;
using Dapper.Contrib.Extensions;
using Nancal.Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Nancal.DBUtility
{
public class DBHelperMySQL
{
#region Property
//private static readonly string ConnectionString = ConfigurationManager.AppSettings["BaseDb"];
private static IDbConnection ReadingFunctionGetSQLDBConnection()
{
string key = "maindb";
var xmlStr = File.ReadAllText(@"C:\Program Files\Siemens\SimaticIT\Unified\bin\Worker.exe.config");
var str = XElement.Parse(xmlStr);
var connStrings = str.Elements("connectionStrings");
if (connStrings.Count() == 0)
{
throw new Exception($"未配置数据库链接字符串");
}
var db = connStrings.Elements().Where(t => t.Attribute("name").Value == key).FirstOrDefault();
if (db == null)
{
throw new Exception($"未找到数据库链接字符串");
}
var url = db.Attribute("connectionString");
if (url == null)
{
throw new Exception($"未找到数据库链接字符串");
}
var urlStr = url.Value;
if (string.IsNullOrWhiteSpace(urlStr))
{
throw new Exception($"未找到数据库链接字符串");
}
取接口地址
//var api = platform.Query<IApiEntity>().FirstOrDefault(t => t.Key == key);
//if (api == null || string.IsNullOrEmpty(api.ApiUrl))
//{
// throw new Exception($"地址{key}不存在");
//}
var connection = new SqlConnection(urlStr);
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
return connection;
}
#endregion
#region Method
#region 按数据集方式获得数据
/// <summary>
/// 获得Table数据
/// </summary>
/// <param name="sql"></param>
/// <param name="msg"></param>
/// <returns></returns>
public static DataTable GetData_Table(string sql, out string msg)
{
DateTime beforDT = DateTime.Now;
DataTable dt = new DataTable();
try
{
#region 1.数据合法性校验
msg = "获取数据成功";
if (string.IsNullOrEmpty(sql))
{
msg = "Sql语句为空!";
return dt;
}
#endregion
IDbConnection connection = ReadingFunctionGetSQLDBConnection();
var reader = connection.ExecuteReader(sql);
dt.Load(reader);
//LogProxy.Info("查询开始==========================================================");
//LogProxy.Info("查询时间:" + DateTime.Now.Subtract(beforDT).TotalMilliseconds.ToString() + " 毫秒");
//LogProxy.Info("GetData_Table()请求SQL:" + sql);
//LogProxy.Info("查询结束==========================================================");
return dt;
}
catch (Exception ex)
{
msg = ex.Message;
//LogProxy.Error("异常信息开始==========================================================");
//LogProxy.Error("查询时间:" + DateTime.Now.Subtract(beforDT).TotalMilliseconds.ToString() + " 毫秒");
//LogProxy.Error("GetData_Table()请求SQL:" + sql);
//LogProxy.Error("GetData_Table()异常信息:" + ex.Message);
//LogProxy.Error("异常信息结束==========================================================");
return dt;
}
}
/// <summary>
/// 获得数据集合
/// </summary>
/// <param name="sql"></param>
/// <param name="msg"></param>
/// <returns></returns>
public static DataSet GetDataSet(string sql, out string msg)
{
DateTime beforDT = DateTime.Now;
try
{
using (IDbConnection connection = ReadingFunctionGetSQLDBConnection())
{
msg = "数据获取成功!";
DataSet dataSet = new XDataSet();
IDataReader reader = connection.ExecuteReader(sql);
dataSet.Load(reader, LoadOption.OverwriteChanges, null, new DataTable[] { });
//LogProxy.Info("查询开始==========================================================");
//LogProxy.Info("查询时间:" + DateTime.Now.Subtract(beforDT).TotalMilliseconds.ToString() + " 毫秒");
//LogProxy.Info("GetDataSet()请求SQL:" + sql);
//LogProxy.Info("查询结束==========================================================");
return dataSet;
}
}
catch (Exception ex)
{
msg = ex.Message;
//LogProxy.Error("异常信息开始==========================================================");
//LogProxy.Error("查询时间:" + DateTime.Now.Subtract(beforDT).TotalMilliseconds.ToString() + " 毫秒");
//LogProxy.Error("GetDataSet()请求SQL:" + sql);
//LogProxy.Error("GetDataSet()异常信息:" + ex.Message);
//LogProxy.Error("异常信息结束==========================================================");
return null;
}
}
#endregion
#region 按实体类方式获得数据
/// <summary>
/// 通过主键获得实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <param name="msg"></param>
/// <returns></returns>
public static T GetDataById<T>(string id, out string msg) where T : class
{
DateTime beforDT = DateTime.Now;
try
{
#region 1.数据合法性校验
msg = "获取数据成功";
if (string.IsNullOrEmpty(id))
{
msg = "主键Id为空!";
return default(T);
}
#endregion
#region 2.获得数据
I