调用方法

方法执行与对象相关的特定任务,例如在数据库上发出 Checkpoint 或请求Microsoft SQL Server 实例的枚举登录列表。

方法对对象执行作。 方法可以采用参数,并且通常具有返回值。 返回值可以是简单数据类型、复杂对象或包含多个成员的结构。

使用异常处理检测方法是否成功。 有关详细信息,请参阅 处理 SMO 异常

例子

若要使用提供的任何代码示例,必须选择要在其中创建应用程序的编程环境、编程模板和编程语言。 有关详细信息,请参阅 SQL Server 联机丛书中的“如何:在 Visual Studio .NET 中创建 Visual Basic SMO 项目”或“如何:在 Visual Studio .NET 中创建 Visual C# SMO 项目”。

在 Visual Basic 中使用简单的 SMO 方法

在此示例中,该方法 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("AdventureWorks2012").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("AdventureWorks2012").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 对象。 该 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 对象,以及一个表示新数据库名称的字符串。

在 Visual C 中构造对象#

可以使用运算符调用 New 任何对象的构造函数。 对象 Database 构造函数重载,示例中使用的对象构造函数的版本 Database 采用两个参数:数据库所属的父 Server 对象,以及一个表示新数据库名称的字符串。

{   
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);   
}  

在 Visual Basic 中复制 SMO 对象

此代码示例使用 Copy 该方法创建对象的副本 Server 。 该 Server 对象表示与 SQL Server 实例的连接。

在 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 = "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);   
}  

在 Visual Basic 中监视服务器进程

可以通过枚举方法获取有关 SQL Server 实例的当前状态类型信息。 该代码示例使用 EnumProcesses 该方法来发现有关当前进程的信息。 它还演示如何处理返回 DataTable 的对象中的列和行。

在 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