呼叫方法

方法會執行與物件相關的特定工作,例如,在資料庫上發出 Checkpoint,或要求 Microsoft SQL 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("AdventureWorks2008R2").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("AdventureWorks2008R2").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("AdventureWorks2008R2").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 = "AdventureWorks2008R2"
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 = "AdventureWorks2008R2"; 
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); 
  } 
} 
} 

請參閱

參考