应用数据库,页面完善

This commit is contained in:
falcon 2020-05-26 16:30:01 +08:00
parent 300cc6d762
commit 0cf8bfae73
12 changed files with 124 additions and 18 deletions

4
.editorconfig Normal file
View File

@ -0,0 +1,4 @@
[*.cs]
# CS1591: 缺少对公共可见类型或成员的 XML 注释
dotnet_diagnostic.CS1591.severity = none

View File

@ -3,7 +3,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 16
VisualStudioVersion = 16.0.30104.148 VisualStudioVersion = 16.0.30104.148
MinimumVisualStudioVersion = 10.0.40219.1 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -6,6 +6,8 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using ReportService.Database;
namespace ReportService.Controllers.api namespace ReportService.Controllers.api
{ {
@ -16,10 +18,12 @@ namespace ReportService.Controllers.api
private readonly ILogger<HomeController> _logger; private readonly ILogger<HomeController> _logger;
public IWebHostEnvironment Env { get; private set; } public IWebHostEnvironment Env { get; private set; }
public RSDbContext Db { get; set; }
public ReportApiController(ILogger<HomeController> logger,IWebHostEnvironment env) { public ReportApiController(ILogger<HomeController> logger,IWebHostEnvironment env,RSDbContext db) {
this._logger = logger; this._logger = logger;
this.Env = env; this.Env = env;
this.Db = db;
} }
/// <summary> /// <summary>
@ -57,12 +61,8 @@ namespace ReportService.Controllers.api
/// <param name="sql">要执行的sql语句</param> /// <param name="sql">要执行的sql语句</param>
/// <returns>json对象</returns> /// <returns>json对象</returns>
public object GetResult(string sql) { public object GetResult(string sql) {
var result = new List<dynamic> { var result = this.Db.SqlJsonQuery(sql);
new {name="n1",age=20,sex=1 }, return Content(result,"application/json; charset=utf-8");
new {name="n2",age=30,sex=2 },
new {name="n3",age=40,sex=1 },
};
return result;
} }
} }
} }

View File

@ -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
{
/// <summary>
/// 报表服务数据库
/// </summary>
public partial class RSDbContext:DbContext
{
public RSDbContext(DbContextOptions options) : base(options) {
}
}
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(',') + "]";
}
}
}
}

View File

@ -2,6 +2,19 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
</ItemGroup>
</Project> </Project>

View File

@ -5,9 +5,11 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using ReportService.Database;
namespace ReportService namespace ReportService
{ {
@ -21,6 +23,12 @@ namespace ReportService
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) { public void ConfigureServices(IServiceCollection services) {
services.AddDbContext<RSDbContext>(b => {
var connStr = this.Configuration.GetValue<string>("Database:ReportService");
b.UseSqlServer(connStr);
});
services.AddControllersWithViews(); services.AddControllersWithViews();
} }

View File

@ -1,4 +1,10 @@
<div id="reportMain"> <style type="text/css">
#rpPage {
padding: 3px 0;
}
</style>
<div id="reportMain">
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">
<li class="nav-item" v-for="m in reportList"> <li class="nav-item" v-for="m in reportList">
<a class="nav-link" href="#" v-on:click="cli(m)">{{m}}</a> <a class="nav-link" href="#" v-on:click="cli(m)">{{m}}</a>

View File

@ -15,7 +15,7 @@
<header> <header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container"> <div class="container">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">ReportService</a> <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">自定义报表服务</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent" <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation"> aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span> <span class="navbar-toggler-icon"></span>

View File

@ -6,5 +6,8 @@
"Microsoft.Hosting.Lifetime": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"Database": {
"ReportService": "Server=.\\SQLSERVER2008R2;Database=ReportService;User ID=sa;Password=111"
}
} }

View File

@ -9,9 +9,9 @@
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<td>name</td> <td>姓名</td>
<td>age</td> <td>年龄</td>
<td>sex</td> <td>性别</td>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -38,7 +38,7 @@
find() { find() {
var sql =myjs.createSql("#para"); var sql =myjs.createSql("#para");
console.log(sql); console.log(sql);
myjs.post(this.url, { sql: this.sql }, function (d) { myjs.post(this.url, { sql: sql }, function (d) {
findPerson.result = d; findPerson.result = d;
}); });
}, },

View File

@ -21,19 +21,23 @@ var myjs = {
for (var i = 0; i < ec.length; i++) { for (var i = 0; i < ec.length; i++) {
var e = $(ec[i]); var e = $(ec[i]);
var n = e.attr("name"); var n = e.attr("name");
var v = e.attr("value"); var v = e.val();
if (n == "ProcedureName") { if (n == "ProcedureName") {
pn = v; pn = v;
} else { } else {
paras = paras + " @" + n + "='" + v + "',"; paras = paras + " @" + n + "='" + v + "',";
} }
} }
return "exec " + pn + paras; return "exec " + pn + paras.substring(0, paras.lastIndexOf(','));;
}, },
//发送post请求发送data数据执行成功方法sc和失败方法ec //发送post请求发送data数据执行成功方法sc和失败方法ec
post(url, data, sc, ec) { post(url, data, sc, ec) {
if (!ec) { if (!ec) {
ec = function (e) { showmsg(e.message); }; ec = function (e) {
debugger;
console.log(e);
alert(e);
};
} }
fetch(url, { fetch(url, {
credentials: 'same-origin', credentials: 'same-origin',