Udostępnij przez


Tworzenie, zmienianie i usuwanie schematów

The Schema object represents an ownership context for database object.The Schemas() właściwość of the Database object represents a kolekcja of Schema objects.

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 obiektów SMO projektu Visual Basic w programie Visual Studio .NET lub Jak Tworzenie projektu programu Visual C# obiekty SMO w programie Visual Studio .NET.

Tworzenie, zmienianie i usuwanie schematu w języku Visual Basic

W tym przykładzie kodu ilustruje sposób tworzenia schematu i przypisać ją do obiektu bazy danych.Program następnie udziela uprawnień do użytkownika, a następnie tworzy nową tabela w schemacie.

Tworzenie, zmienianie i usuwanie schematu w środowisku Visual C#

W tym przykładzie kodu ilustruje sposób tworzenia schematu i przypisać ją do obiektu bazy danych.Program następnie udziela uprawnień do użytkownika, a następnie tworzy nową tabela w schemacie.

//Connect to the local, default instance of SQL Server. 
{ 
Server srv = default(Server); 
srv = new Server(); 
//Reference the AdventureWorks database. 
Database db = default(Database); 
db = srv.Databases("AdventureWorks"); 
//Define a Schema object variable by supplying the parent database and name arguments in the constructor. 
Schema sch = default(Schema); 
sch = new Schema(db, "MySchema1"); 
sch.Owner = "dbo"; 
//Create the schema on the instance of SQL Server. 
sch.Create(); 
//Define an ObjectPermissionSet that contains the Update and Select object permissions. 
ObjectPermissionSet obperset = default(ObjectPermissionSet); 
obperset = new ObjectPermissionSet(); 
obperset.Add(ObjectPermission.Select); 
obperset.Add(ObjectPermission.Update); 
//Grant the set of permissions on the schema to the guest account. 
sch.Grant(obperset, "guest"); 
//Define a Table object variable by supplying the parent database, name and schema arguments in the constructor. 
Table tb = default(Table); 
tb = new Table(db, "MyTable", "MySchema1"); 
Column mycol = default(Column); 
mycol = new Column(tb, "Date", DataType.DateTime); 
tb.Columns.Add(mycol); 
tb.Create(); 
//Modify the owner of the schema and run the Alter method to make the change on the instance of SQL Server. 
sch.Owner = "guest"; 
sch.Alter(); 
//Run the Drop method for the table and the schema to remove them. 
tb.Drop(); 
sch.Drop(); 
}