using System.Collections.Generic;
using System;
using System.Diagnostics;
using SqlSugar;
using System.Linq.Expressions;
using InterfaceSimulator.Utility;
public sealed class DataBaseHelper
{
public SqlSugarClient db;
public DataBaseHelper()
{
db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlConnString"].ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true
});
db.Aop.OnLogExecuted = (sql, pra) =>
{
Console.WriteLine($"sql语句:{sql}");
if (pra.Length > 0)
{
string sqlcmd = this.GetExecMsSqlCommandText(sql, pra);
}
else
{
string sqlcmd = this.GetExecMsSqlCommandText(sql);
}
};
}
public List
{
List
Result = db.Queryable
return Result;
}
public List
{
List
Result = db.Queryable
return Result;
}
public List
{
List
Result = db.Queryable
}
public int Add
{
int result = -1;
result = db.Insertable(info).ExecuteCommand();
return result;
}
public int Delete
{
int result = -1;
result = db.Deleteable
return result;
}
public int Delete
{
int result = -1;
result = db.Deleteable
return result;
}
public int Update
{
int result = -1;
result = db.Updateable
return result;
}
///
/// 通过sqlcommand 获取sql语句
///
///
///
private string GetExecMsSqlCommandText(string sql, SugarParameter[] param = null)
{
string sqlCmd = sql;
if (param != null)
{
foreach (var p in param)
{
string name = p.ParameterName;
object value = p.Value;
string repValue = "Null";
if (p.Value == DBNull.Value)
{ }
else
{
if ((p.DbType == System.Data.DbType.Int32) || (p.DbType == System.Data.DbType.Int64))
{
repValue = value.ToString();
}
else if (p.DbType == System.Data.DbType.String)
{
repValue = string.Format("'{0}'", value.ToString());
}
else if (p.DbType == System.Data.DbType.DateTime)
{
repValue = string.Format("'{0}'", ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"));
}
}
sqlCmd = sqlCmd.Replace(name, repValue);
}
}
Logger.Debug("exec sql:" + sql);
return sqlCmd;
}
}