共用方式為


在 SMO 中設定 SQL Server

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體Azure Synapse Analytics

在 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 設定

程式碼範例會顯示 和 SettingsInformation SQL Server 實例的相關資訊,並修改 和 UserOptions 物件屬性中的 Settings 設定。

在 範例中, 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 設定#

程式碼範例會顯示 和 SettingsInformation SQL Server 實例的相關資訊,並修改 和 UserOptions 物件屬性中的 Settings 設定。

在 範例中, 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 設定

程式碼範例會顯示 和 SettingsInformation SQL Server 實例的相關資訊,並修改 和 UserOptions 物件屬性中的 Settings 設定。

在 範例中, 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."  
}