Powershell SQL 服务器数据库连接和连接超时问题
Powershell SQL server database connectivity and connection timeout issue
我有一个连接到 SQL 服务器 2012 数据库的 powershell 脚本 运行 一个 SQL 查询和结果集到数据 table 中以将格式化的电子邮件发送给相关方.以下是问题所在的代码片段:
$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)
我遇到以下错误 运行 脚本:
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."
我已经尝试增加 SqlConnection 字符串的超时时间,从最初设置的 360 逐渐增加到现在的 800,但仍然有同样的问题。有没有人深入了解这里到底是什么问题?我怎样才能克服它?
提前致谢。
如 OP 所述 - 默认命令执行超时为 30 秒。我在下面找到:
SqlDataAdapter class
这将允许您增加命令执行超时(插入、更新、删除、select 命令)。所以在我下面的例子中做了诀窍:
$adapter.SelectCommand.CommandTimeout=60
希望对您有所帮助。
除了 Zulfiqar 的回答,它引导我朝着正确的方向前进。对我来说,解决方案是在 SqlCommand 中。设置适配器的 属性 时出现的错误如下:
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;
我有一个连接到 SQL 服务器 2012 数据库的 powershell 脚本 运行 一个 SQL 查询和结果集到数据 table 中以将格式化的电子邮件发送给相关方.以下是问题所在的代码片段:
$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)
我遇到以下错误 运行 脚本:
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."
我已经尝试增加 SqlConnection 字符串的超时时间,从最初设置的 360 逐渐增加到现在的 800,但仍然有同样的问题。有没有人深入了解这里到底是什么问题?我怎样才能克服它?
提前致谢。
如 OP 所述 - 默认命令执行超时为 30 秒。我在下面找到:
SqlDataAdapter class
这将允许您增加命令执行超时(插入、更新、删除、select 命令)。所以在我下面的例子中做了诀窍:
$adapter.SelectCommand.CommandTimeout=60
希望对您有所帮助。
除了 Zulfiqar 的回答,它引导我朝着正确的方向前进。对我来说,解决方案是在 SqlCommand 中。设置适配器的 属性 时出现的错误如下:
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;