How can solve connection time out error in c#? - c#

I'm new to C# and SQL Server; I wrote a simple stored procedure in T-SQL, and in C# with under code call that:
da = new SqlDataAdapter("select_equals_Cycle", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
ds = new DataSet();
da.Fill(ds, "select_equals_Cycle");
but on this line:
da.Fill(ds, "select_equals_Cycle");
I get this error:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
My connection string this:
string conn = "Data Source=.;Initial Catalog=ClubEatc;Integrated Security=True;Connect Timeout=30;";
How can I solve that? thanks.

Use CommandTimeout or optimize the StoredProcedure
da.SelectCommand.CommandTimeout = 180; // default is 30 seconds

Don't set the timeout value, if you don't know how to configure it.
Microsoft make the optional has well defined value.
the Timeout question has two items you have to check out.
Database:
e.g. MSSQL
--the code below ran in Query of Sql Server
--this code snippet will show you all the advance options
Exec sp_configure 'show advanced options',1
recogfigure
-- this code snippet configure the seconds of the query wait
-- this configuration means that if the memory couldn't used to query -- the big query, sql server will wait for some seconds
-- this option usually use the default value made by Microsoft
-- the default value about how many seconds of formula is the estimated query -- -- time multiply by 25, and if the memory still couldn't used for querying.
-- then throw TimeOut Exception
Exec sp_configure 'query wait', 200;
ReCONFIGURE
-- the end
// the timeout configuration about C#
SqlConnection.ConnectionTimeout = 200
// here is the Document:
// get the time to wait while trying to establish a connection before
// terminating the attempt and generating and error
// the Unit of the property is second
// 0 is no limit
after you scan the code snippet, you will find two reason can cause the exception.
your code couldn't establish the connection.
your memory in the machine install sql server couldn't used to run the big query

Related

Why is timeout occurring while executing a stored procedure in C# even after increasing the commandtimeout property?

I have a piece of code that executes stored procedure.i have given a "Waitfor Delay '00:05' " in the SP for the purpose of testing the timeout in my C# code wherein my commandtimeout is set for 10 mins. While debugging, after 20 seconds or so when the executereader for the SP is executed,I am getting a timeout .
Stuck on this for 2 days, any help is much appreciated.
Give ConnectionTimeout as 0, it means undefined waiting time.And one more thing , u can debug the SP using EXEC command in sql server itself and check where the time taking in code or in SP.
Connection Timout in code
SqlDataAdapter da = new SqlDataAdapter(Query, ConnectionString);
da.SelectCommand.CommandTimeout = 0;
Execute SP in sql server
EXEC procedure_name;
Regards
Aravind

SqlCommand Times Out, CommandTimeout and ConnectionTimeout both = 0

I'm running a large number of extracts from a SQL database via a C# Script Task within a SSIS package. The connection to the source is acquired from a connection manager within the package:
object rawConnection = Dts.Connections[sqlSpecItems["ConnectionManager"]].AcquireConnection(Dts.Transaction);
SqlConnection connectionFromCM = (SqlConnection)rawConnection;
(splSpecItems is a Dictionary object that supplies the name of the connection manager to use)
The ConnectionTimeout property of the connection manager is set to 0. The connection string generated for the CM is:
Data Source=MyDatabase;User ID=MyUserName;Initial Catalog=MyDatabaseName;Persist Security Info=True;Asynchronous Processing=True;Connect Timeout=0;Application Name=MyPackageApplicationName;
The connection is used to return a SqlDataReader object as follows:
private SqlDataReader GetDataReaderFromQuery(string sqlQueryToExecute)
{
// connect to server
SqlConnection sqlReaderSource = GetSourceSQLConnection();
// create command
SqlCommand sqlReaderCmd = new SqlCommand(sqlQueryToExecute, sqlReaderSource)
{
CommandType = CommandType.Text,
CommandTimeout = 0
};
// execute query to return data to reader
SqlDataReader sqlReader = sqlReaderCmd.ExecuteReader();
return sqlReader;
}
The operation fails with the error message:
Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Logged start and end times for the operation are typically around 50s apart, but can be up to 120s.
The source database is a Business Central cloud hosted SQL db. The failures occur on a small number of specific extracts, the larger ones (although by no means large in absolute terms, c20k rows). When attempting to query these via SSMS, there is typically a delay as the data has to be fetched from the source into memory, which I suspect to be the reason for the timeout (notwithstanding the setting of timeout = 0 for the connection and command). Note that once the failure has happened once, the data is in memory and so it doesn't repeat if I restart the job.
I've looked at various answers on this site, as well as across other sites. Nothing I have seen so far has given me any idea of what I might try to resolve this problem. Any help would be appreciated.
So, I found the problem. Just in case it helps anyone else, the timeout wasn't throwing because of the reader. Rather, it was the SqlBulkCopy operation downstream of it that I pass the reader into. Adding:
bulkCopy.BulkCopyTimeout = 0;
Has cleared the problem...

Getting SQL Server timeout exception even with command timeout set to 0

I have set the command timeout to 0 as per the documentation in SQL Server. I'm indexing a large table, and still get an exception "Execution timeout expired". The timeout period elapsed prior to completion of the operation or the server is not responding. The server is responding as I watch it though the SQL Server Monitor.
Here is the pertinent code:
private void ExecuteQuery(string qStr)
{
using (SqlConnection cnx = new SqlConnection(_ConnectionString))
{
cnx.Open();
using (SqlCommand cmd = new SqlCommand(qStr, cnx))
{
cmd.CommandTimeout = 0;
cmd.ExecuteNonQuery();
}
}
}
This is the connection string
Data Source='tcp:aplace.database.windows.net,1433';Initial Catalog='SQL-Dev';User Id='user#place';Password='password';Connection Timeout=360
Why am I getting a execution timeout? I have the connection timeout set to 7200 seconds, also. I am indexing a 31 million rows table on one column.
First, connection timeout and command timeout are not the same thing. Make sure you understand the difference and are using them correctly.
Second, if this is from a web page, you also need to consider timeout values relating to the web server, etc.
Third, to verify that it is in fact a timeout issue, execute your index statement in SSMS and find out how long it takes. Since the actual indexing takes place on the SQL server no matter where it is called from, the indexing time should be roughly equal whether running from SSMS or your application.

The wait operation timed out in asp.net mvc 4

I have a table contains 100 columns and 2,000,000 records.
When I am fetching records using stored procedure from that sometimes I am getting "The wait operation timed out." error.When I alter the stored procedure and try to fetch the records, it works fine.
Can anyone let me know what is the best solution for this ?
As vishal Naik has mentioned, SQL server has a default query timeout setting of 30 seconds. A possible solution would be to manually increase this time for a given query and this, while not recommended, should be effective. The code is as follows:
SqlCommand cmd = new SqlCommand(commandText, conn);
cmd.CommandTimeout = 60; // or any other length of time in seconds
/*Any other properties to be modified in the command will come here*/
SqlDataReader dataReader = cmd.ExecuteReader();
SQL server has default query timeout setting of 30 seconds.
For more details click here

How to increase the connect timeout on a SqlCommand?

I have around 7000 rows in excel and I want send them to another local computer with linked server. I increased connect timeout in my connections but I get the same time again with no result.
SqlCommand cmd9 = new SqlCommand("insert into [" + ConString + "].Database.dbo.ExpressE select * from ExpressE", conn);
cmd9.ExecuteNonQuery();
Error message is:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
The statement has been terminated.
The SqlCommand has a property CommandTimeout that by default is set to 30 seconds. You can set it to a higher number of seconds, even set it to 0 if you want the command to wait forever if needed. The link to the MSDN includes an example about how to use that property.

Categories

Resources