VB.net Connection Timeout


How would I apply a connection Timeout of 30 seconds to the following code

    Public Function DoesSQLTableExist(ByVal tblName As String, ByVal cnnStr As String) As Boolean

            ' Open connection to the database
            Dim dbConn As New SqlConnection(cnnStr)


            Dim restrictions(3) As String
            restrictions(2) = tblName
            Dim dbTbl As DataTable = dbConn.GetSchema("Tables", restrictions)

            If dbTbl.Rows.Count = 0 Then
                'Table does not exist
                DoesSQLTableExist = False
                'Table exists
                DoesSQLTableExist = True
            End If


        Catch ex As Exception
            MsgBox(ex.Message & " hhr5")
        End Try
    End Function

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
You need use like below-

dbConn  > your connection object.

SQLCmd As New SqlClient.SqlCommand("yourSQLQuery", dbConn )
SQLCmd.CommandTimeout = 20

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. Thanks. How would I change my code to do that?
Pallavi GodseSr. Digital Marketing ExecutiveCommented:
It's not ignoring your connection timeout... rather, its obeying the SqlCommand.CommandTimeout property.  The ConnectionTimeout only applies to when the connection is opening, not for the query itself.

Add the following line to your code...

Public Sub SQLCommand(ByVal dbConnStr As String, ByVal SQLCommQry As String, _

       Optional ByVal ErrorMessage As String = "Data Write Failed!")

        Using dbConn As New SqlClient.SqlConnection(dbConnStr)

            Using SQLComm As New SqlClient.SqlCommand(SQLCommQry, dbConn)

                SQLComm.CommandTimeout = 0





                Catch ex As Exception

                    MsgBox(ErrorMessage, MsgBoxStyle.OkOnly)

                End Try

            End Using

        End Using

    End Sub

Open in new window

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. I am still not sure how to alter my actual code
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You would have to modify your cnnStr value to include this:

Connect Timeout=30

For example:

cnnStr = "Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername;
Pwd=myPassword;Connect Timeout=30"

So wherever you're building that "cnnStr" value, modify that to include the Connect Timeout argument.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.