メソッドは、データベースに対する Checkpoint
の発行や、Microsoft SQL Server インスタンスのログオンの列挙リストの要求など、オブジェクトに関連する特定のタスクを実行します。
メソッドは、オブジェクトに対して操作を実行します。 メソッドはパラメーターを受け取ることができ、多くの場合、戻り値を持つことができます。 戻り値には、単純なデータ型、複合オブジェクト、または多数のメンバーを含む構造体を指定できます。
例外処理を使用して、メソッドが成功したかどうかを検出します。 詳細については、「 SMO 例外の処理」を参照してください。
例示
提供されているコード例を使用するには、プログラミング環境、プログラミング テンプレート、およびアプリケーションを作成するプログラミング言語を選択する必要があります。 詳細については、SQL Server オンライン ブックの「方法: Visual Studio .NET で Visual Basic SMO プロジェクトを作成する」または「方法: Visual Studio .NET で Visual C# SMO プロジェクトを作成する」を参照してください。
Visual Basic での単純な SMO メソッドの使用
この例では、 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("AdventureWorks2012").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("AdventureWorks2012").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 オブジェクトと、新しいデータベースの名前を表す文字列の 2 つのパラメーターを受け取ります。
Visual C でのオブジェクトの構築#
任意のオブジェクトのコンストラクターは、 New
演算子を使用して呼び出すことができます。
Database オブジェクト コンストラクターはオーバーロードされ、サンプルで使用される Database オブジェクト コンストラクターのバージョンは、データベースが属する親Server オブジェクトと、新しいデータベースの名前を表す文字列の 2 つのパラメーターを受け取ります。
{
Server srv;
srv = new Server();
Table tb;
tb = srv.Databases("AdventureWorks2012").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 のインスタンスへの接続を表します。
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 = "AdventureWorks2012";
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 オブジェクト内の列と行を操作する方法も示します。
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);
}
}
}