屬性是儲存物件描述性資訊的值。 例如,Microsoft SQL Server 組態選項是由 Configuration 對象的屬性表示。 您可以使用屬性集合直接或間接存取屬性。 直接存取屬性會使用下列語法:
objInstance.PropertyName
根據屬性是否具有讀取/寫入存取權或只讀存取權,可以修改或擷取屬性值。 建立物件之前,也必須設定特定屬性。 如需詳細資訊,請參閱特定物件的 SMO 參考。
備註
子物件的集合會顯示為 物件的屬性。 例如,集合 Tables 是 對象的屬性 Server 。 如需詳細資訊,請參閱 使用集合。
對象的屬性是 Properties 集合的成員。 Properties 集合可用來逐一查看物件的每個屬性。
有時候屬性因下列原因而無法使用:
伺服器版本不支援 屬性,例如,如果您嘗試存取在舊版 SQL Server 上代表新 SQL Server 功能的屬性。
伺服器不會提供屬性的數據,例如,如果您嘗試存取代表未安裝的 SQL Server 元件的屬性。
您可以藉由攔截 UnknownPropertyException 和 PropertyCannotBeRetrievedException SMO 例外狀況來處理這些情況。
設定預設初始化欄位
SMO 會在擷取物件時執行優化。 優化會將自動調整下列狀態所載入的屬性數目降到最低:
部分載入。 第一次參考物件時,它至少具有可用的屬性(例如 Name 和 Schema)。
滿載。 參考任何屬性時,會初始化並提供快速載入的其餘屬性。
使用大量記憶體的屬性。 其餘無法使用的屬性會使用大量的記憶體,並且具有 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,以及如何將 屬性的 ConnectionContext 屬性設定SqlExecutionModes為ExecuteSql列舉型別的成員SqlExecutionModes。
在 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 物件之前建立和新增數據行。
在 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);
}