diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8859c78 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CS1591: 缺少对公共可见类型或成员的 XML 注释 +dotnet_diagnostic.CS1591.severity = none diff --git a/ReportService.sln b/ReportService.sln index d2f3e16..94a269f 100644 --- a/ReportService.sln +++ b/ReportService.sln @@ -3,7 +3,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30104.148 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReportService", "ReportService\ReportService.csproj", "{3E4CF0D7-E242-4F61-8A02-C1B91402DCE9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReportService", "ReportService\ReportService.csproj", "{3E4CF0D7-E242-4F61-8A02-C1B91402DCE9}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{451EF38E-0BB2-4FAC-AF24-B7E20FFDB71C}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/ReportService/Controllers/api/ReportApiController.cs b/ReportService/Controllers/api/ReportApiController.cs index 3e7c1a0..088f0f3 100644 --- a/ReportService/Controllers/api/ReportApiController.cs +++ b/ReportService/Controllers/api/ReportApiController.cs @@ -6,6 +6,8 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using Microsoft.Net.Http.Headers; +using ReportService.Database; namespace ReportService.Controllers.api { @@ -16,10 +18,12 @@ namespace ReportService.Controllers.api private readonly ILogger _logger; public IWebHostEnvironment Env { get; private set; } + public RSDbContext Db { get; set; } - public ReportApiController(ILogger logger,IWebHostEnvironment env) { + public ReportApiController(ILogger logger,IWebHostEnvironment env,RSDbContext db) { this._logger = logger; this.Env = env; + this.Db = db; } /// @@ -57,12 +61,8 @@ namespace ReportService.Controllers.api /// 要执行的sql语句 /// json对象 public object GetResult(string sql) { - var result = new List { - new {name="n1",age=20,sex=1 }, - new {name="n2",age=30,sex=2 }, - new {name="n3",age=40,sex=1 }, - }; - return result; + var result = this.Db.SqlJsonQuery(sql); + return Content(result,"application/json; charset=utf-8"); } } } diff --git a/ReportService/Database/RSDbContext.cs b/ReportService/Database/RSDbContext.cs new file mode 100644 index 0000000..182df9e --- /dev/null +++ b/ReportService/Database/RSDbContext.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Data.Common; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; + +namespace ReportService.Database +{ + /// + /// 报表服务数据库 + /// + public partial class RSDbContext:DbContext + { + public RSDbContext(DbContextOptions options) : base(options) { + } + + } + + public static class RSDbContextExend + { + /// + /// 执行sql查询,返回json格式数据集 + /// + /// 执行sql语句的数据库上下文 + /// 要执行的sql语句 + /// Json格式sql语句执行结果 + public static string SqlJsonQuery(this DbContext db,string sql) => db.Database.SqlJsonQuery(sql); + + /// + /// 执行sql查询,返回json格式数据集 + /// + /// 执行sql语句的数据库上下文 + /// 要执行的sql语句 + /// Json格式sql语句执行结果 + public static string SqlJsonQuery(this DatabaseFacade db,string sql) { + var connection = db.GetDbConnection(); + using(var cmd = connection.CreateCommand()) { + cmd.CommandText = sql; + cmd.CommandType = System.Data.CommandType.Text; + connection.Open(); + var dr = cmd.ExecuteReader(); + var result = new StringBuilder(); + if(!dr.CanGetColumnSchema()) + return ""; + while(dr.Read()) { + var item = new StringBuilder(); + var columnSchema = dr.GetColumnSchema(); + for(var i = 0;i < columnSchema.Count;i++) { + var name = dr.GetName(i); + var value = dr.IsDBNull(i) ? null : dr.GetValue(i); + item.Append($"\"{name}\":\"{value}\","); + } + result.Append($"{{{item.ToString().TrimEnd(',')}}},"); + } + connection.Close(); + return "[" + result.ToString().TrimEnd(',') + "]"; + } + } + } +} diff --git a/ReportService/ReportService.csproj b/ReportService/ReportService.csproj index 40e2b90..5bfbc7d 100644 --- a/ReportService/ReportService.csproj +++ b/ReportService/ReportService.csproj @@ -2,6 +2,19 @@ netcoreapp3.1 + bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml + + + + + + + + + + + + diff --git a/ReportService/Startup.cs b/ReportService/Startup.cs index 5de9a8a..67782b5 100644 --- a/ReportService/Startup.cs +++ b/ReportService/Startup.cs @@ -5,9 +5,11 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using ReportService.Database; namespace ReportService { @@ -21,6 +23,12 @@ namespace ReportService // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + + services.AddDbContext(b => { + var connStr = this.Configuration.GetValue("Database:ReportService"); + b.UseSqlServer(connStr); + }); + services.AddControllersWithViews(); } diff --git a/ReportService/Views/Home/Index.cshtml b/ReportService/Views/Home/Index.cshtml index 60c65e4..c43ad96 100644 --- a/ReportService/Views/Home/Index.cshtml +++ b/ReportService/Views/Home/Index.cshtml @@ -1,4 +1,10 @@ -
+ + +