속성 설정

속성은 개체에 대한 설명 정보를 저장하는 값입니다. 예를 들어 Microsoft SQL Server 구성 옵션은 Configuration 개체의 속성으로 표시됩니다. 속성 컬렉션을 사용하여 직접 또는 간접적으로 속성에 액세스할 수 있습니다. 속성에 직접 액세스하는 경우 다음 구문을 사용합니다.

objInstance.PropertyName

속성에 읽기/쓰기 권한 또는 읽기 전용 권한이 있는지에 따라 속성 값을 수정하거나 검색할 수 있습니다. 또한 특정 속성을 설정해야 개체를 만들 수 있습니다. 자세한 내용은 특정 개체에 대한 SMO 참조를 참조하십시오.

[!참고]

자식 개체의 컬렉션은 개체의 속성으로 나타납니다. 예를 들어 Tables 컬렉션은 Server 개체의 속성입니다. 자세한 내용은 컬렉션 사용를 참조하세요.

개체의 속성은 속성 컬렉션의 멤버입니다. 속성 컬렉션을 사용하여 개체의 모든 속성을 반복할 수 있습니다.

다음과 같은 이유로 속성을 사용할 수 없는 경우도 있습니다.

  • 이전 버전의 SQL Server에서 새로운 SQL Server 기능을 나타내는 속성에 액세스하려는 경우와 같이 서버 버전에서 속성을 지원하지 않습니다.

  • 설치되지 않은 SQL Server 구성 요소를 나타내는 속성에 액세스하려는 경우와 같이 서버에서 속성 데이터를 제공하지 않습니다.

UnknownPropertyExceptionPropertyCannotBeRetrievedException SMO 실행을 catch하여 이러한 문제를 처리할 수 있습니다.

기본 초기화 필드 설정

SMO는 개체를 검색할 때 최적화를 수행합니다. 최적화는 다음과 같은 상태를 자동으로 조정하여 로드되는 속성 수를 최소화합니다.

  1. 부분적으로 로드됨. 개체를 처음 참조하는 경우 이름 및 스키마와 같은 최소한의 속성만 사용할 수 있습니다.

  2. 전체 로드됨. 아무 속성이나 참조하면 나머지 속성이 신속하게 로드되어 초기화되고 사용할 수 있게 됩니다.

  3. 많은 메모리를 사용하는 속성. 사용할 수 없는 나머지 속성은 많은 메모리를 사용하며 Expensive 속성 값이 true입니다(예: DataSpaceUsage). 이러한 속성은 구체적으로 참조될 때만 로드됩니다.

응용 프로그램이 부분적으로 로드됨 상태에서 제공되는 속성 이외의 추가 속성을 인출하는 경우 쿼리를 제출하여 이러한 추가 속성을 검색하고 전체 로드됨 상태로 확장됩니다. 이로 인해 클라이언트와 서버 간에 불필요한 트래픽이 발생할 수 있습니다. SetDefaultInitFields 메서드를 호출하여 보다 최적화할 수 있습니다. SetDefaultInitFields 메서드를 사용하면 개체를 초기화할 때 로드되는 속성을 지정할 수 있습니다.

SetDefaultInitFields 메서드는 다시 설정될 때까지 또는 나머지 응용 프로그램에 대해 속성 로드 동작을 설정합니다. GetDefaultInitFields 메서드를 사용하여 원래 동작을 저장하고 필요한 경우 복원할 수 있습니다.

제공된 코드 예제를 사용하려면 응용 프로그램을 만들 프로그래밍 환경, 프로그래밍 템플릿 및 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 SQL Server 온라인 설명서의 "방법: Visual Studio .NET에서 Visual Basic SMO 프로젝트 만들기” 또는 “방법: Visual Studio .NET에서 Visual C# SMO 프로젝트 만들기”를 참조하십시오.

Visual Basic에서 속성 가져오기 및 설정

이 코드 예제에서는 Information 개체의 Edition 속성을 가져오는 방법과 ConnectionContext 속성의 SqlExecutionModes 속성을 SqlExecutionModes 열거 유형의 ExecuteSql 멤버로 설정하는 방법을 보여 줍니다.

'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#에서 속성 가져오기 및 설정

이 코드 예제에서는 Information 개체의 Edition 속성을 가져오는 방법과 ConnectionContext 속성의 SqlExecutionModes 속성을 SqlExecutionModes 열거 유형의 ExecuteSql 멤버로 설정하는 방법을 보여 줍니다.

{ 
//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에서 개체를 만들기 전에 다양한 속성 설정

이 코드 예제에서는 Table 개체의 AnsiNullsStatus 속성을 직접 설정하는 방법과 Table 개체를 만들기 전에 열을 만들고 추가하는 방법을 보여 줍니다.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Create a new table in the AdventureWorks2012 database. 
Dim db As Database
db = srv.Databases("AdventureWorks2012")
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#에서 개체를 만들기 전에 다양한 속성 설정

이 코드 예제에서는 Table 개체의 AnsiNullsStatus 속성을 직접 설정하는 방법과 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에서 개체의 모든 속성 반복

이 코드 예제에서는 StoredProcedure 개체의 Properties 컬렉션을 반복하고 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 AdventureWorks2012 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
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#에서 개체의 모든 속성 반복

이 코드 예제에서는 StoredProcedure 개체의 Properties 컬렉션을 반복하고 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 프로그램에서 초기화되는 개체 속성 수를 최소화하는 방법을 보여 줍니다. StringCollection 개체를 사용하려면 using System.Collections.Specialized 문을 포함해야 합니다.

SQL Server 프로파일러를 사용하여 SQL Server 인스턴스로 전송되는 문 수와 이 최적화를 비교할 수 있습니다.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2012 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'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 AdventureWorks2012.
'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 프로그램에서 초기화되는 개체 속성 수를 최소화하는 방법을 보여 줍니다. StringCollection 개체를 사용하려면 using System.Collections.Specialized 문을 포함해야 합니다.

SQL Server 프로파일러를 사용하여 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); 
}