Establecer propiedades

Las propiedades son valores que almacenan información descriptiva sobre el objeto. Por ejemplo, las opciones de configuración de Microsoft SQL Server se representan mediante las propiedades del objeto Configuration. Se puede tener acceso a las propiedades de forma directa o indirecta mediante la colección de propiedades. Para tener acceso directamente a las propiedades, se utiliza la sintaxis siguiente:

objInstance.PropertyName

Un valor de propiedad se puede modificar o recuperar dependiendo de si la propiedad tiene acceso de lectura/escritura o acceso de solo lectura. También es necesario establecer ciertas propiedades antes de que se pueda crear un objeto. Para obtener más información, vea la referencia de SMO del objeto concreto.

Nota

Las colecciones de objetos secundarios aparecen como la propiedad de un objeto. Por ejemplo, la colección Tables es una propiedad de un objeto Server. Para obtener más información, vea Usar colecciones.

Las propiedades de un objeto son miembros de la colección Properties. La colección Properties se puede utilizar para recorrer en iteración cada propiedad de un objeto.

A veces una propiedad no está disponible por las razones siguientes:

  • La versión de servidor no admite la propiedad, como en el caso de que intente obtener acceso a una propiedad que representa una nueva característica de SQL Server en una versión anterior de SQL Server.

  • El servidor no proporciona datos para la propiedad, como en el caso de que intente obtener acceso a una propiedad que representa un componente de SQL Server que no está instalado.

Puede controlar estas circunstancias si detecta las excepciones SMO UnknownPropertyException y PropertyCannotBeRetrievedException.

Establecer campos de inicialización predeterminados

SMO realiza una optimización al recuperar objetos. La optimización reduce el número de propiedades cargadas mediante la aplicación de una escala automática entre los estados siguientes:

  1. Parcialmente cargado. Cuando se hace referencia por primera vez a un objeto, tiene un mínimo de propiedades disponibles (como Nombre y Esquema).

  2. Totalmente cargado. Cuando se hace referencia a una propiedad, las propiedades restantes que se cargan rápidamente se inicializan y pasan a estar disponibles.

  3. Propiedades que utilizan mucha memoria. Las propiedades no disponibles restantes utilizan mucha memoria y tienen un valor de propiedad Expensive de True (como DataSpaceUsage). Estas propiedades solo se cargan cuando se hace referencia a ellas de forma específica.

Si la aplicación captura propiedades adicionales, además de las que se proporcionan en el estado parcialmente cargado, envía una consulta para recuperar estas propiedades adicionales y pasa al estado totalmente cargado. Esto puede producir un tráfico innecesario entre cliente y servidor. Si se llama al método SetDefaultInitFields se puede conseguir una mayor optimización. El método SetDefaultInitFields permite la especificación de las propiedades que se cargan al inicializar el objeto.

El método SetDefaultInitFields establece el comportamiento de carga de propiedades para el resto de aplicación o hasta que se restablece. Puede guardar el comportamiento original mediante el método GetDefaultInitFields y restaurarlo cuando sea necesario.

Ejemplos

Para utilizar cualquier ejemplo de código que se proporcione, deberá elegir el entorno de programación, la plantilla de programación y el lenguaje de programación en los que crear su aplicación. Para obtener más información, vea "Cómo crear un proyecto de Visual Basic SMO en Visual Studio .NET" o "Cómo crear un proyecto de Visual C# SMO en Visual Studio .NET" en los Libros en pantalla de SQL Server.

Obtener y establecer una propiedad en Visual Basic

En este ejemplo de código se muestra cómo obtener la propiedad Edition del objeto Information y cómo establecer la propiedad SqlExecutionModes de la propiedad ConnectionContext en el miembro ExecuteSql del tipo enumerado 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

Obtener y establecer una propiedad en Visual C#

En este ejemplo de código se muestra cómo obtener la propiedad Edition del objeto Information y cómo establecer la propiedad SqlExecutionModes de la propiedad ConnectionContext en el miembro ExecuteSql del tipo enumerado 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; 
}

Establecer diversas propiedades antes de crear un objeto en Visual Basic

En este ejemplo de código se muestra cómo establecer directamente la propiedad AnsiNullsStatus del objeto Table y cómo crear y agregar columnas antes de crear el objeto Table.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Create a new table in the AdventureWorks2008R2 database. 
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
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()

Establecer diversas propiedades antes de crear un objeto en Visual C#

En este ejemplo de código se muestra cómo establecer directamente la propiedad AnsiNullsStatus del objeto Table y cómo crear y agregar columnas antes de crear el objeto Table.

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Create a new table in the AdventureWorks2008R2 database. 
Database db; 
db = srv.Databases("AdventureWorks2008R2"); 
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(); 
}

Recorrer en iteración todas las propiedades de un objeto en Visual Basic

En este ejemplo de código se recorre en iteración la colección Properties del objeto StoredProcedure y las propiedades de esta colección se muestran en la pantalla Salida de Visual Studio.

En el ejemplo, el objeto Property se ha incluido entre corchetes porque también es una palabra clave de 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 AdventureWorks2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
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

Recorrer en iteración todas las propiedades de un objeto en Visual C#

En este ejemplo de código se recorre en iteración la colección Properties del objeto StoredProcedure y las propiedades de esta colección se muestran en la pantalla Salida de 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 AdventureWorks2008R2 database. 
Database db; 
db = srv.Databases("AdventureWorks2008R2"); 
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); 
  } 
}

Establecer campos de inicialización predeterminados en Visual Basic

En este ejemplo de código se muestra cómo minimizar el número de propiedades de objeto inicializadas en un programa SMO. Debe incluir la instrucción using System.Collections.Specialized para utilizar el objeto StringCollection.

Puede utilizarse SQL Server Profiler para comparar el número de instrucciones enviadas a la instancia de SQL Server con esta optimización.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
'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 AdventureWorks2008R2.
'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)

Establecer campos de inicialización predeterminados en Visual C#

En este ejemplo de código se muestra cómo minimizar el número de propiedades de objeto inicializadas en un programa SMO. Debe incluir la instrucción using System.Collections.Specialized para utilizar el objeto StringCollection.

Puede utilizarse SQL Server Profiler para comparar el número de instrucciones enviadas a la instancia de SQL Server con esta optimización.

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Reference the AdventureWorks2008R2 database. 
Database db; 
db = srv.Databases("AdventureWorks2008R2"); 
//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 AdventureWorks2008R2. 
   //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); 
}