调用方法

方法执行与对象有关的特定任务,如对数据库发出 Checkpoint 或为 MicrosoftSQL Server 实例请求枚举的登录名列表。

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

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

示例

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

在 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 对象。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("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); 
  } 
} 
} 

请参阅

参考