VB.net Connection Timeout

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

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
        Try

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

            dbConn.Open()

            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
            Else
                'Table exists
                DoesSQLTableExist = True
            End If

            dbTbl.Dispose()
            dbConn.Close()
            dbConn.Dispose()

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

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016
Commented:
You need use like below-

dbConn  > your connection object.

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

Open in new window

Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Hi. Thanks. How would I change my code to do that?
Pallavi GodseSr. Digital Marketing Executive
Commented:
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



                Try

                    dbConn.Open()

                    SQLComm.ExecuteNonQuery()

                    dbConn.Close()



                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 BrownASP.net/VBA/VSTO Developer

Author

Commented:
Hi. I am still not sure how to alter my actual code
Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014
Commented:
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.
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial