다음을 통해 공유


속성 설정 - SMO

적용 대상: SQL ServerAzure SQL Database Azure SQL Managed Instance Azure Synapse Analytics

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

objInstance.PropertyName

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

참고 항목

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

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

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

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

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

이러한 상황은 SMO 예외와 SMO 예외를 UnknownPropertyException catch하여 처리할 PropertyCannotBeRetrievedException 수 있습니다.

기본 초기화 필드 설정

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

  1. 부분적으로 로드되었습니다. 개체를 처음 참조할 때 사용 가능한 최소 속성(예: 이름 및 스키마)이 있습니다.

  2. 완전히 로드되었습니다. 모든 속성을 참조하면 로드가 빠르게 이루어지고 초기화되고 사용 가능한 나머지 속성이 만들어집니다.

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

애플리케이션에서 부분적으로 로드된 상태 외에 추가 속성을 가져오는 경우 쿼리를 제출하여 이러한 추가 속성을 검색하고 완전히 로드된 상태로 확장합니다. 이로 인해 클라이언트와 서버 간에 불필요한 트래픽이 발생할 수 있습니다. SetDefaultInitFields 메서드를 호출하여 보다 최적화할 수 있습니다. 이 메서드는 SetDefaultInitFields 개체가 초기화될 때 로드되는 속성의 사양을 허용합니다.

메서드는 SetDefaultInitFields 애플리케이션의 나머지 부분 또는 다시 설정 될 때까지 속성 로드 동작을 설정 합니다. GetDefaultInitFields 메서드를 사용하여 원래 동작을 저장하고 필요한 경우 복원할 수 있습니다.

예제

제공된 코드 예제를 사용하려면 프로그래밍 환경, 프로그래밍 템플릿 및 애플리케이션을 만들 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 Visual Studio .NET에서 Visual C# SMO 프로젝트 만들기를 참조하세요.

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

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

'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 방법과 속성의 속성을 열거형 형식의 SqlExecutionModes ConnectionContext ExecuteSql 멤버로 설정하는 SqlExecutionModes 방법을 보여 줍니다.Information

{   
//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에서 개체의 모든 속성을 반복합니다.

이 코드 예제에서는 개체의 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 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에서 개체의 모든 속성을 반복합니다.#

이 코드 예제에서는 개체의 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 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);   
}