last day (15 days later) » 

9:14 AM
2
Q: SQL Query in SSMS works but not in C#

DeckerzI 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 ...

 
if it timesout, you should extend the timeout.
 
@DanielA.White i have tried extending it to 10 minutes. Still fails
 
@Deckerz The constructor should store the connection string to a field, not the connection. Then you create and open it as needed. Keeping the connection around is a bad idea.
 
@Deckerz If you don't have a compelling reason to keep the connection always open (which you don't, and even if you did, connection pooling would already do it for you), you should declare the connection variable at the method level. Then you should put using around all database related objects (SqlConnection, SqlCommand, SqlDataReader) and remove any manual calls to Close, not only in this method, but everywhere. Often that alone solves such problems.
 
@mason Just changed it to see if it makes a difference. It didn't, still fails.
 
9:14 AM
I doubt this has anything to do with it, but why do you have the "top 1" in the code?
 
@juharr it was just while debugging think maybe it was returning more than one and it broke it (lots of large images being returned)
 
If the problem still happens with all the usings, provided that you have restarted the project and you have not called other methods that do not yet have usings, then it's time to double check that the connection string connects you to the same server and same database. If so, execute sp_who in SSMS while the query is running in the program and see what is blocking you.
 
It is @GSerg weirdly this query works with high numbers i.e. 1254306 works but 184 doesn't.
 
could you include the code that sets the timeout? Also, when you run the query in SSMS, how long does it take to return the results?
 
@Matt.G SSMS says it takes 0.00 seconds. and i have used sqlCommand.CommandTimeout = 60;
 
9:14 AM
What version of SQL Server are using? Express or Standard? Have you tried to De-Fragment the database? Intermittent results like this is usually occurs with Express when a database becomes fragmented.
 
@jdweng its 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)
 
Have you had any luck with sp_who (or alternatives)? Is your query executing or waiting?
 
You are running on IIS Server. Are you running SSMS from same machine as the c# code is running? This looks like a credential issue running from a IIS.
 
Yes they are both using the same connection string and are on the same machine.
 
@jdweng What makes you think it's a credential issue? I would think if he couldn't connect to the DB, we'd get a different error. He also stated in comments on a deleted answer that other queries in this app are working. Deckerz - are you using the same account for this web app as you are for SSMS?
 
9:14 AM
@GSerg I don't see it in the list as executing or waiting the status is runnable when its "stuck"
@mason yes SSMS and c# use the same account details.
 
could you try setting the command timeout to 0 (to wait indefinitely)
 
@Matt.G I have just tried that and I have tried 600 seconds. It never returns.
 
If you copied this DB access to a console app and try to run it, does it succeed? I think we need to work towards a minimal reproducible example.
 
@mason Just created that all inline/hardcoded no variables. Still doesn't work.
 
Because it is a server machine I think it is credential issues. Is the code running as a service? I need to see connection string. In SSMS does it show Windows Credentials? If so the connection string should not have a MDF file and should have Integrated security = true. Also should not have user and password. Is the database attached to server. Then the connection string should NOT have ATTACH. ATTACH should only be used when the database is not attached to a server. It is already attached.
 
9:14 AM
Are you sure that you get a SQL time out?
 
@jdweng its not credential issues, other urls (sql queries) on the same MVC app work fine.
@SeyedRaoufModarresi yes that it exception that is thrown.
 
Probably not related to your issue, but you should read over Can we stop using AddWithValue Already? and implement the advice there.
 
Please increase executionTimeout of your asp request in web config, maybe it helps you.
 
@SeyedRaoufModarresi How would that help if it's the SQL command timing out? And why are you assuming there's a web.config - this is ASP.NET Core on .NET Core.
 
@Deckerz runnable means it's doing something. Does it have a non-zero value in the blk field? Please use stackoverflow.com/a/41078535/11683 to see exactly what code it is executing, and/or run the SQL profiler and catch the exact SQL that arrives at the server when you run the code.
 
9:14 AM
With same credentials?
 
@jdweng Given that it works for some parameter values, appears in the sp_who output and does not throw a "Login failed for user" exception, it is hardly about credentials.
 
It depend on what you consider credentials. The database is an MDF file and can get locked. When the MDF file is attached to a server you show never use the MDF file in a connection string. Instead connect to the server to access the database. When the database is attached the Server owns the file and will lock out users from accessing the file. Since the server owns the file when attached I consider this a credential issue.
 
@jdweng It's already been pointed out that the same query with different parameters are working, and that other queries on the site are working, and that we can see the query with sp_who. It's not a credentials problem, and you're continuing to say it is and ignoring what GSerg is saying is just making more noise on an already long comment thread.
 

last day (15 days later) »