【问题标题】:SQL Server SMO programming, where to set the CommandTimeout property?SQL Server SMO编程,CommandTimeout属性在哪里设置?
【发布时间】:2016-05-28 15:06:24
【问题描述】:

我正在使用 SMO 编写一个 PowerShell 脚本来进行一些 SQL Server 编程。下面列出了我的脚本的简化版本。

# Load SMO assembly, and if we're running SQL Server 2008 DLLs load the SMOExtended and SQLWMIManagement libraries
$v = [System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.SqlServer.SMO");

if ((($v.FullName.Split(','))[1].Split('='))[1].Split('.')[0] -ne '9') {
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMOExtended") | out-null;
}
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SmoEnum') | out-null;
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") ".\sql2014";
$server.Databases["master"].ExecuteWithResults("select * from sys.database_files");

我的脚本现在总是超时,我在网上搜索了一下,发现我应该在我的代码中设置CommandTimeout property

但是,CommandTimeout 属性适用于 SqlCommand。我使用 SMO 执行查询的方式没有 SqlCommand 对象。如您所见,我可以直接在$server.Databases["master"] 上执行。

谁能帮忙指出我应该在哪里设置CommandTimeout 属性?

【问题讨论】:

  • 几个样式 cmets - 尝试 import-module SQLPS -DisableNameChecking 而不是所有反射 LoadWithPartialName 的东西。此外,如果您最终要选择数据文件,请实际使用 SMO 模型:数据库具有 FileGroups,而 FileGroups 又具有 Files。数据库也有日志文件。但最终,您希望使用这些事物的对象表示,而不是尝试解析临时查询的输出。

标签: c# sql-server powershell smo


【解决方案1】:

$server.ConnectionContext.StatementTimeout 应该会为您完成工作。按照TechNet这个:

获取或设置语句在因超时错误而失败之前运行的秒数。

【讨论】:

  • 我使用WAITFOR DELAY '00:00:05' 进行了测试,设置$server.ConnectionContext.StatementTimeout 等于1 导致它失败,但设置为6 让它成功;所以该值以秒为单位。
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多