patterncsharpMinor
Timeout expired SqlException for complex Procedure
Viewed 0 times
sqlexceptionexpiredproceduretimeoutforcomplex
Problem
I use SQL Server 2008 R2 and ASP.NET 4.5.
Sometimes, I get this error when I executed a complex procedure:
System.Data.SqlClient.SqlException
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding
Any suggestions for troubleshooting or checklist (steps for improvements SQL)?
My .NET code:
SQL Server Procedure:
```
ALTER PROCEDURE [dbo].[SP_CalendarQuery]
@DateStart datetime = NULL,
@DateEnd datetime = NULL,
@Subject varchar(1000) = NULL,
@Area int = NULL,
@TypeAlert int = NULL,
@Tomador varchar(50) = NULL,
@UserQuery int = NULL,
@CarteraUserQueQuery int = null
AS
BEGIN
DECLARE @intTypeRol INT
DECLARE @intTypeMediador INT
DECLARE @intTypeAsociacion INT
IF @UserQuery IS NOT NULL
begin
SELECT
@intTypeRol = FK_ID_ROL_PORTAL,
@intTypeMediador = FK_ID_Type_MEDIADOR,
@intTypeAsociacion = ASOCIACION
FROM [AccessRoles.Users]
WHERE FK_ID_DATOS_PERSONALES = @UserQuery
END
ELSE
BEGIN
SET @intTypeRol = NULL
SET @intTypeMediador = NULL
SET @intTypeAsociacion = NULL
END
SELECT C.[ID_Alert]
,C.[Empresa]
,C.[Subject]
,C.[Date_Star
Sometimes, I get this error when I executed a complex procedure:
System.Data.SqlClient.SqlException
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding
Any suggestions for troubleshooting or checklist (steps for improvements SQL)?
My .NET code:
return Translate(SqlDbHelper.ExecuteProcedure("SP_CalendarQuery", pfechaAlerta, pfechaAlertaFin, pAsunto, pidArea, pidTipoAlerta, pTomador, pidUsuarioConsulta, pCartera));
public static DataTable ExecuteProcedure(string sql, params SqlParameter[] listParams)
{
var dt = new DataTable();
using (var conn = new SqlConnection(ConnectionString))
{
using (var command = new SqlCommand(sql, conn))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddRange(listParams);
using (var dataAdapter = new SqlDataAdapter(command))
{
dataAdapter.Fill(dt);
}
}
}
return dt;
}SQL Server Procedure:
```
ALTER PROCEDURE [dbo].[SP_CalendarQuery]
@DateStart datetime = NULL,
@DateEnd datetime = NULL,
@Subject varchar(1000) = NULL,
@Area int = NULL,
@TypeAlert int = NULL,
@Tomador varchar(50) = NULL,
@UserQuery int = NULL,
@CarteraUserQueQuery int = null
AS
BEGIN
DECLARE @intTypeRol INT
DECLARE @intTypeMediador INT
DECLARE @intTypeAsociacion INT
IF @UserQuery IS NOT NULL
begin
SELECT
@intTypeRol = FK_ID_ROL_PORTAL,
@intTypeMediador = FK_ID_Type_MEDIADOR,
@intTypeAsociacion = ASOCIACION
FROM [AccessRoles.Users]
WHERE FK_ID_DATOS_PERSONALES = @UserQuery
END
ELSE
BEGIN
SET @intTypeRol = NULL
SET @intTypeMediador = NULL
SET @intTypeAsociacion = NULL
END
SELECT C.[ID_Alert]
,C.[Empresa]
,C.[Subject]
,C.[Date_Star
Solution
This should be a simple matter of setting the SqlCommand.CommandTimeout property:
This is the number of seconds to wait before throwing an error. The default is 30 seconds.
command.CommandTimeout = 300; // 5 minutesThis is the number of seconds to wait before throwing an error. The default is 30 seconds.
Code Snippets
command.CommandTimeout = 300; // 5 minutesContext
StackExchange Database Administrators Q#122773, answer score: 2
Revisions (0)
No revisions yet.