Browse Source

数据库连接

Linshuk 3 months ago
parent
commit
49290c1111
5 changed files with 103 additions and 8 deletions
  1. 51 0
      DAL/BaseService.cs
  2. 30 0
      DAL/DB.cs
  3. 1 1
      Scada/Page/PageTotalEquipmentControl.Designer.cs
  4. 15 1
      Scada/Program.cs
  5. 6 6
      Scada/appsettings.json

+ 51 - 0
DAL/BaseService.cs

@@ -0,0 +1,51 @@
+using Model;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DAL
+{
+    public class BaseService<T> where T : BaseEntity,  new()
+    {
+        public virtual async Task<int> InsertAsync(T model)
+        {
+            var runsql =  DB.SqlSugarClient.Insertable(model);
+            var rows = await runsql.ExecuteCommandAsync();
+            return rows;
+        }
+        public virtual async Task<bool> UpdateAsync(T model)
+        {
+            var runsql = DB.SqlSugarClient.Updateable(model);
+            var rows = await runsql.ExecuteCommandAsync();
+            return rows>0;
+        }
+
+        public virtual async Task<bool> DeleteAsync(T model)
+        {
+            var runsql = DB.SqlSugarClient.Deleteable(model);
+            var rows = await runsql.ExecuteCommandAsync();
+            return rows > 0;
+        }
+        
+        public virtual async Task<T> GetByOneAsync(Expression<Func<T,bool>> where)
+        {
+            var runsql = DB.SqlSugarClient.Queryable<T>().Where(where);
+            return await runsql.FirstAsync();
+        }
+
+        public virtual async Task<List<T>> GetListAsync(Expression<Func<T, bool>> where)
+        {
+            var runsql = DB.SqlSugarClient.Queryable<T>().Where(where);
+            return await runsql.ToListAsync();
+        }
+        public virtual async Task<bool> ExistAsync(Expression<Func<T, bool>> where)
+        {
+            return await DB.SqlSugarClient.Queryable<T>()
+                .Where(where)
+                .AnyAsync();
+        }
+    }
+}

+ 30 - 0
DAL/DB.cs

@@ -0,0 +1,30 @@
+using Microsoft.Extensions.DependencyInjection;
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DAL
+{
+    public static class DB
+    {
+        public static ISqlSugarClient SqlSugarClient { get; set; }
+        public static void AddSqlSugarSetup(this IServiceCollection services, SqlSugar.DbType dataType, string conn)
+        {
+            if (services == null) throw new ArgumentNullException(nameof(services));
+
+            services.AddSingleton<ISqlSugarClient>(s =>
+            {
+                SqlSugarClient = new SqlSugarScope(new ConnectionConfig()
+                {
+                    DbType = dataType,
+                    ConnectionString = conn,
+                    IsAutoCloseConnection = true
+                });
+                return SqlSugarClient;
+            });
+        }
+    }
+}

+ 1 - 1
Scada/Page/PageTotalEquipmentControl.Designer.cs

@@ -85,7 +85,7 @@
             btn_DryRun.Text = null;
             btn_DryRun.Text = null;
             btn_DryRun.TextAlignment = ContentAlignment.MiddleCenter;
             btn_DryRun.TextAlignment = ContentAlignment.MiddleCenter;
             btn_DryRun.VariableName = "空运行";
             btn_DryRun.VariableName = "空运行";
-            btn_DryRun.ClickEvent += btn_DryRun_ClickEvent;
+            btn_DryRun.ClickEvent += this.btn_DryRun_ClickEvent;
             // 
             // 
             // btn_AlarmReset
             // btn_AlarmReset
             // 
             // 

+ 15 - 1
Scada/Program.cs

@@ -1,10 +1,13 @@
 using System.Reflection;
 using System.Reflection;
+using DAL;
 using HZY.Framework.DependencyInjection;
 using HZY.Framework.DependencyInjection;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Logging;
 using Microsoft.Extensions.Logging;
+using Model;
 using NLog;
 using NLog;
 using NLog.Extensions.Logging;
 using NLog.Extensions.Logging;
+using SqlSugar;
 
 
 namespace Scada
 namespace Scada
 {
 {
@@ -24,8 +27,14 @@ namespace Scada
 
 
             var serviceProvider = services.BuildServiceProvider();
             var serviceProvider = services.BuildServiceProvider();
             Globals.ServiceProvider = serviceProvider;
             Globals.ServiceProvider = serviceProvider;
+
             var frmMain = serviceProvider.GetRequiredService<FormMain>();
             var frmMain = serviceProvider.GetRequiredService<FormMain>();
-            
+
+            var db = serviceProvider.GetRequiredService<ISqlSugarClient>();
+
+            db.CodeFirst.SetStringDefaultLength(200)
+.InitTables(typeof(AuthEntity),typeof(DataEntity), typeof(RecipeEntity), typeof(UserEntity));
+
             Application.Run(frmMain);
             Application.Run(frmMain);
         }
         }
 
 
@@ -50,6 +59,11 @@ namespace Scada
                     loggerBuilder.AddNLog();
                     loggerBuilder.AddNLog();
                 });
                 });
 
 
+            DbType dbType = Enum.Parse<DbType>(configuration["DbContexts:DbType"]);
+            string connnectString = configuration[$"DbContexts:{dbType}:ConnectionString"];
+
+            services.AddSqlSugarSetup(dbType, connnectString);
+
             //»ñÈ¡NLogÅäÖÃÐÅÏ¢
             //»ñÈ¡NLogÅäÖÃÐÅÏ¢
             var nlogConifg = configuration.GetSection("NLog");
             var nlogConifg = configuration.GetSection("NLog");
 
 

+ 6 - 6
Scada/appsettings.json

@@ -1,10 +1,10 @@
 {
 {
-    "DbContexts": {
-        "DbType": "Sqlite",
-        "Sqlite": {
-            "ConnectionString": "Data Source=D://实战项目//Scada//Scada//bin//Debug//net8.0-windows"
-        }
-    },
+  "DbContexts": {
+    "DbType": "SqlServer",
+    "SqlServer": {
+      "ConnectionString": "Server=.;Database=scada;User ID=sa;Password=123456;Encrypt=True;TrustServerCertificate=True;"
+    }
+  },
     //相对路径  可以省略前面的东西
     //相对路径  可以省略前面的东西
     // "ConnectionString": "Data Source=SprayData.db"
     // "ConnectionString": "Data Source=SprayData.db"
     "NLog": {
     "NLog": {