メソッドの呼び出し

適用対象:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse Analytics

メソッドは、データベースでチェックポイントを発行したり、Microsoft SQL Server のインスタンスのログオンの列挙リストを要求したりするなど、オブジェクトに関連する特定のタスクを実行します。

メソッドはオブジェクトに対する操作を実行します。 メソッドはパラメーターを受け取ることができ、多くの場合は戻り値があります。 戻り値には、単純なデータ型、複雑なオブジェクト、複数のメンバーを含んでいる構造のいずれを使用することもできます。

メソッドが正常に実行されたかどうかを検出するには、例外処理を使用します。 詳しくは、「 Handling SMO Exceptions」をご覧ください。

使用例

提供されているコード例を使用するには、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。 詳細については、「 Visual Studio .NET で Visual C# SMO プロジェクトを作成する」を参照してください。

Visual Basic での簡単な SMO メソッドの使用

この例で、Create メソッドにはパラメーターがなく、戻り値もありません。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Define a Database object variable by supplying the parent server and the database name arguments in the constructor.
Dim db As Database
db = New Database(srv, "Test_SMO_Database")
'Call the Create method to create the database on the instance of SQL Server. 
db.Create()

Visual C# での簡単な SMO メソッドの使用

この例で、Create メソッドにはパラメーターがなく、戻り値もありません。

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Define a Database object variable by supplying the parent server and the database name arguments in the constructor.   
Database db;   
db = new Database(srv, "Test_SMO_Database");   
//Call the Create method to create the database on the instance of SQL Server.   
db.Create();   
}

Visual Basic でのパラメーターを指定した SMO メソッドの使用

Table オブジェクトには、RebuildIndexes と呼ばれるメソッドがあります。 このメソッドには、 FillFactorを指定する数値パラメーターが必要です。

Dim srv As Server  
srv = New Server  
Dim tb As Table  
tb = srv.Databases("AdventureWorks2022").Tables("Employee", "HumanResources")  
tb.RebuildIndexes(70)  

Visual C# でのパラメーターを指定した SMO メソッドの使用

Table オブジェクトには、RebuildIndexes と呼ばれるメソッドがあります。 このメソッドには、 FillFactorを指定する数値パラメーターが必要です。

{   
Server srv = default(Server);   
srv = new Server();   
Table tb = default(Table);   
tb = srv.Databases("AdventureWorks2022").Tables("Employee", "HumanResources");   
tb.RebuildIndexes(70);   
}   

Visual Basic での DataTable オブジェクトを返す列挙メソッドの使用

このセクションでは、列挙メソッドを呼び出す方法、および返された DataTable オブジェクト内のデータを処理する方法について説明します。

メソッドは EnumCollations オブジェクトをDataTable返します。これには、SQL Serverのインスタンスに関する使用可能なすべての照合順序情報にアクセスするためのさらにナビゲーションが必要です。

'Connect to the local, default instance of SQL Server.  
Dim srv As Server  
srv = New Server  
'Call the EnumCollations method and return collation information to DataTable variable.  
Dim d As DataTable  
'Select the returned data into an array of DataRow.  
d = srv.EnumCollations  
'Iterate through the rows and display collation details for the instance of SQL Server.  
Dim r As DataRow  
Dim c As DataColumn  
For Each r In d.Rows  
    Console.WriteLine("==")  
    For Each c In r.Table.Columns  
        Console.WriteLine(c.ColumnName + " = " + r(c).ToString)  
    Next  
Next  

Visual C# での DataTable オブジェクトを返す列挙メソッドの使用

このセクションでは、列挙メソッドを呼び出す方法、および返された DataTable オブジェクト内のデータを処理する方法について説明します。

EnumCollations メソッドは、システム DataTable オブジェクトを返します。 オブジェクトはDataTable、SQL Serverのインスタンスに関する使用可能なすべての照合順序情報にアクセスするために、さらにナビゲーションを必要とします。

//Connect to the local, default instance of SQL Server.   
{   
Server srv = default(Server);   
srv = new Server();   
//Call the EnumCollations method and return collation information to DataTable variable.   
DataTable d = default(DataTable);   
//Select the returned data into an array of DataRow.   
d = srv.EnumCollations;   
//Iterate through the rows and display collation details for the instance of SQL Server.   
DataRow r = default(DataRow);   
DataColumn c = default(DataColumn);   
foreach ( r in d.Rows) {   
  Console.WriteLine("=========");   
  foreach ( c in r.Table.Columns) {   
    Console.WriteLine(c.ColumnName + " = " + r(c).ToString);   
  }   
}   
}   

Visual Basic でのオブジェクトの構築

オブジェクトのコンストラクターは、 New 演算子を使用して呼び出すことができます。 Database オブジェクト コンストラクターはオーバーロードされます。コード例で使用するバージョンの Database オブジェクト コンストラクターでは、パラメーターとして、データベースの親である Server オブジェクトおよび新しいデータベースの名前を表す文字列を受け取ります。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Declare and define a Database object by supplying the parent server and the database name arguments in the constructor.
Dim d As Database
d = New Database(srv, "Test_SMO_Database")
'Create the database on the instance of SQL Server.
d.Create()
Console.WriteLine(d.Name)

Visual C# でのオブジェクトの構築

オブジェクトのコンストラクターは、 New 演算子を使用して呼び出すことができます。 Database オブジェクト コンストラクターはオーバーロードされます。コード例で使用するバージョンの Database オブジェクト コンストラクターでは、パラメーターとして、データベースの親である Server オブジェクトおよび新しいデータベースの名前を表す文字列を受け取ります。

{   
Server srv;   
srv = new Server();   
Table tb;   
tb = srv.Databases("AdventureWorks2022").Tables("Employee", "HumanResources");   
tb.RebuildIndexes(70);   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Declare and define a Database object by supplying the parent server and the database name arguments in the constructor.   
Database d;   
d = new Database(srv, "Test_SMO_Database");   
//Create the database on the instance of SQL Server.   
d.Create();   
Console.WriteLine(d.Name);   
}  

Visual Basic での SMO オブジェクトのコピー

このコード例では、Copy メソッドを使用して Server オブジェクトのコピーを作成する方法を示します。 オブジェクトはServer、SQL Serverのインスタンスへの接続を表します。

'Connect to the local, default instance of SQL Server.
Dim srv1 As Server
srv1 = New Server()
'Modify the default database and the timeout period for the connection.
srv1.ConnectionContext.DatabaseName = "AdventureWorks2022"
srv1.ConnectionContext.ConnectTimeout = 30
'Make a second connection using a copy of the ConnectionContext property and verify settings.
Dim srv2 As Server
srv2 = New Server(srv1.ConnectionContext.Copy)
Console.WriteLine(srv2.ConnectionContext.ConnectTimeout.ToString)

Visual C# での SMO オブジェクトのコピー

このコード例では、Copy メソッドを使用して Server オブジェクトのコピーを作成する方法を示します。 オブジェクトはServer、SQL Serverのインスタンスへの接続を表します。

{   
//Connect to the local, default instance of SQL Server.   
Server srv1;   
srv1 = new Server();   
//Modify the default database and the timeout period for the connection.   
srv1.ConnectionContext.DatabaseName = "AdventureWorks2022";   
srv1.ConnectionContext.ConnectTimeout = 30;   
//Make a second connection using a copy of the ConnectionContext property and verify settings.   
Server srv2;   
srv2 = new Server(srv1.ConnectionContext.Copy);   
Console.WriteLine(srv2.ConnectionContext.ConnectTimeout.ToString);   
}  

Visual Basic でのサーバー プロセスの監視

列挙メソッドを使用して、SQL Serverのインスタンスに関する現在の状態の型情報を取得できます。 コード例では、EnumProcesses メソッドを使用して、現在のプロセスに関する情報を検出します。 また、返された DataTable オブジェクトの列および行を操作する方法も示します。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Call the EnumCollations method and return collation information to DataTable variable.
Dim d As DataTable
'Select the returned data into an array of DataRow.
d = srv.EnumProcesses
'Iterate through the rows and display collation details for the instance of SQL Server.
Dim r As DataRow
Dim c As DataColumn
For Each r In d.Rows
    Console.WriteLine("============================================")
    For Each c In r.Table.Columns
        Console.WriteLine(c.ColumnName + " = " + r(c).ToString)
    Next
Next

Visual C# でのサーバー プロセスの監視

列挙メソッドを使用して、SQL Serverのインスタンスに関する現在の状態の型情報を取得できます。 コード例では、EnumProcesses メソッドを使用して、現在のプロセスに関する情報を検出します。 また、返された DataTable オブジェクトの列および行を操作する方法も示します。

//Connect to the local, default instance of SQL Server.   
{   
Server srv = default(Server);   
srv = new Server();   
//Call the EnumCollations method and return collation information to DataTable variable.   
DataTable d = default(DataTable);   
//Select the returned data into an array of DataRow.   
d = srv.EnumProcesses;   
//Iterate through the rows and display collation details for the instance of SQL Server.   
DataRow r = default(DataRow);   
DataColumn c = default(DataColumn);   
foreach ( r in d.Rows) {   
  Console.WriteLine("=====");   
  foreach ( c in r.Table.Columns) {   
    Console.WriteLine(c.ColumnName + " = " + r(c).ToString);   
  }   
}   
}   

関連項目

Server
ServerConnection