共用方式為


在 SMO 中設定 SQL Server

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

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

在範例中, UserOptions 對象和 Settings 物件都有 Alter 方法。 您可以個別執行 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."  
 }