Creazione, modifica e rimozione di database

In SMO un database è rappresentato dall'oggetto Database.

Non è necessario creare un oggetto Database per modificarlo o rimuoverlo. È possibile fare riferimento al database tramite una raccolta.

Esempio

Per utilizzare qualsiasi esempio di codice fornito, è necessario scegliere l'ambiente, il modello e il linguaggio di programmazione per la creazione dell'applicazione. Per ulteriori informazioni, vedere Procedura: Creazione di un progetto Visual Basic SMO in Visual Studio .NET o Procedura: Creazione di un progetto Visual C# SMO in Visual Studio .NET.

Creazione, modifica e rimozione di un database in Visual Basic

In questo esempio di codice viene creato un nuovo database. I file e i filegroup del database vengono creati automaticamente.

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

Creazione, modifica e rimozione di un database in Visual C#

In questo esempio di codice viene creato un nuovo database. I file e i filegroup del database vengono creati automaticamente.

{
                //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();
            }

Creazione, modifica e rimozione di un database in PowerShell

In questo esempio di codice viene creato un nuovo database. I file e i filegroup del database vengono creati automaticamente.

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

Vedere anche

Riferimento