设置属性 - SMO

适用于:SQL ServerAzure SQL 数据库Azure SQL 托管实例Azure Synapse Analytics

属性是存储有关对象的说明性信息的值。 例如,Microsoft SQL Server配置选项由 Configuration 对象的属性表示。 使用属性集合可以直接或间接访问属性。 直接访问属性使用以下语法:

objInstance.PropertyName

可以修改或检索属性值,具体取决于该属性是拥有读/写访问权限还是只读访问权限。 此外,还必须在创建对象之前设置某些特定属性。 有关详细信息,请参阅特定对象的 SMO 参考资料。

注意

某一对象的子对象集合显示为该对象的属性。 例如, Tables 集合是 Server 对象的属性。 有关详细信息,请参阅 Using Collections

对象的属性是属性集合的成员。 属性集合可用于遍历对象的每个属性。

有时某一属性不可用,其原因如下:

  • 服务器版本不支持 属性,例如,如果尝试访问表示旧版 SQL Server 上的新SQL Server功能的属性。

  • 服务器不提供属性的数据,例如,如果尝试访问表示未安装的SQL Server组件的属性。

通过捕获 UnknownPropertyExceptionPropertyCannotBeRetrievedException SMO 异常,可以处理上述情况。

设置默认的初始化字段

SMO 检索对象时将执行优化功能。 通过在以下状态之间进行缩放,此优化功能最大程度地减少了加载的属性数目:

  1. 部分加载。 首次引用某一对象时,该对象具有最少的可用属性(例如 Name 和 Schema)。

  2. 完全加载。 当引用任一属性时,将初始化剩余属性中可快速加载的属性并使其可用。

  3. 占用大量内存的属性。 剩余的不可用属性占用大量内存,并将 Expensive 属性值设置为 True(例如 DataSpaceUsage)。 只有专门引用这些属性时才会进行加载。

除在部分加载状态中提供的属性之外,如果应用程序的确还需要提取额外属性,则会提交检索这些额外属性的查询,并向上扩展到完全加载状态。 这可能会在客户端和服务器之间造成不必要的通信流量。 调用 SetDefaultInitFields 方法可以实现更多优化。 使用 SetDefaultInitFields 方法可以指定在初始化对象时加载的属性。

SetDefaultInitFields 方法设置其余应用程序或重置应用程序后的属性加载行为。 您可以使用 GetDefaultInitFields 方法保存原始行为,并根据需要还原。

示例

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

在 Visual Basic 中获取和设置属性

此代码示例演示如何获取 Edition 对象的 属性Information,以及如何将 属性的 ConnectionContext 属性设置为SqlExecutionModes枚举类型的 ExecuteSql 成员SqlExecutionModes

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Get a property.
Console.WriteLine(srv.Information.Version)
'Set a property.
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteSql

在 Visual C# 中获取和设置属性

此代码示例演示如何获取 Edition 对象的 属性Information,以及如何将 属性的 ConnectionContext 属性设置为SqlExecutionModes枚举类型的 ExecuteSql 成员SqlExecutionModes

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Get a property.   
Console.WriteLine(srv.Information.Version);   
//Set a property.   
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteSql;   
}  

在 Visual Basic 中创建对象之前设置各种属性

此代码示例演示如何直接设置 AnsiNullsStatus 对象的 Table 属性,以及如何在创建 Table 对象之前创建和添加列。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Create a new table in the AdventureWorks2022 database. 
Dim db As Database
db = srv.Databases("AdventureWorks2022")
Dim tb As Table
'Specify the parent database, table schema and the table name in the constructor.
tb = New Table(db, "Test_Table", "HumanResources")
'Add columns because the table requires columns before it can be created. 
Dim c1 As Column
'Specify the parent table, the column name and data type in the constructor.
c1 = New Column(tb, "ID", DataType.Int)
tb.Columns.Add(c1)
c1.Nullable = False
c1.Identity = True
c1.IdentityIncrement = 1
c1.IdentitySeed = 0
Dim c2 As Column
c2 = New Column(tb, "Name", DataType.NVarChar(100))
c2.Nullable = False
tb.Columns.Add(c2)
tb.AnsiNullsStatus = True
'Create the table on the instance of SQL Server.
tb.Create()

在 Visual C# 中创建对象之前设置各种属性

此代码示例演示如何直接设置 AnsiNullsStatus 对象的 Table 属性,以及如何在创建 Table 对象之前创建和添加列。

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Create a new table in the AdventureWorks2022 database.   
Database db;   
db = srv.Databases["AdventureWorks2022"];   
Table tb;   
//Specify the parent database, table schema, and the table name in the constructor.   
tb = new Table(db, "Test_Table", "HumanResources");   
//Add columns because the table requires columns before it can be created.   
Column c1;   
//Specify the parent table, the column name, and data type in the constructor.   
c1 = new Column(tb, "ID", DataType.Int);   
tb.Columns.Add(c1);   
c1.Nullable = false;   
c1.Identity = true;   
c1.IdentityIncrement = 1;   
c1.IdentitySeed = 0;   
Column c2;   
c2 = new Column(tb, "Name", DataType.NVarChar(100));   
c2.Nullable = false;   
tb.Columns.Add(c2);   
tb.AnsiNullsStatus = true;   
//Create the table on the instance of SQL Server.   
tb.Create();   
}  

在 Visual Basic 中遍历对象的所有属性

此代码示例循环访问 对象的 Properties 集合 StoredProcedure ,并在 Visual Studio 输出屏幕上显示它们。

在此示例中, Property 对象已被置于方括号中,因为它也是 Visual Basic 关键字。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Set properties on the uspGetEmployeeManagers stored procedure on the AdventureWorks2022 database.
Dim db As Database
db = srv.Databases("AdventureWorks2022")
Dim sp As StoredProcedure
sp = db.StoredProcedures("uspGetEmployeeManagers")
sp.AnsiNullsStatus = False
sp.QuotedIdentifierStatus = False
'Iterate through the properties of the stored procedure and display.
'Note the Property object requires [] parentheses to distinguish it from the Visual Basic key word.
Dim p As [Property]
For Each p In sp.Properties
    Console.WriteLine(p.Name & p.Value)
Next

在 Visual C# 中遍历对象的所有属性

此代码示例循环访问 对象的 Properties 集合 StoredProcedure ,并在 Visual Studio 输出屏幕上显示它们。

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Set properties on the uspGetEmployeedManagers stored procedure on the AdventureWorks2022 database.   
Database db;   
db = srv.Databases["AdventureWorks2022"];   
StoredProcedure sp;   
sp = db.StoredProcedures("uspGetEmployeeManagers");   
sp.AnsiNullsStatus = false;   
sp.QuotedIdentifierStatus = false;   
//Iterate through the properties of the stored procedure and display.   
  Property p;   
  foreach ( p in sp.Properties) {   
    Console.WriteLine(p.Name + p.Value);   
  }   
}  

在 Visual Basic 中设置默认的初始化字段

此代码示例演示如何使 SMO 程序中初始化的对象属性的数目降到最低。 您必须包括 using System.Collections.Specialized; 语句,以便使用 StringCollection 对象。

SQL Server Profiler可用于将发送到 SQL Server 实例的数字语句与此优化进行比较。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2022 database.
Dim db As Database
db = srv.Databases("AdventureWorks2022")
'Assign the Table object type to a System.Type object variable.
Dim tb As Table
Dim typ As Type
tb = New Table
typ = tb.GetType
'Assign the current default initialization fields for the Table object type to a 
'StringCollection object variable.
Dim sc As StringCollection
sc = srv.GetDefaultInitFields(typ)
'Set the default initialization fields for the Table object type to the CreateDate property.
srv.SetDefaultInitFields(typ, "CreateDate")
'Retrieve the Schema, Name, and CreateDate properties for every table in AdventureWorks2022.
'Note that the improvement in performance can be viewed in SQL Profiler.
For Each tb In db.Tables
    Console.WriteLine(tb.Schema + "." + tb.Name + " " + tb.CreateDate)
Next
'Set the default initialization fields for the Table object type back to the original settings.
srv.SetDefaultInitFields(typ, sc)

在 Visual C# 中设置默认的初始化字段

此代码示例演示如何使 SMO 程序中初始化的对象属性的数目降到最低。 您必须包括 using System.Collections.Specialized; 语句,以便使用 StringCollection 对象。

SQL Server Profiler可用于将发送到 SQL Server 实例的数字语句与此优化进行比较。

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Reference the AdventureWorks2022 database.   
Database db;   
db = srv.Databases["AdventureWorks2022"];   
//Assign the Table object type to a System.Type object variable.   
Table tb;   
Type typ;   
tb = new Table();   
typ = tb.GetType;   
//Assign the current default initialization fields for the Table object type to a   
//StringCollection object variable.   
StringCollection sc;   
sc = srv.GetDefaultInitFields(typ);   
//Set the default initialization fields for the Table object type to the CreateDate property.   
srv.SetDefaultInitFields(typ, "CreateDate");   
//Retrieve the Schema, Name, and CreateDate properties for every table in AdventureWorks2022.   
   //Note that the improvement in performance can be viewed in SQL Server Profiler.   
foreach ( tb in db.Tables) {   
   Console.WriteLine(tb.Schema + "." + tb.Name + " " + tb.CreateDate);   
}   
//Set the default initialization fields for the Table object type back to the original settings.   
srv.SetDefaultInitFields(typ, sc);   
}