Share via


メソッドの呼び出し

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

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

メソッドが正常に実行されたかどうかを検出するには、例外処理を使用します。詳細については、「SMO 例外の処理」を参照してください。

提供されているコード例を使用するには、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。詳細については、SQL Server オンライン ブックの「Visual Studio .NET で Visual Basic SMO プロジェクトを作成する方法」および「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("AdventureWorks2008R2").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("AdventureWorks2008R2").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("AdventureWorks2008R2").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 = "AdventureWorks2008R2"
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 = "AdventureWorks2008R2"; 
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); 
  } 
} 
} 

関連項目

参照