创建、更改和删除架构

Schema 对象表示数据库对象的所有权上下文。Database 对象的 Schemas 属性表示 Schema 对象的集合。

示例

若要使用所提供的任何代码示例,您必须选择创建应用程序所需的编程环境、编程模板和编程语言。有关详细信息,请参阅如何在 Visual Studio .NET 中创建 Visual Basic SMO 项目如何在 Visual Studio .NET 中创建 Visual C# SMO 项目

在 Visual Basic 中创建、更改和删除架构

此代码示例说明如何创建架构并将其分配给数据库对象。接着,程序向用户授予权限,然后在该架构中创建一个新表。

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

在 Visual C# 中创建、更改和删除架构

此代码示例说明如何创建架构并将其分配给数据库对象。接着,程序向用户授予权限,然后在该架构中创建一个新表。

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