호출 메서드

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

메서드는 데이터베이스에서 검사점을 발급하거나 Microsoft SQL Server 인스턴스에 대해 열거된 로그온 목록을 요청하는 등 개체와 관련된 특정 작업을 수행합니다.

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

예외 처리를 사용하여 메서드가 성공했는지 여부를 검색합니다. 자세한 내용은 Handling SMO Exceptions을 참조하세요.

예제

제공된 코드 예제를 사용하려면 프로그래밍 환경, 프로그래밍 템플릿 및 애플리케이션을 만들 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 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("AdventureWorks2022").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("AdventureWorks2022").Tables("Employee", "HumanResources");   
tb.RebuildIndexes(70);   
}   

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

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

이 메서드는 EnumCollations SQL Server 인스턴스에 대해 사용 가능한 모든 데이터 정렬 정보에 액세스하기 위해 추가 탐색이 필요한 개체를 반환 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.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 개체를 반환합니다. DataTable SQL Server 인스턴스에 대해 사용 가능한 모든 데이터 정렬 정보에 액세스하려면 개체를 추가로 탐색해야 합니다.

//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("AdventureWorks2022").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 = "AdventureWorks2022"
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 = "AdventureWorks2022";   
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);   
  }   
}   
}   

참고 항목

Server
ServerConnection