Share via


SMO での SQL Server の構成

SMO では、Information オブジェクト、Settings オブジェクト、UserOptions オブジェクト、および Configuration オブジェクトを使用して、Microsoft SQL Server のインスタンスの設定および情報を構成できます。

SQL Server には、インストールされたインスタンスの動作を記述する複数のプロパティがあります。これらのプロパティでは、スタートアップ オプション、サーバーの既定、ファイルとディレクトリ、システムとプロセッサの情報、製品とバージョン、接続情報、メモリ オプション、言語と照合順序の選択、および認証モードについて記述します。

SQL Server 構成

Information オブジェクト プロパティには、プロセッサやプラットフォームなど、SQL Server のインスタンスに関する情報が含まれています。

Settings オブジェクト プロパティには、SQL Server のインスタンスに関する情報が含まれています。Mail Profile および Server Account に加え、既定のデータベース ファイルおよびディレクトリも変更することができます。これらのプロパティは、接続が続いている間は保持されます。

UserOptions オブジェクト プロパティには、算術、ANSI 規格、およびトランザクションに関連する現在の接続の動作に関する情報が含まれています。

また、Configuration オブジェクトで表現される構成オプションのセットもあります。これには、sp_configure ストアド プロシージャによって変更可能なオプションを表すプロパティのセットが含まれています。priority boostrecovery intervalnetwork packet sizeなどのオプションは、SQL Server のインスタンスのパフォーマンスを制御します。これらのオプションの多くは動的に変更できますが、場合によっては、構成した値が SQL Server のインスタンスの再起動時に変更されることもあります。

構成オプションごとに Configuration オブジェクト プロパティがあります。ConfigProperty オブジェクトを使用すると、グローバル構成設定を変更することができます。多くのプロパティには、最大値および最小値が設定されており、これらも ConfigProperty プロパティとして格納されます。これらのプロパティには、SQL Server のインスタンスへの変更をコミットする Alterメソッドが必要です。

Configuration オブジェクトの構成オプションの変更はすべて、システム管理者が行う必要があります。

次のコード例では、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。詳細については、「Visual Studio .NET で Visual Basic SMO プロジェクトを作成する方法」および「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 設定の変更

コード例では、Information および Settings 内の SQL Server インスタンスについての情報が表示され、Settings オブジェクト プロパティおよび UserOptions オブジェクト プロパティの設定が変更されます。

この例では、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 設定の変更

コード例では、Information および Settings 内の SQL Server インスタンスについての情報が表示され、Settings オブジェクト プロパティおよび UserOptions オブジェクト プロパティの設定が変更されます。

この例では、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 設定の変更

コード例では、Information および Settings 内の SQL Server インスタンスについての情報が表示され、Settings オブジェクト プロパティおよび UserOptions オブジェクト プロパティの設定が変更されます。

この例では、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."
}