SMO では、データベースは Database オブジェクトによって表されます。
変更または削除するために Database オブジェクトを作成する必要はありません。 データベースはコレクションを使用して参照できます。
例
提供されているコード例を使用するには、プログラミング環境、プログラミング テンプレート、およびアプリケーションを作成するプログラミング言語を選択する必要があります。 詳細については、「 Visual Studio .NET で Visual Basic SMO プロジェクトを作成する」または「Visual Studio .NETで Visual C# SMO プロジェクトを作成する」を参照してください。
Visual Basic でのデータベースの作成、変更、および削除
このコード例では、新しいデータベースを作成します。 ファイルとファイル グループは、データベース用に自動的に作成されます。
Visual C でのデータベースの作成、変更、および削除#
このコード例では、新しいデータベースを作成します。 ファイルとファイル グループは、データベース用に自動的に作成されます。
{
//Connect to the local, default instance of SQL Server.
Server srv;
srv = new Server();
//Define a Database object variable by supplying the server and the database name arguments in the constructor.
Database db;
db = new Database(srv, "Test_SMO_Database");
//Create the database on the instance of SQL Server.
db.Create();
//Reference the database and display the date when it was created.
db = srv.Databases["Test_SMO_Database"];
Console.WriteLine(db.CreateDate);
//Remove the database.
db.Drop();
}
PowerShell でのデータベースの作成、変更、および削除
このコード例では、新しいデータベースを作成します。 ファイルとファイル グループは、データベース用に自動的に作成されます。
#Get a server object which corresponds to the default instance
cd \sql\localhost\
$srv = Get-Item default
#Create a new database
$db = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database -argumentlist $srv, "Test_SMO_Database"
$db.Create()
#Reference the database and display the date when it was created.
$db = $srv.Databases["Test_SMO_Database"]
$db.CreateDate
#Drop the database
$db.Drop()