根据sql获取数据
parent
c930aa1ad4
commit
461d454aa8
|
|
@ -0,0 +1,55 @@
|
|||
using OpenAuth.App.BaseApp.Base;
|
||||
using OpenAuth.Repository.Domain.DataMaintenance;
|
||||
using OpenAuth.Repository;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using OpenAuth.App.Interface;
|
||||
using SqlSugar;
|
||||
using System.Data;
|
||||
using Infrastructure;
|
||||
|
||||
namespace OpenAuth.App.ServiceApp.FireManagement
|
||||
{
|
||||
public class FireResourcesApp : SqlSugarBaseApp<ApplicationData, SugarDbContext>
|
||||
{
|
||||
public FireResourcesApp(ISugarUnitOfWork<SugarDbContext> unitWork,
|
||||
ISimpleClient<ApplicationData> repository, IAuth auth) : base(unitWork,
|
||||
repository,
|
||||
auth)
|
||||
{
|
||||
_auth = auth;
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据sql获取数据
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Response<DataTable>> GetDataBySql(string sql)
|
||||
{
|
||||
// 检查 SQL 是否为查询操作
|
||||
if (string.IsNullOrWhiteSpace(sql) || !sql.Trim().StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new ArgumentException("只允许查询操作");
|
||||
}
|
||||
try
|
||||
{
|
||||
using (SugarDbContext db = base.UnitWork.CreateContext())
|
||||
{
|
||||
// 执行查询操作,返回 DataTable
|
||||
DataTable dt = await db.Db.SqlQueryable<object>(sql).ToDataTableAsync();
|
||||
return new Response<DataTable>
|
||||
{
|
||||
Result = dt
|
||||
};
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("sql格式不正确");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using OpenAuth.App.ServiceApp.FireManagement;
|
||||
using OpenAuth.App.ServiceApp.GoView;
|
||||
using System.Data;
|
||||
|
||||
namespace OpenAuth.WebApi.Controllers.ServiceControllers.FireManagement
|
||||
{
|
||||
/// <summary>
|
||||
/// 自然资源管理
|
||||
/// </summary>
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class FireResourcesController : ControllerBase
|
||||
{
|
||||
private readonly FireResourcesApp _app;
|
||||
|
||||
public FireResourcesController(FireResourcesApp app)
|
||||
{
|
||||
_app = app;
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据sql获取数据
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public async Task<Response<DataTable>> GetDataBySql(string sql)
|
||||
{
|
||||
return await _app.GetDataBySql(sql);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue