Powershell SQL server database connectivity and connection timeout issue

*爱你&永不变心* 提交于 2021-02-07 20:51:38

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/47073578/powershell-sql-server-database-connectivity-and-connection-timeout-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!