在 SMO 中, Information 对象、 Settings 对象、 UserOptions 对象和 Configuration 对象包含Microsoft SQL Server 实例的设置和信息。
SQL Server 具有许多描述已安装实例行为的属性。 这些属性描述启动选项、服务器默认值、文件和目录、系统和处理器信息、产品和版本、连接信息、内存选项、语言和排序规则选择以及身份验证模式。
SQL Server 配置
对象 Information 属性包含有关 SQL Server 实例的信息,例如处理器和平台。
对象 Settings 属性包含有关 SQL Server 实例的信息。 除了邮件配置文件和服务器帐户之外,还可以修改默认数据库文件和目录。 这些属性在连接期间保持不变。
对象 UserOptions 属性包含有关与算术、ANSI 标准和事务相关的当前连接行为的信息。
还有一组由对象表示 Configuration 的配置选项。 它包含一组属性,这些属性表示可由存储过程修改 sp_configure 的选项。
优先级提升、恢复间隔和网络数据包大小等选项控制 SQL Server 实例的性能。 其中许多选项可以动态更改,但在某些情况下,该值先配置,然后在重新启动 SQL Server 实例时更改。
每个配置选项都有一个 Configuration 对象属性。 ConfigProperty使用对象可以修改全局配置设置。 许多属性的最大值和最小值也存储为 ConfigProperty 属性。 这些属性要求 Alter 该方法将更改提交到 SQL Server 实例。
对象中的所有配置选项 Configuration 必须由系统管理员更改。
例子
对于以下代码示例,必须选择编程环境、编程模板和编程语言来创建应用程序。 有关详细信息,请参阅 在 Visual Studio .NET 中创建 Visual Basic SMO 项目 ,并在 Visual Studio .NET 中创建 Visual C# SMO 项目。
在 Visual Basic 中修改 SQL Server 配置选项
代码示例演示如何在 Visual Basic .NET 中更新配置选项。 它还检索并显示有关指定配置选项的最大值和最小值的信息。 最后,程序会通知用户更改是否已动态进行,或者是否存储在 SQL Server 实例重新启动之前。
在 Visual Basic 中修改 SQL Server 设置
该代码示例显示有关 SQL Server InformationSettings实例的信息,并修改对象属性中的SettingsUserOptions设置。
在示例中, UserOptions 对象和 Settings 对象都有一个 Alter 方法。 可以单独运行 Alter 这些方法。
在 Visual C 中修改 SQL Server 设置#
该代码示例显示有关 SQL Server InformationSettings实例的信息,并修改对象属性中的SettingsUserOptions设置。
在示例中, UserOptions 对象和 Settings 对象都有一个 Alter 方法。 可以单独运行 Alter 这些方法。
//Connect to the local, default instance of SQL Server.
{
Server srv = new Server();
//Display all the configuration options.
foreach (ConfigProperty p in srv.Configuration.Properties)
{
Console.WriteLine(p.DisplayName);
}
Console.WriteLine("There are " + srv.Configuration.Properties.Count.ToString() + " configuration options.");
//Display the maximum and minimum values for ShowAdvancedOptions.
int min = 0;
int max = 0;
min = srv.Configuration.ShowAdvancedOptions.Minimum;
max = srv.Configuration.ShowAdvancedOptions.Maximum;
Console.WriteLine("Minimum and Maximum values are " + min + " and " + max + ".");
//Modify the value of ShowAdvancedOptions and run the Alter method.
srv.Configuration.ShowAdvancedOptions.ConfigValue = 0;
srv.Configuration.Alter();
//Display when the change takes place according to the IsDynamic property.
if (srv.Configuration.ShowAdvancedOptions.IsDynamic == true)
{
Console.WriteLine("Configuration option has been updated.");
}
else
{
Console.WriteLine("Configuration option will be updated when SQL Server is restarted.");
}
}
在 PowerShell 中修改 SQL Server 设置
该代码示例显示有关 SQL Server InformationSettings实例的信息,并修改对象属性中的SettingsUserOptions设置。
在示例中, UserOptions 对象和 Settings 对象都有一个 Alter 方法。 可以单独运行 Alter 这些方法。
# Set the path context to the local, default instance of SQL Server.
CD \sql\localhost\
$srv = Get-Item default
#Display information about the instance of SQL Server in Information and Settings.
"OS Version = " + $srv.Information.OSVersion
"State = "+ $srv.Settings.State.ToString()
#Display information specific to the current user in UserOptions.
"Quoted Identifier support = " + $srv.UserOptions.QuotedIdentifier
#Modify server settings in Settings.
$srv.Settings.LoginMode = [Microsoft.SqlServer.Management.SMO.ServerLoginMode]::Integrated
#Modify settings specific to the current connection in UserOptions.
$srv.UserOptions.AbortOnArithmeticErrors = $true
#Run the Alter method to make the changes on the instance of SQL Server.
$srv.Alter()
在 PowerShell 中修改 SQL Server 配置选项
代码示例演示如何在 Visual Basic .NET 中更新配置选项。 它还检索并显示有关指定配置选项的最大值和最小值的信息。 最后,程序会通知用户更改是否已动态进行,或者是否存储在 SQL Server 实例重新启动之前。
#Get a server object which corresponds to the default instance replace LocalMachine with the physical server
cd \sql\LocalMachine
$svr = Get-Item default
#enumerate its properties
foreach ($Item in $Svr.Configuration.Properties)
{
$Item.DisplayName
}
"There are " + $svr.Configuration.Properties.Count.ToString() + " configuration options."
#Display the maximum and minimum values for ShowAdvancedOptions.
$min = $svr.Configuration.ShowAdvancedOptions.Minimum
$max = $svr.Configuration.ShowAdvancedOptions.Maximum
"Minimum and Maximum values are " + $min.ToString() + " and " + $max.ToString() + "."
#Modify the value of ShowAdvancedOptions and run the Alter method.
$svr.Configuration.ShowAdvancedOptions.ConfigValue = 0
$svr.Configuration.Alter()
#Display when the change takes place according to the IsDynamic property.
If ($svr.Configuration.ShowAdvancedOptions.IsDynamic -eq $true)
{
"Configuration option has been updated."
}
Else
{
"Configuration option will be updated when SQL Server is restarted."
}