Aracılığıyla paylaş


Oluşturma, değiştirme ve veritabanları kaldırma

Bir veritabanını smo içinde temsil edilir Database nesne.

Oluşturmak gerekli değildir bir Database nesne it. kaldırmak veya değiştirmek içinVeritabanı, koleksiyon kullanarak başvurulabilir.

Örnek

Sunulan kod örneklerinden herhangi birini kullanmak için, programlama ortamını, programlama şablonunu ve uygulamanızı oluşturacağınız programlama dilini seçmeniz gerekecektir.Daha fazla bilgi için bkz: Nasıl yapılır: Visual Studio'da Visual Basic smo proje oluşturun.NET veya Nasıl yapılır: Bir Visual C# smo Project Visual Studio'da oluşturun.NET.

Oluşturma, değiştirme ve Visual Basic'te bir veritabanı kaldırma

Bu kod örneği, yeni bir veritabanı oluşturur.Dosyaları ve dosya gruplarını veritabanı için otomatik olarak oluşturulur.

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

Oluşturma, değiştirme ve Visual C# içinde bir veritabanı kaldırma

Bu kod örneği, yeni bir veritabanı oluşturur.Dosyaları ve dosya gruplarını veritabanı için otomatik olarak oluşturulur.

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

Oluşturma, değiştirme ve PowerShell içinde bir veritabanı kaldırma

Bu kod örneği, yeni bir veritabanı oluşturur.Dosyaları ve dosya gruplarını veritabanı için otomatik olarak oluşturulur.

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

Ayrıca bkz.

Başvuru