Share via


データベースの作成、変更、および削除

SMO では、データベースは Database オブジェクトで表現されます。

修正または削除のために、Database オブジェクトを作成する必要はありません。データベースは、コレクションを使用して参照することができます。

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

Visual Basic でのデータベースの作成、変更、および削除

このコード例では、新しいデータベースを作成します。このデータベースに対し、ファイルおよびファイル グループが自動的に作成されます。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Define a Database object variable by supplying the server and the database name arguments in the constructor.
Dim db As Database
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()

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()

関連項目

参照