43

This is not a connection timeout as a connection to the database is made fine. The problem is that the stored procedure that I'm calling takes longer than, say, 30 seconds and causes a timeout.

The code of the function looks something like this:

SqlDatabase db = new SqlDatabase(connectionManager.SqlConnection.ConnectionString);
return db.ExecuteScalar(Enum.GetName(typeof(StoredProcs), storedProc), parameterValues);

The ExecuteScalar call is timing out. How can I extend the timeout period of this function?

For quick stored procedures, it works fine. But, one of the functions takes a while and the call fails. I can't seem to find any way to extend the timeout period when the ExecuteScalar function is called this way.

1
  • 6
    OK, downvoting my question is just rude. My question is clearly defined and (hopefully) has an answer.
    – BoltBait
    Jul 10, 2009 at 21:06

6 Answers 6

37

If you are using the EnterpriseLibrary (and it looks like you are) try this:

 Microsoft.Practices.EnterpriseLibrary.Data.Database db = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("ConnectionString");
 System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("StoredProcedureName");
 cmd.CommandTimeout = 600;
 db.AddInParameter(cmd, "ParameterName", DbType.String, "Value");

 // Added to handle paramValues array conversion
 foreach (System.Data.SqlClient.SqlParameter param in parameterValues) 
 {
     db.AddInParameter(cmd, param.ParameterName, param.SqlDbType, param.Value);
 }

 return cmd.ExecuteScalar();

Edited to handle the paramValues array directly based on the comments. I also included your ConnectionString value:

Microsoft.Practices.EnterpriseLibrary.Data.Database db = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase(connectionManager.SqlConnection.ConnectionString);
System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("StoredProcedureName", parameterValues);
cmd.CommandTimeout = 600;
return cmd.ExecuteScalar();
9
  • Hmmm... I don't have access to the ParameterNames. The parameterValues variable in my sample code is defined as "object[] parameterValues". Can I still add the parameters without knowing the names only the order?
    – BoltBait
    Jul 10, 2009 at 21:27
  • I added code that should convert your paramValues array into the parameters expected by the DbCommand. Jul 10, 2009 at 21:44
  • No need. You just add it to the GetStoredProcCommand command.
    – BoltBait
    Jul 10, 2009 at 21:48
  • Nice find, edited to show "proper" solution. Did this solve your issue? Jul 10, 2009 at 21:55
  • I'm not sure yet. I just made the changes to test, but it isn't working. I'm still tinkering and will report back when solved.
    – BoltBait
    Jul 10, 2009 at 22:02
25

you do this by setting the SqlCommand.CommandTimeout property

5
  • 3
    Which would work great if I was using a SqlCommand... but, I'm not.
    – BoltBait
    Jul 10, 2009 at 20:36
  • 3
    Yes, you are. SqlDatabase isn't part of the standard data provider; it's a wrapper class that someone has written, and it will use an SqlCommand object internally. Jul 10, 2009 at 20:43
  • 3
    The "someone" may be Microsoft, though -- is this the SQLDatabase class from Microsoft.Practices.EnterpriseLibrary? Jul 10, 2009 at 20:52
  • @Joel, that function is defined in a file called Microsoft.Practices.EnterpriseLibrary.Data.dll Hmmm... not sure if I have the source code for that. Searching...
    – BoltBait
    Jul 10, 2009 at 20:57
  • Rewrite it to use db.CreateConnection(); the you'll have 4-5 extra lines of code, but easy access to the SqlCommand object.
    – nos
    Jul 10, 2009 at 21:10
7

I think this might be a better way to do this (as of Enterprise Library 6.0):

SqlDatabase db = new SqlDatabase(connectionManager.SqlConnection.ConnectionString);
System.Data.Common.DbCommand cmd = db.GetStoredProcCommand(storedProc, parameterValues);
cmd.CommandTimeout = 600;
return db.ExecuteScalar(cmd);
1
  • thank you buddy this save my lots of hours. very much appreciated
    – user5308950
    May 2, 2016 at 7:02
4

Try this one

SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(connection.ConnectionString);
connectionStringBuilder.ConnectTimeout = 180;
connection.ConnectionString = connectionStringBuilder.ConnectionString;

connection.Open();
SqlCommand command = new SqlCommand("sp_ProcedureName", connection);
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = connection.ConnectionTimeout;
command.ExecuteNonQuery();
connection.Close();
0

Mladen is right but if you have to do this you probably have a bigger problem with the proc itself. Under load it might take much longer than your new timeout. Might be worth spending some quality time with the proc to optimize.

2
  • 1
    Thanks for telling me to go optimize my application. Sigh. In the long run that may happen, but for TODAY I just need to get this thing running.
    – BoltBait
    Jul 10, 2009 at 20:39
  • I certainly meant no offense. It's just that if SQL is timing out, it is unlikely you'll be able to find a timeout # that will work in every situation. You WANT SQL to time out in many cases and the default is still pretty long.
    – n8wrl
    Jul 13, 2009 at 11:31
0

Timeout can occur due to a Microsoft issue. Seems to still occur on my Windows 8 system.

http://support.microsoft.com/kb/2605597?wa=wsignin1.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.