How to extend the timeout of a SQL query

前端 未结 6 1827
没有蜡笔的小新
没有蜡笔的小新 2021-02-11 14:21

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

6条回答
  •  被撕碎了的回忆
    2021-02-11 14:47

    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();
    

提交回复
热议问题