C# 无法以异步方式执行sql查询

C# 无法以异步方式执行sql查询,c#,.net,sql-server,asp.net-core,C#,.net,Sql Server,Asp.net Core,我试图以异步方式执行SQL查询,并希望返回结果,但它没有返回数据,不确定我缺少了什么 public async Task<string> AsyncSBSystemApps() { using (var scope = _scopeFactory.CreateScope()) { using (SqlConnection connection = new SqlConnection(connectionString))

我试图以异步方式执行SQL查询,并希望返回结果,但它没有返回数据,不确定我缺少了什么

public async Task<string> AsyncSBSystemApps()
    {
        using (var scope = _scopeFactory.CreateScope())
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(@"SELECT 
                        a.app_guid as AppGuid,
                        a.name as AppName,
                        a.state as AppState,
                        a.created_at as AppCreatedAt,
                        a.updated_at as AppUpdatedAt,
                        a.foundation as AppFoundation,

                        s.name as SpaceName,
                        o.name as OrgName
                    FROM
                        apps as a
                    INNER JOIN
                        spaces as s ON a.space_guid = s.space_guid
                    INNER JOIN
                        organizations as o ON s.org_guid = o.org_guid
                    where s.name = 'system' and o.name = 'system' and a.foundation = 2", connection);

                await connection.OpenAsync();


                string result = (string)command.ExecuteScalar() ;

                return result;
            }
        }
公共异步任务AsyncSBSystemApps()
{
使用(var scope=\u scopeFactory.CreateScope())
{
使用(SqlConnection连接=新的SqlConnection(connectionString))
{
SqlCommand=newsqlcommand(@“选择
a、 应用程序guid作为应用程序guid,
a、 名称为AppName,
a、 州为AppState,
a、 以AppCreatedAt的形式创建,
a、 以AppUpdatedAt的形式更新了\u,
A.基金会作为基金会,
s、 名称为SpaceName,
o、 名为OrgName
从…起
应用程序作为
内连接
空间作为s在a.space\u guid=s.space\u guid上
内连接
在s.org\u guid=o.org\u guid上作为o的组织
其中S.NoNe=“Stand”和O.NeNe=“Stand”和A.Fund=2“,连接);
等待连接。OpenAsync();
字符串结果=(字符串)命令。ExecuteScalar();
返回结果;
}
}
如果要使用来保持对JSON输出格式的完全控制,可以将其添加到sql查询后面并反序列化JSON,下面是我测试的一个示例

public class ResponseBody
    {
        public string StudioName { get; set; }
        public string CityName { get; set; }
        public string Street { get; set; }
    }

    public async Task<IActionResult> AsyncSBSystemApps()
    {
        using (var scope = _scopeFactory.CreateScope())
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(@"SELECT 
                    s.Name as StudioName,

                    c.Name as CityName,
                    a.Street as Street
                FROM
                    Studios as s
                INNER JOIN
                    Cities as c ON s.Id = c.StudioId
                INNER JOIN
                    Addresses as a ON c.Id = a.CityId
                where s.Name = 'Studio1' and c.Name = 'Wuxi' and a.Number = '101'
                For JSON PATH  ", connection);

                await connection.OpenAsync();

                var result = command.ExecuteScalar().ToString();

                 // Deserializing json data to object 
                var getModel = JsonConvert.DeserializeObject<List<ResponseBody>>(result);

                //return ...
            }
        }

    }
公共类响应库
{
公共字符串研究名称{get;set;}
公共字符串CityName{get;set;}
公共字符串Street{get;set;}
}
公共异步任务AsyncSBSystemApps()
{
使用(var scope=\u scopeFactory.CreateScope())
{
使用(SqlConnection连接=新的SqlConnection(connectionString))
{
SqlCommand=newsqlcommand(@“选择
s、 以StudioName命名,
c、 名称为CityName,
a、 街道
从…起
电影制片厂
内连接
城市为s.Id上的c=c.StudioId
内连接
在c.Id=a.CityId上作为a的地址
其中s.Name='Studio1'和c.Name='无锡',a.Number='101'
对于JSON路径,“连接”;
等待连接。OpenAsync();
var result=command.ExecuteScalar().ToString();
//将json数据反序列化到对象
var getModel=JsonConvert.DeserializeObject(结果);
//返回。。。
}
}
}
正如mason所建议的,您也可以使用Dapper(安装Dapper软件包 )要轻松获取查询,请执行以下操作:

using (SqlConnection connection = new SqlConnection(connectionString))
            {
                await connection.OpenAsync();

                var queryResult = connection.Query<ResponseBody>(@"SELECT 
                    s.Name as StudioName,

                    c.Name as CityName,
                    a.Street as Street
                FROM
                    Studios as s
                INNER JOIN
                    Cities as c ON s.Id = c.StudioId
                INNER JOIN
                    Addresses as a ON c.Id = a.CityId
                where s.Name = 'Studio1' and c.Name = 'Wuxi' and a.Number = '101'");

                return queryResult ;
            }
使用(SqlConnection连接=新的SqlConnection(connectionString))
{
等待连接。OpenAsync();
var queryResult=connection.Query(@“选择
s、 以StudioName命名,
c、 名称为CityName,
a、 街道
从…起
电影制片厂
内连接
城市为s.Id上的c=c.StudioId
内连接
在c.Id=a.CityId上作为a的地址
其中s.Name='Studio1',c.Name='无锡',a.Number='101';
返回查询结果;
}

您是否在SSMS中尝试过此查询?它是否返回任何数据?此外,ExecuteScalar返回第一行的第一列。您希望此代码返回什么?是的,我在SSMS中尝试过,它确实返回数据,我需要查询executesCan返回的所有数据(JSON)?首先,您需要一个表示此查询返回的数据的模型类然后,您需要ExecuteCalar而不是ExecuteReader。使用读取器,您可以逐行读取返回的数据并创建模型类的实例。每个实例都应添加到该类的列表中。最后,您可以使用适当的库将列表序列化为json字符串。没有直接模式来转换rows以单个字符串的形式由查询返回。也许您可以开始使用Dapper之类的ORM工具,这将大大简化对象实例中原始数据的转换。仍然需要在json中序列化结果