wangxinhui
2024-09-23 a3eb67538c4716aef9967f1e6301720cce095e3c
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
using FreeSql;
using System.Collections.Generic;
using System.IO;
 
namespace WIDESEA_Common.DBHelper
{
    public class FreeDB
    {
        private static IFreeSql fsql;
        public static IFreeSql DB
        {
            get
            {
                if (fsql == null)
                {
                    string coonStr = string.Empty;
                    var configTxt = File.ReadAllText(Directory.GetCurrentDirectory() + "/appsettings.json");
                    var res = System.Text.RegularExpressions.Regex.Match(configTxt, "\"DbConnectionString\".*?:.*?\"(.*?)\"");
                    if (res.Success)
                    {
                        coonStr = res.Groups[1].Value;
                    }
                    fsql = new FreeSql.FreeSqlBuilder()
                        //.UseNoneCommandParameter(true)
                        //.UseMonitorCommand(cmd =>
                        //{
                        //    File.AppendAllText("sql.txt", cmd.CommandText + "\n\n");
                        //})                        //.UseNoneCommandParameter(true)
                        //.UseMonitorCommand(cmd =>
                        //{
                        //    File.AppendAllText("sql.txt", cmd.CommandText + "\n\n");
                        //})
                        .UseConnectionString(FreeSql.DataType.SqlServer, coonStr)
                        .Build();
                }
                return fsql;
            }
            set { }
        }
 
        public IFreeSql DataBase = DB;
 
        //返回数据库结果
        public int Remove<Entity>(Entity entity) where Entity : class
        {
            return DB.Delete<Entity>().Where(entity).ExecuteAffrows();
        }
 
        //返回数据库结果
        public int Add<Entity>(Entity entity) where Entity : class
        {
            return DB.Insert(entity).ExecuteAffrows();
        }
 
        //返回数据库结果
        public int Add<Entity>(List<Entity> list) where Entity : class
        {
            return DB.Insert(list).ExecuteAffrows();
        }
 
        //拓展
        public ISelect<Entity> Select<Entity>() where Entity : class
        {
            return DB.Select<Entity>();
        }
 
        //拓展
        public IUpdate<Entity> Update<Entity>(Entity entity) where Entity : class
        {
            return DB.Update<Entity>().SetSource(entity);
        }
 
        //关闭
        public static void Dispose()
        {
            fsql?.Dispose();
        }
    }
}