Chiamata di metodi

I metodi eseguono attività specifiche correlate all'oggetto, ad esempio l'esecuzione di un oggetto Checkpoint in un database o la richiesta di un elenco enumerato di accessi per l'istanza di Microsoft SQL Server.

I metodi eseguono un'operazione su un oggetto. I metodi possono accettare parametri e spesso restituiscono un valore. Il valore restituito può essere un tipo di dati semplice, un oggetto complesso o una struttura che contiene molti membri.

Utilizzare la gestione delle eccezioni per determinare se il metodo è stato eseguito correttamente. Per ulteriori informazioni, vedere Gestione delle eccezioni SMO.

Esempi

Per utilizzare qualsiasi esempio di codice fornito, è necessario scegliere l'ambiente di programmazione, il modello di programmazione e il linguaggio di programmazione per la creazione dell'applicazione. Per ulteriori informazioni, vedere "Procedura: Creare un progetto Visual Basic SMO in Visual Studio .NET" o "Procedura: Creare un progetto Visual C# SMO in Visual Studio .NET" nella documentazione online di SQL Server.

Utilizzo di un metodo SMO semplice in Visual Basic

In questo esempio il metodo Create non accetta parametri e non restituisce alcun valore.

'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()

Utilizzo di un metodo SMO semplice in Visual C#

In questo esempio il metodo Create non accetta parametri e non restituisce alcun valore.

{ 
//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(); 

}

Utilizzo di un metodo SMO con un parametro in Visual Basic

L'oggetto Table include un metodo denominato RebuildIndexes. Questo metodo richiede un parametro numerico che specifica FillFactor.

Dim srv As Server
srv = New Server
Dim tb As Table
tb = srv.Databases("AdventureWorks2008R2").Tables("Employee", "HumanResources")
tb.RebuildIndexes(70)

Utilizzo di un metodo SMO con un parametro in Visual C#

L'oggetto Table include un metodo denominato RebuildIndexes. Questo metodo richiede un parametro numerico che specifica FillFactor.

{ 
Server srv = default(Server); 
srv = new Server(); 
Table tb = default(Table); 
tb = srv.Databases("AdventureWorks2008R2").Tables("Employee", "HumanResources"); 
tb.RebuildIndexes(70); 
} 

Utilizzo di un metodo di enumerazione che restituisce un oggetto DataTable in Visual Basic

In questa sezione viene descritto come chiamare un metodo di enumerazione e come gestire i dati nell'oggetto DataTable restituito.

Il metodo EnumCollations restituisce un oggetto DataTable, che richiede ulteriore navigazione per accedere a tutte le informazioni sulle regole di confronto disponibili relative all'istanza di 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

Utilizzo di un metodo di enumerazione che restituisce un oggetto DataTable in Visual C#

In questa sezione viene descritto come chiamare un metodo di enumerazione e come gestire i dati nell'oggetto DataTable restituito.

Il metodo EnumCollations restituisce un oggetto DataTable di sistema. L'oggetto DataTable richiede ulteriore navigazione per accedere a tutte le informazioni sulle regole di confronto disponibili relative all'istanza di 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); 
  } 
} 
} 

Costruzione di un oggetto in Visual Basic

È possibile chiamare il costruttore di qualsiasi oggetto utilizzando l'operatore New. Viene eseguito l'overload del costruttore dell'oggetto Database e la versione del costruttore dell'oggetto Database utilizzata nell'esempio accetta due parametri: l'oggetto Server padre cui appartiene il database e una stringa che rappresenta il nome del nuovo database.

'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)

Costruzione di un oggetto in Visual C#

È possibile chiamare il costruttore di qualsiasi oggetto utilizzando l'operatore New. Viene eseguito l'overload del costruttore dell'oggetto Database e la versione del costruttore dell'oggetto Database utilizzata nell'esempio accetta due parametri: l'oggetto Server padre cui appartiene il database e una stringa che rappresenta il nome del nuovo database.

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

Copia di un oggetto SMO in Visual Basic

In questo esempio di codice viene utilizzato il metodo Copy per creare una copia dell'oggetto Server. L'oggetto Server rappresenta una connessione a un'istanza di 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)

Copia di un oggetto SMO in Visual C#

In questo esempio di codice viene utilizzato il metodo Copy per creare una copia dell'oggetto Server. L'oggetto Server rappresenta una connessione a un'istanza di 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); 
}

Monitoraggio di processi server in Visual Basic

È possibile ottenere le informazioni sul tipo di stato corrente relative all'istanza di SQL Server utilizzando metodi di enumerazione. Nell'esempio di codice viene utilizzato il metodo EnumProcesses per individuare informazioni sui processi correnti. Viene inoltre illustrato come utilizzare le colonne e le righe nell'oggetto DataTable restituito.

'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

Monitoraggio di processi server in Visual C#

È possibile ottenere le informazioni sul tipo di stato corrente relative all'istanza di SQL Server utilizzando metodi di enumerazione. Nell'esempio di codice viene utilizzato il metodo EnumProcesses per individuare informazioni sui processi correnti. Viene inoltre illustrato come utilizzare le colonne e le righe nell'oggetto DataTable restituito.

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

Vedere anche

Riferimento