Udostępnij za pośrednictwem


Tworzenie, modyfikowanie i usuwanie baz danych

W SMO, baza danych jest reprezentowana przez Database obiektu.

Nie jest konieczne tworzenie Database obiekt, aby zmodyfikować lub usunąć goBazy danych można się odwoływać za pomocą kolekcja.

Przykład

Aby używać dostarczonych przykładów kodu źródłowego, należy wybrać środowisko, szablon oraz język programowania, które będą używane do tworzenia aplikacji.Aby uzyskać więcej informacji, zobacz Jak Tworzenie projektu SMO Visual Basic w programie Visual Studio.NET lub Jak Tworzenie projektu programu Visual C# SMO w programie Visual Studio.NET.

Tworzenie, modyfikowanie i usuwanie bazy danych w języku Visual Basic

Ten przykładowy kod tworzy nową bazę danych.Pliki i grup 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, modyfikowanie i usuwanie bazy danych w środowisku Visual C#

Ten przykładowy kod tworzy nową bazę danych.Pliki i grup 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 PowerShell

Ten przykładowy kod tworzy nową bazę danych.Pliki i grup 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 także

Odwołanie