--- title: "Calling Methods | Microsoft Docs" ms.custom: "" ms.date: "08/06/2017" ms.prod: "sql-server-2016" ms.reviewer: "" ms.suite: "" ms.technology: - "docset-sql-devref" ms.tgt_pltfrm: "" ms.topic: "reference" helpviewer_keywords: - "methods [SMO]" - "calling methods" - "SQL Server Management Objects, method calling" - "SMO [SQL Server], method calling" ms.assetid: c88d5c5f-9ff0-4f84-b2b6-24c6b90fa15e caps.latest.revision: 46 author: "JennieHubbard" ms.author: "jhubbard" manager: "jhubbard" --- # Calling Methods Methods perform specific tasks related to the object, such as issuing a **Checkpoint** on a database or requesting an enumerated list of logons for the instance of [!INCLUDE[msCoName](../../../includes/msconame-md.md)] [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)]. Methods perform an operation on an object. Methods can take parameters and often have a return value. The return value can be a simple data type, a complex object, or a structure that contains many members. Use exception handling to detect whether the method has been successful. For more information, see [Handling SMO Exceptions](../../../relational-databases/server-management-objects-smo/create-program/handling-smo-exceptions.md). ## Examples To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see [Create a Visual C# SMO Project in Visual Studio .NET](../../../relational-databases/server-management-objects-smo/how-to-create-a-visual-csharp-smo-project-in-visual-studio-net.md). ## Using a Simple SMO Method in Visual Basic In this example, the method takes no parameters and has no return value. ```VBNET '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() ``` ## Using a Simple SMO Method in Visual C# In this example, the method takes no parameters and has no return value. ```csharp { //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(); ``` } ## Using an SMO Method with a Parameter in Visual Basic The object has a method called . This method requires a numeric parameter that specifies the **FillFactor**. ```VBNET Dim srv As Server srv = New Server Dim tb As Table tb = srv.Databases("AdventureWorks2012").Tables("Employee", "HumanResources") tb.RebuildIndexes(70) ``` ## Using an SMO Method with a Parameter in Visual C# The object has a method called . This method requires a numeric parameter that specifies the `FillFactor`. ```csharp { Server srv = default(Server); srv = new Server(); Table tb = default(Table); tb = srv.Databases("AdventureWorks2012").Tables("Employee", "HumanResources"); tb.RebuildIndexes(70); } ``` ## Using an Enumeration Method that Returns a DataTable Object in Visual Basic This section describes how to call an enumeration method and how to handle the data in the returned object. The method returns a object, which requires further navigation to access all available collation information about the instance of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)]. ```VBNET '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 ``` ## Using an Enumeration Method that Returns a DataTable Object in Visual C# This section describes how to call an enumeration method and how to handle the data in the returned object. The method returns a system object. The object requires further navigation to access all available collation information about the instance of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)]. ```csharp //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); } } } ``` ## Constructing an Object in Visual Basic The constructor of any object can be called by using the **New** operator. The object constructor is overloaded and the version of the object constructor that is used in the sample takes two parameters: the parent object to which the database belongs, and a string that represents the name of the new database. ```VBNET '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) ``` ## Constructing an Object in Visual C# The constructor of any object can be called by using the **New** operator. The object constructor is overloaded and the version of the object constructor that is used in the sample takes two parameters: the parent object to which the database belongs, and a string that represents the name of the new database. ```csharp { Server srv; srv = new Server(); Table tb; tb = srv.Databases("AdventureWorks2012").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); } ``` ## Copying an SMO Object in Visual Basic This code example uses the method to create a copy of the object. The object represents a connection to an instance of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)]. ```VBNET '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 = "AdventureWorks2012" 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) ``` ## Copying an SMO Object in Visual C# This code example uses the method to create a copy of the object. The object represents a connection to an instance of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)]. ```csharp { //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 = "AdventureWorks2012"; 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); } ``` ## Monitoring Server Processes in Visual Basic You can obtain the current status type information about the instance of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] through enumeration methods. The code example uses the method to discover information about the current processes. It also demonstrates how to work with the columns and rows in the returned object. ```VBNET '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 ``` ## Monitoring Server Processes in Visual C# You can obtain the current status type information about the instance of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] through enumeration methods. The code example uses the method to discover information about the current processes. It also demonstrates how to work with the columns and rows in the returned object. ```csharp //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); } } } ``` ## See Also