I want to get a person from database in C# with ADO.net.
I use this command:
System.Data.Common.DbCommand command = DatabaseManager.Database.GetStoredProcCommand(proc_name, firstName);
and then
object personID = DatabaseManager.Database.ExecuteScalar(command);
When I click a button, these commands calls two times.
In the first time it is OK. But the second time, I get TimeOutException:
Timeout expired. The timeout period elapsed prior to completion of
the operation or the server is not responding.
I used command.Disposed(); and command.Connection.Close();. But these not resolve my problem.
Please help me how I can resolve this problem.
It seems that some transaction is left open during your first execution of the procedure. This can be either by an error in your procedure or bad connection management in your application.
Related
I have a windows form application written in C# that passes a query to a SQL Server database and then displays the results in a dataviewgrid. The query that is passed to the database depends on the option selected in the form.
One particular query takes a little over a minute to run in management studio, but timeouts when it is passed to the database from the program. here are the steps I have done to try to resolve the situation:
Added a 5 minute timeout in the program by setting the connection timeout option to 300 seconds in the sql connection string. Example: Data Source=ab;Initial Catalog=abc;User ID=user; Password =pw; Connection Timeout=300
Setting the remote query timeout in the SQL Server instance to 0 (meaning, no timeout). Example: EXEC sp_configure 'remote query timeout', 0 ; GO
Neither of these options work. Despite implementing both of them, the c# program throws back a sql timeout error after less than a minute.
Is there a workaround for this? I have searched for this topic on stack overflow and so far all of the suggestions have been to do either 1 or 2 (which I have done).
For reference, I am using Visual Studio 17 Community edition and SQL Server 2016 Developer edition.
Any help would be greatly appreciated.
Thanks!
There's a "CommandTimeout" property on the SQL command. Try setting that.
Connection timeout and command timeout are two different things.
A connection timeout occurs when a connection cannot be retrieved from the connection pool within the allotted timeout period.
A command timeout occurs when a connection has been retrieved, but the query being executed against it doesn't return results within the allotted command timeout period. The default command timeout period in ADO.NET is 30 seconds.
If you've set the connection timeout to 300 seconds and you're still getting a timeout, it's likely a command timeout. As walkers01 said, set your command timeout to a suitable number of seconds. 300 seconds should be far more than sufficient; if your query executes in one minute in SSMS, a timeout of 90 seconds should suffice.
Try this for unlimited time of query execution if you are using SqlDataAdapter. I have use this one, solved my problem.
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
dscmd.SelectCommand.CommandTimeout = 0;
In my Windows application, I use SQL Server 2008. My database size is 5086080 KB. Now I get the error as timeout expired when saving a transaction by using a stored procedure. So I set command timeout to 1200. It works fine. But I think it shouldn't because insert data have 2 or 3 lines. Is there any other way to solve this problem?
This is detail error message:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding .
Timeout is entirely on how long the actual SQL command is likely to take.
For example example, most of our commands run sproc's that should take no longer than 30 seconds to complete, however there are a couple that run for much longer, which means they have their own high valued timeouts.
You'll need to profile how long on average your routine takes then adjust the timeout accordingly, and remember to leave room for variables like latency etc
you need to profile your sql query and your code at every step. only then will you be able to know the exact bottleneck in your program.
Is somebody else keeping a transaction open that is holding up your query? Run sp_who or sp_who2 on the server to see what else is running.
I have 1 store procedure to generate some report, its is very complex so it is taking upto 7-8 mins sometimes to generate output.
When i am trying to access from the Webpage ( C# ) i am getting connection time out error.
I have already set remote connection timeout=0 (unlimited) and in connection string also i have tried to supply connection timeout.
I have suppose 6 Lacs around records of bills and i have performing 6 times around sum based on different groups and different dates, so is there any solution to make it faster.
Or any connection timeout workout?
I think the problem with page request timeout. I suppose you start stored procedure execution right on Page_Load event and after some time IIS close request by timeout.
I'm suggesting to you remove load function from Page_Load event and after page loaded send AJAX request to server or page to start stored procedure execution and check execution result from time to time. When result will be ready you can get it by AJAX and display to user.
Is it really connection timeout that you should be worrying about?
Since you have a long-running command, please ensure that you set CommandTimeout on your SqlCommand to 0.
Let's say I have a query that is sent to my SQL-Server database, it takes more than 30 seconds, and my program throws an SQL Query Timeout exception. Is the query still chugging along on my database or does it get terminated as soon as the exception is thrown?
A client signals a query timeout to
the server using an attention event.
An attention event is simply a
distinct type of TDS packet a SQL
Server client can send to it. In
addition to connect/disconnect, T-SQL
batch, and RPC events, a client can
signal an attention to the server. An
attention tells the server to cancel
the connection's currently executing
query (if there is one) as soon as
possible. An attention doesn't
rollback open transactions, and it
doesn't stop the currently executing
query on a dime -- the server aborts
whatever it was doing for the
connection at the next available
opportunity. Usually, this happens
pretty quickly, but not always.
Source There's no such thing as a query timeout...
When the client decides that the command has run long enough, it issues an "Abort". The query simply stops running in the database.
Any CATCH block won't be hit, transactions will be left open and locks can still remain allocated after this, even if the connection is closed because "close" means "return to connection pool".
If you expect a lot of Command Timeouts then consider using SET XACT_ABORT ON (and this too) that will release locks and rollback transactions. or fix the code...
Before executing a query, SQL Server
estimates how much memory it needs to
run and tries to reserve this amount
of memory from the buffer pool. If the
reservation succeeds the query is
executed immediately. If there is not
enough memory readily available from
the buffer pool, then the query is put
into a queue with a timeout value,
where the timeout value is guided by
the query cost. The basic rule is:
higher the estimated cost is, larger
the time out value is. When the
waiting time of this query exceeds the
timeout value, a time out error is
thrown and the query is removed from
the queue.
Source
If you get a SQL timeout then SQL has stopped, however web applications can time out and the SQL query can continue.
Usually when it times out it means the connection has died, meaning the query has not been sent to the database, most database support Transactions where you can start a transaction, run your queries, and if your happy you can commit them.
Example:
BEGIN TRAN
UPDATE authors
SET au_fname = 'John'
WHERE au_id = '172-32-1176'
UPDATE authors
SET au_fname = 'Marg'
WHERE au_id = '213-46-8915'
COMMIT TRAN
I'm doing a basic select against a view. Unfortunately the result can be slow and I'm getting timeout errors intermittently. How can I increase the timeout?
Using .NET 3.5, Sql Server 2000, Linq2Entities
I'm using the very basic query List<MyData> result = db.MyData.Where(x.Attribute == search).ToList();
Fixing the query so that it's faster on the DB side is not an option here.
Exact Error: "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."
Update: I'd prefer to just change it for this one query.
You can set the timeout in your connection string.
Edit (new):
It turns out that there are two different timeout concepts. The connection timeout is used to determine wether or not a connection can be established. The CommandTimeout property on the object context controls timeout for commands. So just set that to a high value, and it should not influence the short-running queries in any way.