在 SMO 中配置 SQL Server

适用于:SQL ServerAzure SQL 数据库Azure SQL 托管实例Azure Synapse AnalyticsMicrosoft Fabric SQL 数据库

在 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 C# SMO 项目。

在 Visual Basic 中修改 SQL Server 配置选项

此代码示例说明如何在 Visual Basic .NET 中更新配置选项。 它还检索并显示指定配置选项的最大值和最小值的相关信息。 最后,程序会通知用户更改是否已动态进行,或者是否存储在 SQL Server 实例重新启动之前。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Display all the configuration options.
Dim p As ConfigProperty
For Each p In srv.Configuration.Properties
    Console.WriteLine(p.DisplayName)
Next
Console.WriteLine("There are " & srv.Configuration.Properties.Count.ToString & " configuration options.")
'Display the maximum and minimum values for ShowAdvancedOptions.
Dim min As Integer
Dim max As Integer
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 Then
    Console.WriteLine("Configuration option has been updated.")
Else
    Console.WriteLine("Configuration option will be updated when SQL Server is restarted.")
End If

在 Visual Basic 中修改 SQL Server 设置

该代码示例显示有关 SQL Server InformationSettings实例的信息,并修改对象属性中的SettingsUserOptions设置。

在此示例中,UserOptions 对象和 Settings 对象都有一个 Alter 方法。 您可以分别运行这两个对象的 Alter 方法。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Display information about the instance of SQL Server in Information and Settings.
Console.WriteLine("OS Version = " & srv.Information.OSVersion)
Console.WriteLine("State = " & srv.Settings.State.ToString)
'Display information specific to the current user in UserOptions.
Console.WriteLine("Quoted Identifier support = " & srv.UserOptions.QuotedIdentifier)
'Modify server settings in Settings.

srv.Settings.LoginMode = 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()

在 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."  
}