using Newtonsoft.Json;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Configuration;
|
using System.Linq;
|
using System.Linq.Expressions;
|
|
namespace Hanhe.iWCS.TaizhouGEMTwoProtocol
|
{
|
public class SqlHelper<T> where T : class, new()
|
{
|
public bool ExecuteSql(string sql) {
|
try {
|
var code = GetInstance().Ado.ExecuteCommand(sql);
|
return code > 1;
|
}
|
catch (Exception ex) {
|
return false;
|
}
|
}
|
|
public bool Update(T model, string[] cols) {
|
var db = GetInstance();
|
return db.Updateable<T>(model).UpdateColumns(cols).ExecuteCommand() > 0;
|
}
|
public List<T> GetList(Expression<Func<T, bool>> where = null) {
|
var 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>> orderBy = null) {
|
try {
|
var db = GetInstance();
|
T model = null;
|
if (orderBy != null) {
|
model = db.Queryable<T>().Where(where).OrderBy(orderBy, OrderByType.Desc).First();//查询表的所有
|
}
|
else {
|
model = db.Queryable<T>().Where(where).First();
|
}
|
return model;
|
}
|
catch (Exception ex) {
|
Console.WriteLine($"读取SQLSERVER数据库异常!异常:{ex}");
|
return default(T);
|
}
|
}
|
public bool Update(T model) {
|
var db = GetInstance();
|
return db.Updateable<T>(model).ExecuteCommand() > 0;
|
}
|
public bool Insert(T model) {
|
try {
|
var db = GetInstance();
|
return db.Insertable<T>(model).ExecuteCommand() > 0;
|
}
|
catch (Exception ex) {
|
Console.WriteLine("写入信息异常:" + ex.Message);
|
return false;
|
}
|
}
|
|
//删除指定条件数据
|
public bool Deleteable(Expression<Func<T, bool>> where) {
|
try {
|
var db = GetInstance();
|
return db.Deleteable<T>().Where(where).ExecuteCommand() > 0;
|
}
|
catch (Exception ex) {
|
return false;
|
}
|
}
|
|
|
//创建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 = "Data Source=192.168.1.199;Initial Catalog=JD_AMS;User ID=sa;Password=test?123;",//写死SqlServer
|
ConnectionString = Settings.SqlServer,//连接符字串
|
DbType = DbType.SqlServer,//DbType.MySql
|
IsAutoCloseConnection = true,
|
InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
|
});
|
|
//添加Sql打印事件,开发中可以删掉这个代码
|
//db.Aop.OnLogExecuting = (sql, pars) => {
|
// Console.WriteLine(db.CurrentConnectionConfig.ConnectionString);
|
// Console.WriteLine("sql"+sql+",pars"+ JsonConvert.SerializeObject(pars));
|
// //Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
// Console.WriteLine();
|
//};
|
return db;
|
}
|
else
|
{
|
//创建数据库对象
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
{
|
//ConnectionString = "Server=192.168.1.233;Database=ams;Uid=root;Pwd=123456;",//连接符字串
|
//ConnectionString = "Data Source=192.168.1.199;Initial Catalog=JD_AMS;User ID=sa;Password=test?123;",//写死SqlServer
|
ConnectionString = Settings.SqlServer1,//连接符字串
|
DbType = DbType.SqlServer,//DbType.MySql
|
IsAutoCloseConnection = true,
|
InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
|
});
|
|
//添加Sql打印事件,开发中可以删掉这个代码
|
//db.Aop.OnLogExecuting = (sql, pars) => {
|
// Console.WriteLine(db.CurrentConnectionConfig.ConnectionString);
|
// Console.WriteLine("sql"+sql+",pars"+ JsonConvert.SerializeObject(pars));
|
// //Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
// Console.WriteLine();
|
//};
|
return db;
|
}
|
|
}
|
//实体与数据库结构一样
|
public static string AMS_MSSQL = ConfigurationManager.ConnectionStrings["AMS_MSSQL"].ToString();
|
}
|
|
public class SqlTwoHelper<T> where T : class, new()
|
{
|
public bool ExecuteSql(string sql)
|
{
|
try
|
{
|
var code = GetInstance().Ado.ExecuteCommand(sql);
|
return code > 1;
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine(ex.Message);
|
return false;
|
}
|
}
|
|
public bool Update(T model, string[] cols)
|
{
|
var db = GetInstance();
|
return db.Updateable<T>(model).UpdateColumns(cols).ExecuteCommand() > 0;
|
}
|
public List<T> GetList(Expression<Func<T, bool>> where = null)
|
{
|
var 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 T Get(Expression<Func<T, bool>> where, Expression<Func<T, object>> orderBy = null)
|
{
|
try
|
{
|
var db = GetInstance();
|
T model = null;
|
if (orderBy != null)
|
{
|
model = db.Queryable<T>().Where(where).OrderBy(orderBy, OrderByType.Desc).First();//查询表的所有
|
}
|
else
|
{
|
model = db.Queryable<T>().Where(where).First();
|
}
|
return model;
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine(ex.Message);
|
return default(T);
|
}
|
}
|
public bool Update(T model)
|
{
|
var db = GetInstance();
|
return db.Updateable<T>(model).ExecuteCommand() > 0;
|
}
|
public bool Insert(T model)
|
{
|
try
|
{
|
var db = GetInstance();
|
return db.Insertable<T>(model).ExecuteCommand() > 0;
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine(ex.Message);
|
return false;
|
}
|
}
|
|
//删除指定条件数据
|
public bool Deleteable(Expression<Func<T, bool>> where)
|
{
|
try
|
{
|
var db = GetInstance();
|
return db.Deleteable<T>().Where(where).ExecuteCommand() > 0;
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine(ex.Message);
|
return false;
|
}
|
}
|
|
public bool CreateTable()
|
{
|
bool result = false;
|
var db = GetInstance();
|
|
//定义SQL语句
|
//string sql = "create table student" + "(num varchar(50),name varchar(255),age int)";
|
|
try
|
{
|
|
// 创建数据库
|
//db.Context.DbMaintenance.CreateDatabase();
|
// 创建表
|
db.Context.CodeFirst.InitTables(
|
typeof(T)
|
);
|
//Console.WriteLine("数据表创建成功。");
|
}
|
catch (Exception ae)
|
{
|
Console.WriteLine("数据表创建失败。" + ae.Message.ToString());
|
}
|
finally
|
{
|
//db.Close();//对数据库操作完成后,需要关闭数据库,释放内存
|
}
|
return result;
|
}
|
|
|
//创建SqlSugarClient
|
public SqlSugarClient GetInstance()
|
{
|
//创建数据库对象
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
{
|
ConnectionString = Settings.SqlServer,
|
DbType = DbType.SqlServer,
|
IsAutoCloseConnection = true,
|
InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
|
});
|
//db.MappingTables.Add("","");
|
|
//添加Sql打印事件,开发中可以删掉这个代码
|
//db.Aop.OnLogExecuting = (sql, pars) => {
|
// Console.WriteLine(db.CurrentConnectionConfig.ConnectionString);
|
// Console.WriteLine("sql"+sql+",pars"+ JsonConvert.SerializeObject(pars));
|
// //Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
// Console.WriteLine();
|
//};
|
|
return db;
|
}
|
}
|
|
public class AMSSqlHelper
|
{
|
|
public bool ExecuteSql(string sql) {
|
try {
|
var code = GetInstance().Ado.ExecuteCommand(sql);
|
return code > 1;
|
}
|
catch (Exception ex) {
|
return false;
|
}
|
}
|
|
private SqlSugarClient GetInstance() {
|
//创建数据库对象
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() {
|
//ConnectionString = "Server=192.168.1.233;Database=ams;Uid=root;Pwd=123456;",//连接符字串
|
ConnectionString = AMS_MSSQL,//连接符字串
|
DbType = DbType.SqlServer,//DbType.MySql
|
IsAutoCloseConnection = true,
|
InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
|
});
|
|
//添加Sql打印事件,开发中可以删掉这个代码
|
//db.Aop.OnLogExecuting = (sql, pars) => {
|
// Console.WriteLine(db.CurrentConnectionConfig.ConnectionString);
|
// Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
// Console.WriteLine();
|
//};
|
return db;
|
}
|
//实体与数据库结构一样
|
public static string AMS_MSSQL = ConfigurationManager.ConnectionStrings["AMS_MSSQL"].ToString();
|
}
|
}
|