Udostępnij za pomocą


Tworzenie, zmienianie i usuwanie baz danych

Dotyczy:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsSQL database w usłudze Microsoft Fabric

W środowisku SMO baza danych jest reprezentowana Database przez obiekt .

Nie jest konieczne utworzenie Database obiektu w celu zmodyfikowania lub usunięcia go. Do bazy danych można odwoływać się przy użyciu kolekcji.

Example

Aby użyć dowolnego podanego przykładu kodu, musisz wybrać środowisko programowania, szablon programowania i język programowania, w którym ma zostać utworzona aplikacja. Aby uzyskać więcej informacji, zobacz Create a Visual C# SMO Project in Visual Studio .NET(Tworzenie projektu SMO w programie Visual Studio .NET).

Tworzenie, zmienianie i usuwanie bazy danych w Visual Basic

Ten przykładowy kod tworzy nową bazę danych. Pliki i grupy plików są tworzone automatycznie dla bazy danych.

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

Tworzenie, zmienianie i usuwanie bazy danych w programie Visual C#

Ten przykładowy kod tworzy nową bazę danych. Pliki i grupy plików są tworzone automatycznie dla bazy danych.

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

Tworzenie, zmienianie i usuwanie bazy danych w programie PowerShell

Ten przykładowy kod tworzy nową bazę danych. Pliki i grupy plików są tworzone automatycznie dla bazy danych.

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

Zobacz też

Database