属性是存储有关对象的描述性信息的值。 例如,Microsoft SQL Server 配置选项由 Configuration 对象的属性表示。 可以使用属性集合直接或间接访问属性。 直接访问属性使用以下语法:
objInstance.PropertyName
可以根据属性具有读/写访问权限还是只读访问权限来修改或检索属性值。 在创建对象之前,还需要设置某些属性。 有关详细信息,请参阅特定对象的 SMO 参考。
注释
子对象的集合显示为对象的属性。 例如,集合 Tables 是对象的 Server 属性。 有关详细信息,请参阅 “使用集合”。
对象的属性是 Properties 集合的成员。 Properties 集合可用于循环访问对象的每个属性。
有时,由于以下原因,属性不可用:
服务器版本不支持该属性,例如,如果尝试访问表示较旧版本的 SQL Server 上的新 SQL Server 功能的属性。
服务器不提供该属性的数据,例如,如果尝试访问表示未安装的 SQL Server 组件的属性。
可以通过捕获 UnknownPropertyException SMO 异常和 PropertyCannotBeRetrievedException SMO 异常来处理这些情况。
设置默认初始化字段
SMO 在检索对象时执行优化。 优化可最大程度地减少通过自动缩放以下状态加载的属性数:
部分加载。 首次引用对象时,它至少具有可用的属性(如名称和架构)。
满载。 引用任何属性时,将初始化并提供快速加载的剩余属性。
使用大量内存的属性。 剩余不可用的属性使用大量内存,并且 Expensive 属性值为 true(例如 DataSpaceUsage)。 仅当专门引用时,才会加载这些属性。
如果应用程序确实提取了额外的属性,除了部分加载状态中提供的属性外,它还会提交一个查询来检索这些额外的属性,并纵向扩展到完全加载的状态。 这可能会导致客户端和服务器之间不必要的流量。 可以通过调用 SetDefaultInitFields 该方法来实现更多优化。 该方法 SetDefaultInitFields 允许规范初始化对象时加载的属性。
该方法 SetDefaultInitFields 为应用程序的其余部分设置属性加载行为,或直到重置它为止。 可以使用该方法保存原始行为 GetDefaultInitFields ,并根据需要还原它。
例子
若要使用提供的任何代码示例,必须选择要在其中创建应用程序的编程环境、编程模板和编程语言。 有关详细信息,请参阅 SQL Server 联机丛书中的“如何:在 Visual Studio .NET 中创建 Visual Basic SMO 项目”或“如何:在 Visual Studio .NET 中创建 Visual C# SMO 项目”。
在 Visual Basic 中获取和设置属性
此代码示例演示如何获取 Edition 对象的属性 Information 以及如何将 SqlExecutionModes 属性的属性 ConnectionContext 设置为 ExecuteSql 枚举类型的成员 SqlExecutionModes 。
获取和设置 Visual C 中的属性#
此代码示例演示如何获取 Edition 对象的属性 Information 以及如何将 SqlExecutionModes 属性的属性 ConnectionContext 设置为 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 对象之前创建和添加列。
在 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 AdventureWorks2012 database.
Database db;
db = srv.Databases("AdventureWorks2012");
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 关键字。
循环访问 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 AdventureWorks2012 database.
Database db;
db = srv.Databases("AdventureWorks2012");
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 实例的数字语句与此优化进行比较。
在 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 AdventureWorks2012 database.
Database db;
db = srv.Databases("AdventureWorks2012");
//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 AdventureWorks2012.
//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);
}