c# - SSMS 中的 SQL 查询有效但在 C# 中无效

标签 c# sql-server .net-core

我有这个查询,它返回存储在 MS SQL 数据库中的图像。如果我在 SSMS (SQL Management Studio) 中运行它,它会完美运行并立即返回。

select image from extra where product_id = 184

但是在 .NET Core MVC 中,它不会返回,SQL 命令只是超时,并出现错误 500,表明 SQL 命令超时。我什至给了它整整 10 分钟来“返回”SQL 命令,但它仍然没有。

[HttpGet("{id}/image")]
public object Get(int id)
{
    using (SqlConnection connection = new SqlConnection(connectionstring)) {
        using (SqlCommand sqlCommand = new SqlCommand("select top 1 image from extra where product_id = @product_id", connection))
        {
            sqlCommand.Parameters.AddWithValue("@product_id", id);
            sqlCommand.Connection.Open();
            using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
            {
                object img = null;

                if (sqlDataReader.HasRows)
                {
                    sqlDataReader.Read();
                    img = new
                    {
                        image = sqlDataReader["image"] == DBNull.Value ? null : "data:image/png;base64," + Convert.ToBase64String((byte[])sqlDataReader["image"])
                    };

                    return img;
                }
            }
        }
    }

    return new { error = true, message = "Unknown error in image getting" };
}

在 Debug模式下单步执行代码。它不会超过这一行:

SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

pastebin是在 SSMS 中运行时 SQL 查询返回的内容。

编辑:SQL 版本为 Microsoft SQL Server 2017 (RTM-CU15-GDR) (KB4505225) - 14.0.3192.2 (X64) Jun 15 2019 00:45:05 Copyright (C) 2017 Microsoft Corporation Standard Edition (64-bit) on Windows Server 2012 R2 Standard 6.3 <X64> (Build 9600: ) (Hypervisor)

最佳答案

请尝试以下。

同样在Catch上打个断点,看看ex.Message是什么

try
            {
                SqlCommand sqlCommand = new SqlCommand("select top 1 image from extra where product_id = @product_id", connection);
                sqlCommand.Parameters.Add(new SqlParameter("@product_id", id));
                using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                {
                    while (sqlDataReader.Read())
                    {
                        img = new
                        {
                            image = sqlDataReader["image"] == DBNull.Value ? null : "data:image/png;base64," + Convert.ToBase64String((byte[])sqlDataReader["image"])
                        };
                        connection.Close();
                        return img;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return new { error = true, message = "Unknown error in image getting" };

关于c# - SSMS 中的 SQL 查询有效但在 C# 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58029271/

相关文章:

sql - 从sql server中的字符串中提取单词

c# - Xunit.Net 由于 ID 重复而跳过测试

c# - 如何在 .NET 中使用 1.2 TLS 版本获取 EWS 服务器的身份验证

c# - 如何正确处理 WebResponse 实例?

c# - 产生相同数据类型的另一个 Enumerable 的返回结果

sql-server - 打开在 SQL Server 的新实例中创建的数据库

c# - 错误无法从程序集 'dotless.Core,版本 = 1.5.3.0 加载类型 'dotless.Core.configuration.WebConfigConfigurationLoader'

php - 如何在 Windows 开发环境的 PHP 中将 PDO 与 MSSQL 结合使用?

c# - Docker.Dotnet 如何拉取镜像

c# - 与 IIS 相比,Kestrel 的性能较慢