메서드 호출

메서드는 데이터베이스에 대해 Checkpoint를 실행하거나 MicrosoftSQL Server의 인스턴스에 대해 로그온 열거 목록을 요청하는 것과 같이 개체와 관련된 특정 태스크를 수행합니다.

메서드는 개체에 대해 작업을 수행하며 매개 변수를 사용할 수 있고 반환 값을 갖는 경우도 있습니다. 반환 값은 단순한 데이터 형식, 복잡한 개체 또는 여러 멤버가 포함된 구조일 수 있습니다.

메서드가 성공적으로 실행되었는지 여부를 확인하려면 예외 처리를 사용합니다. 자세한 내용은 SMO 예외 처리를 참조하십시오.

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

Visual Basic에서 간단한 SMO 메서드 사용

이 예에서 Create 메서드는 매개 변수를 사용하지 않고 반환 값도 가지지 않습니다.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Define a Database object variable by supplying the parent server and the database name arguments in the constructor.
Dim db As Database
db = New Database(srv, "Test_SMO_Database")
'Call the Create method to create the database on the instance of SQL Server. 
db.Create()

Visual C#에서 간단한 SMO 메서드 사용

이 예에서 Create 메서드는 매개 변수를 사용하지 않고 반환 값도 가지지 않습니다.

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Define a Database object variable by supplying the parent server and the database name arguments in the constructor. 
Database db; 
db = new Database(srv, "Test_SMO_Database"); 
//Call the Create method to create the database on the instance of SQL Server. 
db.Create(); 

}

Visual Basic에서 매개 변수가 있는 SMO 메서드 사용

Table 개체에는 RebuildIndexes라는 메서드가 있습니다. 이 메서드에는 FillFactor를 지정하는 숫자 매개 변수가 필요합니다.

Dim srv As Server
srv = New Server
Dim tb As Table
tb = srv.Databases("AdventureWorks").Tables("Employee", "HumanResources")
tb.RebuildIndexes(70)

Visual C#에서 매개 변수가 있는 SMO 메서드 사용

Table 개체에는 RebuildIndexes라는 메서드가 있습니다. 이 메서드에는 FillFactor를 지정하는 숫자 매개 변수가 필요합니다.

{ 
Server srv = default(Server); 
srv = new Server(); 
Table tb = default(Table); 
tb = srv.Databases("AdventureWorks").Tables("Employee", "HumanResources"); 
tb.RebuildIndexes(70); 
} 

Visual Basic에서 DataTable 개체를 반환하는 열거형 메서드 사용

이 섹션에서는 열거형 메서드를 호출하는 방법 및 반환된 DataTable 개체의 데이터를 처리하는 방법에 대해 설명합니다.

EnumCollations 메서드는 DataTable 개체를 반환하며, SQL Server 인스턴스에 대한 모든 데이터 정렬 정보에 액세스하려면 이 개체를 추가적으로 탐색해야 합니다.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Call the EnumCollations method and return collation information to DataTable variable.
Dim d As DataTable
'Select the returned data into an array of DataRow.
d = srv.EnumCollations
'Iterate through the rows and display collation details for the instance of SQL Server.
Dim r As DataRow
Dim c As DataColumn
For Each r In d.Rows
    Console.WriteLine("============================================")
    For Each c In r.Table.Columns
        Console.WriteLine(c.ColumnName + " = " + r(c).ToString)
    Next
Next

Visual C#에서 DataTable 개체를 반환하는 열거형 메서드 사용

이 섹션에서는 열거형 메서드를 호출하는 방법 및 반환된 DataTable 개체의 데이터를 처리하는 방법에 대해 설명합니다.

EnumCollations 메서드는 시스템 DataTable 개체를 반환합니다. SQL Server 인스턴스에 대한 모든 데이터 정렬 정보에 액세스하려면 DataTable 개체를 추가적으로 탐색해야 합니다.

//Connect to the local, default instance of SQL Server. 
{ 
Server srv = default(Server); 
srv = new Server(); 
//Call the EnumCollations method and return collation information to DataTable variable. 
DataTable d = default(DataTable); 
//Select the returned data into an array of DataRow. 
d = srv.EnumCollations; 
//Iterate through the rows and display collation details for the instance of SQL Server. 
DataRow r = default(DataRow); 
DataColumn c = default(DataColumn); 
foreach ( r in d.Rows) { 
  Console.WriteLine("====================================="); 
  foreach ( c in r.Table.Columns) { 
    Console.WriteLine(c.ColumnName + " = " + r(c).ToString); 
  } 
} 
} 

Visual Basic에서 개체 생성

New 연산자를 사용하여 모든 개체의 생성자를 호출할 수 있습니다. Database 개체 생성자는 오버로드되며 예제에 사용된 Database 개체 생성자의 버전은 매개 변수 두 개, 즉 데이터베이스가 속해 있는 부모 Server 개체와 새 데이터베이스의 이름을 나타내는 문자열을 사용합니다.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Declare and define a Database object by supplying the parent server and the database name arguments in the constructor.
Dim d As Database
d = New Database(srv, "Test_SMO_Database")
'Create the database on the instance of SQL Server.
d.Create()
Console.WriteLine(d.Name)

Visual C#에서 개체 생성

New 연산자를 사용하여 모든 개체의 생성자를 호출할 수 있습니다. Database 개체 생성자는 오버로드되며 예제에 사용된 Database 개체 생성자의 버전은 매개 변수 두 개, 즉 데이터베이스가 속해 있는 부모 Server 개체와 새 데이터베이스의 이름을 나타내는 문자열을 사용합니다.

{ 
Server srv; 
srv = new Server(); 
Table tb; 
tb = srv.Databases("AdventureWorks").Tables("Employee", "HumanResources"); 
tb.RebuildIndexes(70); 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Declare and define a Database object by supplying the parent server and the database name arguments in the constructor. 
Database d; 
d = new Database(srv, "Test_SMO_Database"); 
//Create the database on the instance of SQL Server. 
d.Create(); 
Console.WriteLine(d.Name); 
}

Visual Basic에서 SMO 개체 복사

이 코드 예에서는 Copy 메서드를 사용하여 Server 개체의 복사본을 만듭니다. Server 개체는 SQL Server 인스턴스에 대한 연결을 나타냅니다.

'Connect to the local, default instance of SQL Server.
Dim srv1 As Server
srv1 = New Server()
'Modify the default database and the timeout period for the connection.
srv1.ConnectionContext.DatabaseName = "AdventureWorks"
srv1.ConnectionContext.ConnectTimeout = 30
'Make a second connection using a copy of the ConnectionContext property and verify settings.
Dim srv2 As Server
srv2 = New Server(srv1.ConnectionContext.Copy)
Console.WriteLine(srv2.ConnectionContext.ConnectTimeout.ToString)

Visual C#에서 SMO 개체 복사

이 코드 예에서는 Copy 메서드를 사용하여 Server 개체의 복사본을 만듭니다. Server 개체는 SQL Server의 인스턴스에 대한 연결을 나타냅니다.

{ 
//Connect to the local, default instance of SQL Server. 
Server srv1; 
srv1 = new Server(); 
//Modify the default database and the timeout period for the connection. 
srv1.ConnectionContext.DatabaseName = "AdventureWorks"; 
srv1.ConnectionContext.ConnectTimeout = 30; 
//Make a second connection using a copy of the ConnectionContext property and verify settings. 
Server srv2; 
srv2 = new Server(srv1.ConnectionContext.Copy); 
Console.WriteLine(srv2.ConnectionContext.ConnectTimeout.ToString); 
}

Visual Basic에서 서버 프로세스 모니터링

열거형 메서드를 사용하면 SQL Server의 인스턴스에 대한 현재 상태 형식 정보를 얻을 수 있습니다. 코드 예에서는 EnumProcesses 메서드를 사용하여 현재 프로세스에 대한 정보를 확인합니다. 또한 이 예에서는 반환된 DataTable 개체의 열과 행으로 작업하는 방법도 보여 줍니다.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Call the EnumCollations method and return collation information to DataTable variable.
Dim d As DataTable
'Select the returned data into an array of DataRow.
d = srv.EnumProcesses
'Iterate through the rows and display collation details for the instance of SQL Server.
Dim r As DataRow
Dim c As DataColumn
For Each r In d.Rows
    Console.WriteLine("============================================")
    For Each c In r.Table.Columns
        Console.WriteLine(c.ColumnName + " = " + r(c).ToString)
    Next
Next

Visual C#에서 서버 프로세스 모니터링

열거형 메서드를 사용하면 SQL Server의 인스턴스에 대한 현재 상태 형식 정보를 얻을 수 있습니다. 코드 예에서는 EnumProcesses 메서드를 사용하여 현재 프로세스에 대한 정보를 확인합니다. 또한 이 예에서는 반환된 DataTable 개체의 열과 행으로 작업하는 방법도 보여 줍니다.

//Connect to the local, default instance of SQL Server. 
{ 
Server srv = default(Server); 
srv = new Server(); 
//Call the EnumCollations method and return collation information to DataTable variable. 
DataTable d = default(DataTable); 
//Select the returned data into an array of DataRow. 
d = srv.EnumProcesses; 
//Iterate through the rows and display collation details for the instance of SQL Server. 
DataRow r = default(DataRow); 
DataColumn c = default(DataColumn); 
foreach ( r in d.Rows) { 
  Console.WriteLine("================================="); 
  foreach ( c in r.Table.Columns) { 
    Console.WriteLine(c.ColumnName + " = " + r(c).ToString); 
  } 
} 
} 

참고 항목

참조