ams
cjs
2025-05-28 1fb5bbbd57fcc62e5dfb38a058a0aeca0118d643
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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();
 
 
 
    }
}