How to extend the timeout of a SQL query

2020-06-06 17:29发布

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.

6条回答
做个烂人
2楼-- · 2020-06-06 17:55

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();
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-06-06 18:02

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();
查看更多
虎瘦雄心在
4楼-- · 2020-06-06 18:04

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

查看更多
冷血范
5楼-- · 2020-06-06 18:16

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);
查看更多
相关推荐>>
6楼-- · 2020-06-06 18:17

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.

查看更多
7楼-- · 2020-06-06 18:18

you do this by setting the SqlCommand.CommandTimeout property

查看更多
登录 后发表回答