using Hanhe.iWCS.Common;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Configuration;
|
using System.Linq;
|
using System.Linq.Expressions;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace Hanhe.iWCS.IndonesiaGLMProtocol
|
{
|
public class SqlHelper<T> where T : class, new()
|
{
|
public bool ExecuteSql(string sql,bool result = true) {
|
try {
|
var code = GetInstance(result).Ado.ExecuteCommand(sql);
|
CMMLog.Info($"code:{code}");
|
return code >= 1;
|
}
|
catch (Exception ex) {
|
CMMLog.Info($"更新数据库失败,错误原因可能是:{ex.Message}");
|
return false;
|
}
|
}
|
public List<T> GetList(Expression<Func<T, bool>> where = null) {
|
SqlSugarClient db = GetInstance();
|
if (where == null) {
|
return db.Queryable<T>().ToList();
|
}
|
else {
|
return db.Queryable<T>().Where(where).ToList();
|
}
|
}
|
|
public T Get(Expression<Func<T, bool>> where, Expression<Func<T, object>> order, bool asc = false) {
|
SqlSugarClient db = GetInstance();
|
if (order == null) {
|
|
return db.Queryable<T>().Where(where).Single();
|
}
|
else {
|
return db.Queryable<T>().Where(where).OrderBy(order, asc ? OrderByType.Asc : OrderByType.Desc).First();
|
}
|
}
|
public bool Update(T model) {
|
SqlSugarClient db = GetInstance();
|
return db.Updateable<T>(model).ExecuteCommand() > 0;
|
}
|
|
//删除指定条件数据
|
public bool Deleteable(Expression<Func<T, bool>> where) {
|
SqlSugarClient db = GetInstance();
|
return db.Deleteable<T>().Where(where).ExecuteCommand() > 0;
|
}
|
|
|
//创建SqlSugarClient
|
public SqlSugarClient GetInstance(bool action = true) {
|
if (action)
|
{
|
//创建数据库对象
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
{
|
//ConnectionString = "Server=192.168.1.233;Database=ams;Uid=root;Pwd=123456;",//连接符字串
|
ConnectionString = Settings.SqlServer,//连接符字串
|
DbType = DbType.SqlServer,//DbType.MySql
|
IsAutoCloseConnection = true,
|
InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
|
});
|
|
//添加Sql打印事件,开发中可以删掉这个代码
|
db.Aop.OnLogExecuting = (sql, pars) => {
|
//Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
};
|
return db;
|
}
|
else
|
{
|
//创建数据库对象
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
{
|
//ConnectionString = "Server=192.168.1.233;Database=ams;Uid=root;Pwd=123456;",//连接符字串
|
ConnectionString = Settings.SqlServer1,//连接符字串
|
DbType = DbType.SqlServer,//DbType.MySql
|
IsAutoCloseConnection = true,
|
InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
|
});
|
|
//添加Sql打印事件,开发中可以删掉这个代码
|
db.Aop.OnLogExecuting = (sql, pars) => {
|
//Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
};
|
return db;
|
}
|
|
}
|
|
|
public static string AMS_MSSQL = ConfigurationManager.ConnectionStrings["AMS_MSSQL"].ToString();
|
|
|
|
}
|
}
|