# PlayGround\.config\dotnet-tools.json
| |
| { |
| "version": 1, |
| "isRoot": true, |
| "tools": { |
| "csharpier": { |
| "version": "0.29.1", |
| "commands": [ |
| "dotnet-csharpier" |
| ], |
| "rollForward": false |
| } |
| } |
| } |
PlayGround\.vscode\launch.json
| |
| { |
| "version": "0.2.0", |
| "configurations": [ |
| { |
| |
| |
| |
| "name": ".NET Core Launch (console)", |
| "type": "coreclr", |
| "request": "launch", |
| "preLaunchTask": "build", |
| |
| "program": "${workspaceFolder}/bin/Debug/net9.0/PlayGround.dll", |
| "args": [], |
| "cwd": "${workspaceFolder}", |
| |
| "console": "internalConsole", |
| "stopAtEntry": false |
| }, |
| { |
| "name": ".NET Core Attach", |
| "type": "coreclr", |
| "request": "attach" |
| } |
| ] |
| } |
PlayGround\.vscode\tasks.json
| |
| { |
| "version": "2.0.0", |
| "tasks": [ |
| { |
| "label": "build", |
| "command": "dotnet", |
| "type": "process", |
| "args": [ |
| "build", |
| "${workspaceFolder}/Playground.sln", |
| "/property:GenerateFullPaths=true", |
| "/consoleloggerparameters:NoSummary;ForceNoAlign" |
| ], |
| "problemMatcher": "$msCompile" |
| }, |
| { |
| "label": "publish", |
| "command": "dotnet", |
| "type": "process", |
| "args": [ |
| "publish", |
| "${workspaceFolder}/Playground.sln", |
| "/property:GenerateFullPaths=true", |
| "/consoleloggerparameters:NoSummary;ForceNoAlign" |
| ], |
| "problemMatcher": "$msCompile" |
| }, |
| { |
| "label": "watch", |
| "command": "dotnet", |
| "type": "process", |
| "args": [ |
| "watch", |
| "run", |
| "--project", |
| "${workspaceFolder}/Playground.sln" |
| ], |
| "problemMatcher": "$msCompile" |
| } |
| ] |
| } |
PlayGround\.csharpierrc
PlayGround\appsettings.json
| |
| { |
| "ConnectionStrings": { |
| "AppOne": "Server=localhost;Database=DDD;Trusted_Connection=True;MultipleActiveResultSets=true", |
| "MySqlConnection": "Server=localhost;Database=ElectricDDD;Uid=root;Pwd=123456;" |
| }, |
| "DataProvider": "MsSql", |
| "Logging": { |
| "LogLevel": { |
| "Default": "Information", |
| "Microsoft.AspNetCore": "Warning" |
| } |
| }, |
| "Authentication": { |
| "JwtBearer": { |
| "SecurityKey": "ELETRICAPIC421AAEE0D114E9C", |
| "Issuer": "EletricAPI", |
| "Audience": "EletricAPI" |
| } |
| }, |
| "CorsOrigins": "http://localhost:9527", |
| "AllowedHosts": "*" |
| } |
| |
PlayGround\Entity.cs
| |
| public class SugarEntity |
| { |
| |
| |
| |
| } |
| |
PlayGround\PlayGround.csproj
| |
| <Project Sdk="Microsoft.NET.Sdk"> |
| |
| <PropertyGroup> |
| <OutputType>Exe</OutputType> |
| <TargetFramework>net9.0</TargetFramework> |
| <ImplicitUsings>enable</ImplicitUsings> |
| <Nullable>enable</Nullable> |
| </PropertyGroup> |
| <ItemGroup> |
| <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> |
| <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> |
| <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> |
| <PackageReference Include="SqlSugarCore" Version="5.1.4.168-preview11" /> |
| <PackageReference Include="System.Data.SqlClient" Version="4.8.6" /> |
| </ItemGroup> |
| |
| <ItemGroup> |
| <None Update="appsettings.json"> |
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> |
| </None> |
| <None Update="nlog.config" CopyToOutputDirectory="Always" /> |
| </ItemGroup> |
| |
| </Project> |
| |
PlayGround\Playground.sln
PlayGround\Program.cs
| |
| using System.Data; |
| using Microsoft.Extensions.Configuration; |
| using SqlSugarFrameworkCore; |
| |
| class Program |
| { |
| static void Main(string[] args) |
| { |
| ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); |
| IConfigurationRoot configurationRoot = configurationBuilder.SetBasePath(AppContext.BaseDirectory).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build(); |
| IConfigurationSection dbConfigurationSection = configurationRoot.GetSection("ConnectionStrings"); |
| LongSugarClient longSugarClient = LongSugarClient.GetInstance(DbInfo.MesOne, dbConfigurationSection); |
| List<EleTodo> eleTodos = longSugarClient.ExcuteRawSql<EleTodo>("SELECT * FROM [DDD].[dbo].[EleTodo]"); |
| foreach (var eleTodo in eleTodos) |
| { |
| System.Console.WriteLine(eleTodo.Name); |
| } |
| |
| DataTable dataTable = longSugarClient.ExcuteRawSql("SELECT * FROM [DDD].[dbo].[EleTodo]"); |
| System.Console.WriteLine(dataTable.Rows.Count); |
| |
| DataTable dataTable2 = longSugarClient.ExcuteRawSql("SELECT count(*) FROM [DDD].[dbo].[EleTodo]"); |
| System.Console.WriteLine(dataTable2.Rows[0][0]); |
| |
| |
| List<EleTodoCount> eleTodoCounts = longSugarClient.ExcuteRawSql<EleTodoCount>("SELECT count(*) as count FROM [DDD].[dbo].[EleTodo]"); |
| System.Console.WriteLine(eleTodoCounts.First().Count); |
| |
| } |
| } |
| |
| |
| |
PlayGround\run.bat
PlayGround\SqlSugarCore.cs
| |
| using System.Data; |
| using System.Linq.Expressions; |
| using Microsoft.Extensions.Configuration; |
| using SqlSugar; |
| |
| namespace SqlSugarFrameworkCore; |
| |
| public class DbSettings |
| { |
| public const string AppOne = "AppOne"; |
| public const string AppDevOne = "AppDevOne"; |
| public const string AppThreeOne = "AppThreeOne"; |
| public const string Oracle = "Oracle"; |
| } |
| |
| public enum DbInfo |
| { |
| MesOne = 1, |
| MesDevOne = 2, |
| MesThree = 3, |
| Oracle = 4, |
| } |
| |
| public class LongSugarClient |
| { |
| public static LongSugarClient sugarInstance; |
| public static DbInfo curDbInfo; |
| public static Dictionary<DbInfo, LongSugarClient> sugarInstances = new Dictionary<DbInfo, LongSugarClient>(); |
| |
| public static readonly object sugarInstanceLock = new object(); |
| public SqlSugarClient _db; |
| |
| private LongSugarClient(SqlSugarClient sqlSugarClient) |
| { |
| _db = sqlSugarClient; |
| } |
| |
| public static LongSugarClient GetInstance(DbInfo dbInfo, IConfigurationSection dbConfigurationSection) |
| { |
| if (sugarInstances.ContainsKey(dbInfo)) |
| { |
| sugarInstances.TryGetValue(dbInfo, out LongSugarClient sugarInstance); |
| return sugarInstance; |
| } |
| |
| lock (sugarInstanceLock) |
| { |
| if (sugarInstances.ContainsKey(dbInfo)) |
| { |
| sugarInstances.TryGetValue(dbInfo, out LongSugarClient sugarInstance); |
| return sugarInstance; |
| } |
| GetSugarClient(dbInfo, dbConfigurationSection); |
| } |
| return sugarInstance; |
| } |
| |
| private static void GetSugarClient(DbInfo dbInfo, IConfigurationSection dbConfigurationSection) |
| { |
| if (dbInfo == DbInfo.MesOne) |
| { |
| string connectStr = dbConfigurationSection.GetSection(DbSettings.AppOne).Value ?? throw new ArgumentNullException($"{DbSettings.AppOne} is misstion"); |
| |
| System.Console.WriteLine(connectStr); |
| CreateSugarClient(dbInfo, connectStr); |
| } |
| else if (dbInfo == DbInfo.MesDevOne) |
| { |
| string connectStr = dbConfigurationSection.GetSection(DbSettings.AppDevOne).Value ?? throw new ArgumentNullException($"{DbSettings.AppDevOne} is misstion"); |
| CreateSugarClient(dbInfo, connectStr); |
| } |
| else if (dbInfo == DbInfo.MesThree) |
| { |
| string connectStr = dbConfigurationSection.GetSection(DbSettings.AppThreeOne).Value ?? throw new ArgumentNullException($"{DbSettings.AppThreeOne} is misstion"); |
| CreateSugarClient(dbInfo, connectStr); |
| } |
| else if (dbInfo == DbInfo.Oracle) |
| { |
| string connectStr = dbConfigurationSection.GetSection(DbSettings.Oracle).Value ?? throw new ArgumentNullException($"{DbSettings.Oracle} is misstion"); |
| CreateSugarClient(dbInfo, connectStr); |
| } |
| } |
| |
| private static void CreateSugarClient(DbInfo dbInfo, string connectStr) |
| { |
| SqlSugarClient sqlSugar = new SqlSugarClient( |
| new ConnectionConfig() |
| { |
| DbType = SqlSugar.DbType.SqlServer, |
| ConnectionString = connectStr, |
| IsAutoCloseConnection = true, |
| }, |
| db => |
| { |
| |
| db.Aop.OnLogExecuting = (sql, pars) => |
| { |
| |
| |
| }; |
| } |
| ); |
| sugarInstance = new LongSugarClient(sqlSugar); |
| sugarInstances.Add(dbInfo, sugarInstance); |
| } |
| |
| |
| public DataTable ExcuteRawSql(string sql) |
| { |
| DataTable dataTable = this._db.SqlQueryable<DataTable>(sql).ToDataTable(); |
| return dataTable; |
| } |
| |
| public List<T> ExcuteRawSql<T>(string sql) |
| where T : SugarEntity, new() |
| { |
| List<T> list = this._db.SqlQueryable<T>(sql).ToList(); |
| return list; |
| } |
| |
| } |
| |
PlayGround\TodoEntity.cs
| |
| public enum TodoStatus |
| { |
| Undo, |
| Done, |
| } |
| |
| public class EleTodo : SugarEntity |
| { |
| public EleTodo() { } |
| |
| public Guid Id { get; set; } |
| |
| public string Name { get; set; } |
| |
| public TodoStatus Status { get; set; } |
| |
| public string? Remark { get; set; } |
| |
| public Guid UserId { get; set; } |
| } |
| |
| public class EleTodoCount : SugarEntity |
| { |
| public int Count { get; set; } |
| } |
| |
PlayGround\zz.bat
| |
| set "scriptPath=%cd%" |
| D: && cd D:\dotnet.-script\App\bin\Debug\net8.0\ && D:\dotnet.-script\App\bin\Debug\net8.0\App.exe "%scriptPath%" "Question" |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战