引入Falcon.StoredProcedureRunner组件包在数据库执行原始sql语句并获取json格式结果
This commit is contained in:
parent
32426e53e8
commit
57d7a71932
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Falcon.StoredProcedureRunner;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
|
@ -17,6 +18,7 @@ namespace ReportService.Controllers.api
|
|||
public IWebHostEnvironment Env { get; private set; }
|
||||
public RSDbContext Db { get; set; }
|
||||
public ILogger Logger { get; set; }
|
||||
public IRunner DbRunner { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 报表存放位置
|
||||
|
@ -27,10 +29,11 @@ namespace ReportService.Controllers.api
|
|||
}
|
||||
}
|
||||
|
||||
public ReportApiController(ILogger<ReportApiController> logger,IWebHostEnvironment env,RSDbContext db) {
|
||||
public ReportApiController(ILogger<ReportApiController> logger,IWebHostEnvironment env,RSDbContext db,IRunner r) {
|
||||
this.Logger = logger;
|
||||
this.Env = env;
|
||||
this.Db = db;
|
||||
this.DbRunner = r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -120,7 +123,7 @@ namespace ReportService.Controllers.api
|
|||
/// <param name="sql">要执行的sql语句</param>
|
||||
/// <returns>json对象</returns>
|
||||
public object GetResult(string sql) {
|
||||
var result = this.Db.SqlJsonQuery(sql);
|
||||
var result = this.DbRunner.RunRaw(this.Db,sql);
|
||||
this.Logger.LogInformation($"GetResult:\n{sql}\n{result}");
|
||||
return Content(result,"application/json; charset=utf-8");
|
||||
}
|
||||
|
|
|
@ -1,11 +1,4 @@
|
|||
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;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ReportService.Database
|
||||
{
|
||||
|
@ -18,46 +11,4 @@ namespace ReportService.Database
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public static class RSDbContextExend
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行sql查询,返回json格式数据集
|
||||
/// </summary>
|
||||
/// <param name="db">执行sql语句的数据库上下文</param>
|
||||
/// <param name="sql">要执行的sql语句</param>
|
||||
/// <returns>Json格式sql语句执行结果</returns>
|
||||
public static string SqlJsonQuery(this DbContext db,string sql) => db.Database.SqlJsonQuery(sql);
|
||||
|
||||
/// <summary>
|
||||
/// 执行sql查询,返回json格式数据集
|
||||
/// </summary>
|
||||
/// <param name="db">执行sql语句的数据库上下文</param>
|
||||
/// <param name="sql">要执行的sql语句</param>
|
||||
/// <returns>Json格式sql语句执行结果</returns>
|
||||
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(',') + "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web" ToolsVersion="Current">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5</TargetFramework>
|
||||
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
||||
<FileUpgradeFlags>40</FileUpgradeFlags>
|
||||
<UpgradeBackupLocation>D:\Applications\ReportService\Backup\</UpgradeBackupLocation>
|
||||
<OldToolsVersion>2.0</OldToolsVersion>
|
||||
<Version>1.0.0</Version>
|
||||
<UserSecretsId>5be4fd95-6f72-4658-a0a9-e46921749643</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Remove="wwwroot\report\上传记录.html" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="..\.editorconfig" Link=".editorconfig" />
|
||||
<None Include="wwwroot\report\模板\上传记录.html" />
|
||||
|
@ -16,7 +18,6 @@
|
|||
<None Include="wwwroot\report\报表1.rpt.html" />
|
||||
<None Include="wwwroot\report\报表2.html" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.6" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" />
|
||||
|
@ -25,11 +26,14 @@
|
|||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="4.12.0" />
|
||||
<PackageReference Include="Falcon.StoredProcedureRunner" Version="1.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Update="appsettings.json">
|
||||
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
<ItemGroup>
|
||||
<Folder Include="Database\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using Falcon.StoredProcedureRunner;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
@ -24,6 +25,8 @@ namespace ReportService
|
|||
services.AddDbContext<RSDbContext>(b => {
|
||||
b.UseSqlServer(connStr);
|
||||
});
|
||||
//引入存储过程执行器
|
||||
services.AddFalconSPRunner();
|
||||
|
||||
services.AddControllersWithViews();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user