VB.net Connection Timeout

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

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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

0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. Thanks. How would I change my code to do that?
0
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



                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

0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. I am still not sure how to alter my actual code
0
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.
0

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
0
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.