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": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net9.0/PlayGround.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"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
{
public 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="DataTablePrettyPrinter" Version="0.2.0" />
<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 DataTablePrettyPrinter;
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);
DataTable dataTable = longSugarClient.ExcuteRawSql("SELECT * FROM [DDD].[dbo].[EleTodo]");
Console.WriteLine(dataTable.ToPrettyPrintedString());
foreach (DataRow dataRowrow in dataTable.Rows)
{
System.Console.WriteLine(dataRowrow[0]);
}
}
}
PlayGround\run.bat
dotnet run
PlayGround\SqlSugarCore.cs
using System.Data;
using System.Linq.Expressions;
using Microsoft.Extensions.Configuration;
using SqlSugar;
namespace SqlSugarFrameworkCore;
public class DbSettings
{
public const string MesOne = "AppOne";
public const string MesDevOne = "AppDevOne";
public const string MesThreeOne = "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.MesOne).Value ?? throw new ArgumentNullException($"{DbSettings.MesOne} is misstion");
System.Console.WriteLine(connectStr);
CreateSugarClient(dbInfo, connectStr);
}
else if (dbInfo == DbInfo.MesDevOne)
{
string connectStr = dbConfigurationSection.GetSection(DbSettings.MesDevOne).Value ?? throw new ArgumentNullException($"{DbSettings.MesDevOne} is misstion");
CreateSugarClient(dbInfo, connectStr);
}
else if (dbInfo == DbInfo.MesThree)
{
string connectStr = dbConfigurationSection.GetSection(DbSettings.MesThreeOne).Value ?? throw new ArgumentNullException($"{DbSettings.MesThreeOne} 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) =>
{
//打印日志
// Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
};
}
);
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"
标签:string,connectStr,dbInfo,PlayGround,单例,泛型,new,public,sqlsugar
From: https://www.cnblogs.com/zhuoss/p/18389488