共用方式為


呼叫方法

適用於:SQL ServerAzure SQL 資料庫 Azure SQL 受控執行個體 Azure Synapse Analytics

方法會執行與 對象相關的特定工作,例如在資料庫上發出 檢查點 ,或要求Microsoft SQL Server 實例的列舉登入清單。

方法會在物件上執行作業。 方法可以採用參數,而且通常會有傳回值。 傳回值可以是簡單數據類型、複雜物件或包含許多成員的結構。

使用例外狀況處理來偵測方法是否成功。 如需詳細資訊,請參閱 處理 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("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 會傳回 物件,需要進一 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 建構函式會多載,而且範例中使用的物件建構函式版本會採用兩個參數:資料庫所屬的 DatabaseServer 物件,以及代表新資料庫名稱的字串。

'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 建構函式會多載,而且範例中使用的物件建構函式版本會採用兩個參數:資料庫所屬的 DatabaseServer 物件,以及代表新資料庫名稱的字串。

{   
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