Udostępnij za pośrednictwem


Wywołanie metody

Methods perform specific tasks related to the object, such as issuing a Checkpoint on a database or requesting an enumerated list of logons for the instance of Microsoft SQL Server.

Metody wykonania operacji na obiekcie.Wykorzystać parametrów metod i często mają wartości zwracanej.Zwracana wartość może być typu prostego danych, złożonych obiektu lub strukturę, która zawiera wiele składników.

Obsługa wyjątków służy do wykrywać, czy metoda została pomyślnie.Aby uzyskać więcej informacji zobaczObsługa wyjątków obiektów SMO.

Przykłady

To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see "How to: Create a Visual Basic SMO Project in Visual Studio .NET" or "How to: Create a Visual C# SMO Project in Visual Studio .NET" in SQL Server Books Online.

W języku Visual Basic przy użyciu metoda Simple obiektów SMO

W tym przykładzie Create() metoda pobiera bez parametrów i nie ma wartości zwracanej.

W środowisku Visual C# za pomocą metoda Simple obiektów SMO

W tym przykładzie Create() metoda pobiera bez 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(); 

}

Parametr w języku Visual Basic przy użyciu innej metoda obiektów SMO

The Table object has a metoda called RebuildIndexes(Int32). Ta metoda wymaga, aby parametr numerycznych, który określa FillFactor.

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

Parametr w środowisku Visual C# przy użyciu innej metoda obiektów SMO

The Table object has a metoda called RebuildIndexes(Int32). Ta metoda wymaga, aby parametr numerycznych, który określa FillFactor.

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

Za pomocą metoda wyliczania, która zwraca obiekt elementu DataTable w języku Visual Basic

W tej sekcji opisano, jak wywołać metoda wyliczania i sposób obsługi danych w zwrócone DataTable obiekt.

The EnumCollations() metoda returns a DataTable object, which requires further navigation to access all available sortowanie information about the wystąpienie of 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

Za pomocą metoda wyliczania, która zwraca obiekt elementu DataTable w środowisku Visual C#

W tej sekcji opisano, jak wywołać metoda wyliczania i sposób obsługi danych w zwrócone DataTable obiekt.

The EnumCollations() metoda returns a system DataTable object. The DataTable object requires further navigation to access all available sortowanie information about the wystąpienie of 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); 
  } 
} 
} 

Tworząc obiekt w języku Visual Basic

Konstruktor dowolny obiekt może zostać wywołana za pomocą New operator. The Database object constructor is overloaded and the wersja of the Database object constructor that is used in the sample takes two parameters: nadrzędnyServer obiekt, do której należy dany bazy danych, a ciąg znaków reprezentujący nazwę nowej bazy danych.

Konstruowanie obiektu w środowisku Visual C#

Konstruktor dowolny obiekt może zostać wywołana za pomocą New operator. The Database object constructor is overloaded and the wersja of the Database object constructor that is used in the sample takes two parameters: nadrzędnyServer obiekt, do której należy dany bazy danych, a ciąg znaków reprezentujący nazwę nowej bazy danych.

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

Kopiowanie obiektu obiekty SMO w języku Visual Basic

W tym przykładzie kodu Copy() metoda, aby utworzyć kopię Server obiekt. The Server object represents a connection to an wystąpienie of SQL Server.

Kopiowanie obiektu obiekty SMO w środowisku Visual C#

W tym przykładzie kodu Copy() metoda, aby utworzyć kopię Server obiekt. The Server object represents a connection to an wystąpienie of 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); 
}

Monitorowanie procesów serwera w języku Visual Basic

Można uzyskać bieżące informacje typ stanu o wystąpieniu SQL Server za pomocą metody wyliczania. W przykładzie kodu EnumProcesses() Metoda odnajdywania informacji o bieżących procesów. Ilustruje też sposób pracy z kolumnami i wierszami w zwrócone DataTable obiekt.

Monitorowanie procesów serwera w środowisku Visual C#

Można uzyskać bieżące informacje typ stanu o wystąpieniu SQL Server za pomocą metody wyliczania. W przykładzie kodu EnumProcesses() Metoda odnajdywania informacji o bieżących procesów. Ilustruje też sposób pracy z kolumnami i wierszami w zwrócone DataTable obiekt.

//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

Reference

Server

ServerConnection