I have this query which returns an image stored in an MS SQL database. If I run this in SSMS (SQL Management Studio) it works perfectly and returns instantly.
select image from extra where product_id = 184
However in .NET Core MVC it doesn't return, and the SQL command simply times out and an error 500 occurs stating the SQL Command timeout. I have even gave it 10 full minutes to "return" the SQL command and it still doesn't.
[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" };
}
Stepping through the code in debug mode. It doesn't get past this line:
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
This pastebin is what the SQL query returns if I run it in SSMS.
EDIT: SQL Version is 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)