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?
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.
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?
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. I am still not sure how to alter my actual code
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.