Condividi tramite


Creazione, modifica e rimozione di database

Si applica a:SQL ServerDatabase SQL diAzure Istanzagestita di SQL di Azure Azure Synapse Analytics

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 usare qualsiasi esempio di codice fornito, è necessario scegliere l'ambiente di programmazione, il modello di programmazione e il linguaggio di programmazione per la creazione dell'applicazione. Per altre informazioni, vedere Creare un progetto SMO di Visual C# 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()  

Vedi anche

Database