Sqlsugar Oracle 配置 和服务注册以及使用
关于Sqlsugar
项目模板:
实体(仅参考 可以不看)
-- inpatient.cs
-- Recorddetail.cs
1. launchSettings.json 数据库配置
"profiles": {
"http": {
...默认
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"LOCAL_DB_TYPE": "Oracle",
"LOCAL_DB_URL": "Data Source=域名/服务名;User ID=用户名;Password=密码"
}
}
},
2.在program 将SqlSugarClient注入
builder.Services.AddKeyedScoped<ISqlSugarClient>("local", (_,_) => new SqlSugarClient(new ConnectionConfig
{
DbType = DbType.Oracle,
ConnectionString = builder.Configuration.GetSection("LOCAL_DB_URL").Value,
IsAutoCloseConnection = true,
}));
AddKeyedScooped 是以键值对的方式注册。
3.在服务中使用SqlSugarClient
通过[FromKeyedServices("local")] 来获取键为”local“的这个SqlSugarClient
// --IService
public interface IGetinformation // 定义接口
{
Task<Recorddetail> Getxaml(Inpatient inpatient);
}
// --Service
public class Getinformation([FromKeyedServices("local")] ISqlSugarClient client) : IGetinformation // 接口实现
public async Task<Recorddetail> Getxaml(Inpatient inpatient)
{
// 一个简单的条件查询
// $"select noofinpat from Inpatient it where(it.patid == {inpatient.patid})"
await client.Queryable<Inpatient>().Where(
it => it.patid == inpatient.patid &&
it.incount==inpatient.incount
).Select(it=>it.noofinpat).FirstAsync();
}
在program 再将这条服务注入
builder.Services.AddScoped<IGetinformation, Getinformation>();
4. 通过控制器中暴露请求
[Route("api/[controller]")]
[ApiController]
public class GetinformationController(IGetinformation service) : ControllerBase
{
[HttpGet("[action]")]
public async Task<EmrXamlResponse> GetXaml([FromBody] Inpatient inpatient)
{
var result = await service.Getxaml(inpatient);
return new EmrXamlResponse()
{
xaml = result.content,
Code = result.content==null ? -1 : 1,
ErrMsg = result.name
};
}
}