Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell SQL server database connectivity and connection timeout issue

I've a powershell script connecting to SQL server 2012 database running a SQL query and result set into data table to send formatted email to relevant parties. Below is the code snippet where issue is:

$CBA = New-Object System.Data.DataSet "CBAData"
$sqlConn = New-Object System.Data.SqlClient.SqlConnection("Data Source=DataSource;Initial Catalog=DataCatalog;Integrated Security = False;Connection Timeout=800;User ID = user; Password =pwd;")
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter($CBAData, $sqlConn)
$adapter.Fill($CBA)

I am getting below error running the script:

Exception calling "Fill" with "1" argument(s): "Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding."

I've tried increasing timeoutin SqlConnection string from initially set up 360 gradually and now upto 800 but still having same issue. Does anyone throw insight into what exactly issue is here? and How can I overome it?

Thank you in advance.

like image 636
Zulfiqar Dholkawala Avatar asked Nov 02 '17 10:11

Zulfiqar Dholkawala


People also ask

How do I fix SQL Server connection timeout?

If you encounter a connection-timeout error, follow the steps: Increase the connection-timeout parameter. If you use an application to connect to SQL Server, increase the relevant connection-timeout parameter values and check whether the connection eventually succeeds.

How do I fix query timeout expired?

You need to configure timeout parameter in SQL server settings as shown on the screenshot. 'Remote query timeout' parameter must be set in 0 instead of 600.

What is CommandTimeout in connection string?

The time in seconds to wait for the command to execute. The default is 30 seconds.

What causes SQL Server timeout?

SQL Server will typically show you connection timeouts or operation (query) timeouts. These values are set by the client connecting to the SQL Server. An operation timeout occurs when a command takes too long to complete, and the client raises an error.


2 Answers

As mentioned by OP - default command execution timeout is 30 seconds. I found below within:

SqlDataAdapter class

that would allow you to increase command execution timeout (insert, update, delete, select command). So in my case below did the trick:

$adapter.SelectCommand.CommandTimeout=60

Hope this helps.

like image 73
Zulfiqar Dholkawala Avatar answered Oct 03 '22 15:10

Zulfiqar Dholkawala


In addition to Zulfiqar's answer, which led me in the right direction. For me, the solution was in the SqlCommand. The error I was getting when setting the property of the adapter was as follows:

The property 'CommandTimeout' cannot be found on this object. Verify that the property exists and can be set.

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
$SqlCmd.CommandTimeout = 60;
like image 37
Bernard Moeskops Avatar answered Oct 03 '22 14:10

Bernard Moeskops