Udostępnij za pośrednictwem


Calling Methods

Dotyczy:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsBaza danych SQL w usłudze Microsoft Fabric (wersja zapoznawcza)

Metody wykonują określone zadania związane z obiektem, takie jak wystawianie punktu kontrolnego w bazie danych lub żądanie wyliczonej listy logowania dla wystąpienia programu Microsoft SQL Server.

Metody wykonują operację na obiekcie. Metody mogą przyjmować parametry i często mają wartość zwracaną. Wartość zwracana może być prostym typem danych, obiektem złożonym lub strukturą zawierającą wiele elementów członkowskich.

Użyj obsługi wyjątków, aby wykryć, czy metoda zakończyła się pomyślnie. Aby uzyskać więcej informacji, zobacz Obsługa wyjątków SMO.

Examples

Aby użyć dowolnego podanego przykładu kodu, musisz wybrać środowisko programowania, szablon programowania i język programowania, w którym ma zostać utworzona aplikacja. Aby uzyskać więcej informacji, zobacz Create a Visual C# SMO Project in Visual Studio .NET(Tworzenie projektu SMO w programie Visual Studio .NET).

Używanie prostej metody SMO w Visual Basic

W tym przykładzie Create metoda nie przyjmuje parametrów i nie ma wartości zwracanej.

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

Używanie prostej metody SMO w visual C#

W tym przykładzie Create metoda nie przyjmuje parametrów i nie ma wartości zwracanej.

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

Używanie metody SMO z parametrem w Visual Basic

Obiekt Table ma metodę o nazwie RebuildIndexes. Ta metoda wymaga parametru liczbowego, który określa FillFactor.

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

Używanie metody SMO z parametrem w visual C#

Obiekt Table ma metodę o nazwie RebuildIndexes. Ta metoda wymaga parametru liczbowego, który określa FillFactor.

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

Używanie metody wyliczenia zwracającej obiekt DataTable w Visual Basic

W tej sekcji opisano sposób wywoływania metody wyliczenia i sposobu obsługi danych w zwracanym DataTable obiekcie.

Metoda EnumCollations zwraca DataTable obiekt, który wymaga dalszej nawigacji w celu uzyskania dostępu do wszystkich dostępnych informacji sortowania dotyczących wystąpienia programu 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  

Używanie metody wyliczenia zwracającej obiekt DataTable w visual C#

W tej sekcji opisano sposób wywoływania metody wyliczenia i sposobu obsługi danych w zwracanym DataTable obiekcie.

Metoda EnumCollations zwraca obiekt systemowy DataTable . Obiekt DataTable wymaga dalszej nawigacji, aby uzyskać dostęp do wszystkich dostępnych informacji sortowania dotyczących wystąpienia programu 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);   
  }   
}   
}   

Konstruowanie obiektu w Visual Basic

Konstruktor dowolnego obiektu można wywołać za pomocą operatora New . Konstruktor Database obiektu jest przeciążony, a wersja Database konstruktora obiektu używanego w przykładzie przyjmuje dwa parametry: obiekt nadrzędny Server , do którego należy baza danych, oraz ciąg reprezentujący nazwę nowej bazy danych.

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

Konstruowanie obiektu w visual C#

Konstruktor dowolnego obiektu można wywołać za pomocą operatora New . Konstruktor Database obiektu jest przeciążony, a wersja Database konstruktora obiektu używanego w przykładzie przyjmuje dwa parametry: obiekt nadrzędny Server , do którego należy baza danych, oraz ciąg reprezentujący nazwę nowej bazy danych.

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

Kopiowanie obiektu SMO w Visual Basic

W tym przykładzie kodu użyto Copy metody do utworzenia kopii Server obiektu. Obiekt Server reprezentuje połączenie z wystąpieniem programu 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)

Kopiowanie obiektu SMO w programie Visual C#

W tym przykładzie kodu użyto Copy metody do utworzenia kopii Server obiektu. Obiekt Server reprezentuje połączenie z wystąpieniem programu 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);   
}  

Monitorowanie procesów serwera w Visual Basic

Bieżące informacje o typie stanu dotyczące wystąpienia programu SQL Server można uzyskać za pomocą metod wyliczenia. W przykładzie kodu użyto EnumProcesses metody , aby odnaleźć informacje o bieżących procesach. Pokazuje również, jak pracować z kolumnami i wierszami w zwróconym DataTable obiekcie.

'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

Monitorowanie procesów serwera w programie Visual C#

Bieżące informacje o typie stanu dotyczące wystąpienia programu SQL Server można uzyskać za pomocą metod wyliczenia. W przykładzie kodu użyto EnumProcesses metody , aby odnaleźć informacje o bieżących procesach. Pokazuje również, jak pracować z kolumnami i wierszami w zwróconym DataTable obiekcie.

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

See Also

Server
ServerConnection