Enterprise Library 6 Command Timeout

Jun 25, 2013 at 4:53 PM
Hello - I'm new to using the Enterprise Library (using 6) and am using it in an ASP.NET app using VB. I have read the documentation and have searched the web for a basic way to do this but can't find a clear example. This is not the connection timeout - but the command timeout. I have read where many people say if the code takes too long to return the data than it needs to be re-written or you database is not optimized or so on. I understand all of those arguments and am not looking to get into that discussion. I'd just like to know what is the best way to set it in the code. I know it can't be done in a config file. In the past I would have used something like db.CommandTimeout = 60 within the code. I understand the DatabaseFactory is different.

This is a basic block I'm using to view data:
Dim db As Database = DatabaseFactory.CreateDatabase()
Using rs As System.Data.IDataReader = db.ExecuteReader(CommandType.Text, "Select Data from Table")
    While rs.Read()
      {do something}
    End While
End Using
Thanks for any insight
Jun 25, 2013 at 5:08 PM
please use the code below:

objCommand = db.GetStoredProcCommand("procname"), this will give the command object.
objCommand.commandtimeout = 60;
Send this command object to Execute reader method: db.ExecuteReader(objCommand)
Jun 25, 2013 at 5:20 PM
You will have to manually set the CommandTimeout property:
        DatabaseFactory.SetDatabaseProviderFactory(New DatabaseProviderFactory())

        Dim db As Database = DatabaseFactory.CreateDatabase()

        Dim cmd = db.GetSqlStringCommand("Select Data from Table")
        cmd.CommandTimeout = 60 ' 60 seconds

        Using rs As System.Data.IDataReader = db.ExecuteReader(cmd)
            While rs.Read()
                '{do something}
            End While
        End Using

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Jun 25, 2013 at 6:32 PM
Thanks so much to both of you for your quick replies! I used Randy's suggestion and it seems to work perfectly. It's very straightforward and will make adjustments to legacy code very simple.

Thanks again!
Jun 25, 2013 at 8:41 PM
Thanks. Just a quick note that you only need to call:

DatabaseFactory.SetDatabaseProviderFactory(New DatabaseProviderFactory())

once to initialize the block. Usually you would do this at application startup. Didn't want you to think you needed to call it every time. :)

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to