如何从 C# 中的 SQL 查询字符串调用 DAO
How to Call DAO from a SQL query string in C#
我知道在 springboot 中你可以直接从 sql 字符串调用 DAO。
I.E
SqlQuery = "
SELECT new com.testProject.model.testDAO(test.country, test.code, sum(case when test.dummy1 = 'POS' then 1 else 0 end), sum(case when test.dummy2 = 'POS' then 1 else 0 end))
FROM test";
但是可以在 C# 中从 Sql 字符串调用 DAO 吗?我从查询中删除了 DAO 以使其工作:
SqlQuery = "
SELECT test.country, test.code, sum(test.dummy1 = 'POS' ? 1 : 0) as dumm1, sum(test.dummy2 = 'POS' ? 1 : 0) as dummy2
FROM test";
有什么方法可以在 SQL 查询字符串中调用 DAO 吗?
既然你说你正在使用带 cosmos db 输入的 azure http 触发器,据我所知,没有像 spring 这样的 DAO
机制在 cosmos db 输入中使用查询字符串参数启动。
不过,根据Azure Function Cosmos Db Input文档中提供的example,它支持使用自定义对象Class映射来自sql的查询结果。请参考我的测试:
cosmos db 中的示例数据:
Azure 函数代码:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace CosmosTriggerCore
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[CosmosDB(
databaseName: "db",
collectionName: "coll",
ConnectionStringSetting = "cosmosdbstring",
SqlQuery = "SELECT top 2 * FROM c")]
IEnumerable<TestDoc> testDocItems,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
foreach (TestDoc doc in testDocItems)
{
log.LogInformation(doc.Id);
log.LogInformation(doc.Name);
}
return new OkResult();
}
}
public class TestDoc
{
public string Id { get; set; }
public string Name { get; set; }
}
}
我用TestDoc
Class接受了查询结果,我可以访问代码中的属性。
我知道在 springboot 中你可以直接从 sql 字符串调用 DAO。
I.E
SqlQuery = "
SELECT new com.testProject.model.testDAO(test.country, test.code, sum(case when test.dummy1 = 'POS' then 1 else 0 end), sum(case when test.dummy2 = 'POS' then 1 else 0 end))
FROM test";
但是可以在 C# 中从 Sql 字符串调用 DAO 吗?我从查询中删除了 DAO 以使其工作:
SqlQuery = "
SELECT test.country, test.code, sum(test.dummy1 = 'POS' ? 1 : 0) as dumm1, sum(test.dummy2 = 'POS' ? 1 : 0) as dummy2
FROM test";
有什么方法可以在 SQL 查询字符串中调用 DAO 吗?
既然你说你正在使用带 cosmos db 输入的 azure http 触发器,据我所知,没有像 spring 这样的 DAO
机制在 cosmos db 输入中使用查询字符串参数启动。
不过,根据Azure Function Cosmos Db Input文档中提供的example,它支持使用自定义对象Class映射来自sql的查询结果。请参考我的测试:
cosmos db 中的示例数据:
Azure 函数代码:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace CosmosTriggerCore
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[CosmosDB(
databaseName: "db",
collectionName: "coll",
ConnectionStringSetting = "cosmosdbstring",
SqlQuery = "SELECT top 2 * FROM c")]
IEnumerable<TestDoc> testDocItems,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
foreach (TestDoc doc in testDocItems)
{
log.LogInformation(doc.Id);
log.LogInformation(doc.Name);
}
return new OkResult();
}
}
public class TestDoc
{
public string Id { get; set; }
public string Name { get; set; }
}
}
我用TestDoc
Class接受了查询结果,我可以访问代码中的属性。