Поделиться через


Создание, изменение и удаление схем

Объект Schema представляет контекст владения для объекта базы данных. Свойство Schemas объекта Database представляет коллекцию объектов Schema.

Пример

Чтобы использовать предоставленный пример кода при создании собственного приложения, необходимо выбрать среду разработки, шаблон программирования и язык программирования. Дополнительные сведения см. в разделе Как создать проект SMO на языке Visual Basic в среде Visual Studio .NET или Как создать проект SMO на языке Visual C# в среде Visual Studio .NET.

Создание, изменение и удаление схемы в среде Visual Basic .NET

Данный пример кода демонстрирует, как создавать схему и назначать ее объекту базы данных. Затем программа предоставляет пользователю разрешение и создает новую таблицу в схеме.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Define a Schema object variable by supplying the parent database and name arguments in the constructor.
Dim sch As 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.
Dim obperset As 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.
Dim tb As Table
tb = New Table(db, "MyTable", "MySchema1")
Dim mycol As 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()