Share via


ServerConnection Class

The ServerConnection object represents a connection to a server.

Inheritance Hierarchy

Object
  Microsoft.SqlServer.Management.Common.ConnectionSettings
    Microsoft.SqlServer.Management.Common.ConnectionManager
      Microsoft.SqlServer.Management.Common.ServerConnection

Namespace:  Microsoft.SqlServer.Management.Common
Assembly:  Microsoft.SqlServer.ConnectionInfo (in Microsoft.SqlServer.ConnectionInfo.dll)

Syntax

'Declaration
Public NotInheritable Class ServerConnection _
    Inherits ConnectionManager _
    Implements ISfcConnection
'Usage
Dim instance As ServerConnection
public sealed class ServerConnection : ConnectionManager, 
    ISfcConnection
public ref class ServerConnection sealed : public ConnectionManager, 
    ISfcConnection
[<SealedAttribute>]
type ServerConnection =  
    class 
        inherit ConnectionManager 
        interface ISfcConnection 
    end
public final class ServerConnection extends ConnectionManager implements ISfcConnection

The ServerConnection type exposes the following members.

Constructors

  Name Description
Public method ServerConnection() Initializes a new instance of the ServerConnection class.
Public method ServerConnection(SqlConnection) Initializes a new instance of the ServerConnection class with the specified connection parameters.
Public method ServerConnection(String) Initializes a new instance of the ServerConnection class with the specified server instance.
Public method ServerConnection(SqlConnectionInfo) Initializes a new instance of the ServerConnection class with the specified connection information.
Public method ServerConnection(String, String, SecureString) Initializes a new instance of the ServerConnection class with the specified server instance and logon credentials.
Public method ServerConnection(String, String, String) Initializes a new instance of the ServerConnection class with the specified server instance and logon credentials.

Top

Properties

  Name Description
Public property ApplicationName Gets or sets the name of the application that has established the connection with the server. (Inherited from ConnectionSettings.)
Public property AutoDisconnectMode Gets or sets the automatic disconnection mode of the referenced connection to the instance of SQL Server. (Inherited from ConnectionManager.)
Public property BatchSeparator Gets or sets the string value used to separate batches in the referenced connection.
Public property CapturedSql Gets the Transact-SQL statements that are captured when the connection is running in capture mode. (Inherited from ConnectionManager.)
Public property ConnectAsUser Gets or sets the Boolean property value that specifies whether the connection is established using impersonation. (Inherited from ConnectionSettings.)
Public property ConnectAsUserName Gets or sets the name of the user when connecting using impersonation. (Inherited from ConnectionSettings.)
Public property ConnectAsUserPassword Gets or sets the password for the user when connecting using impersonation. (Inherited from ConnectionSettings.)
Public property ConnectionString Gets or sets the connection string used to establish a connection with a specified data source. (Inherited from ConnectionSettings.)
Public property ConnectionType Gets the type of connection to use in the server.
Public property ConnectTimeout Gets or sets the connection time-out period in seconds. (Inherited from ConnectionSettings.)
Public property DatabaseEngineType Gets the database engine type. (Inherited from ConnectionManager.)
Public property DatabaseName Gets or sets the database required by the connection. (Inherited from ConnectionSettings.)
Public property EncryptConnection Gets or sets the Boolean property value that specifies whether the connection is encrypted. (Inherited from ConnectionSettings.)
Public property FixedServerRoles Gets the fixed server role associated with the referenced connection.
Public property InUse Gets or sets the Boolean property value that specifies whether the connection is being used. (Inherited from ConnectionManager.)
Public property IsForceDisconnected Gets a Boolean value that specifies whether the connection was forcibly disconnected. (Inherited from ConnectionManager.)
Public property IsOpen Gets the Boolean property value that specifies whether the connection is currently open. (Inherited from ConnectionManager.)
Public property LockTimeout Gets or sets the time in seconds required for the connection to time out when the current transaction is locked. (Inherited from ConnectionManager.)
Public property Login Gets or sets the logon required to establish a connection with the instance of the server. (Inherited from ConnectionSettings.)
Public property LoginSecure Gets or sets the Boolean property that specifies whether the connection is established to the server by using Windows Authentication or SQL Server Authentication. (Inherited from ConnectionSettings.)
Public property MaxPoolSize Gets or sets the maximum number of connections in a connection pool that the specified connection accepts when establishing a connection with a server. (Inherited from ConnectionSettings.)
Public property MinPoolSize Gets or sets the minimum number of connections in a connection pool that the specified connection accepts when establishing a connection with a server. (Inherited from ConnectionSettings.)
Public property MultipleActiveResultSets Gets or sets the Boolean property value that specifies whether more than one active result set is permitted. (Inherited from ConnectionSettings.)
Public property NetworkProtocol Gets or sets the network protocol used to establish the connection to the server. (Inherited from ConnectionSettings.)
Public property NonPooledConnection Gets or sets the Boolean property value that specifies whether the connection may be pooled or nonpooled. (Inherited from ConnectionSettings.)
Public property PacketSize Gets or sets the packets of data sent between the client and the server over the network in bytes. (Inherited from ConnectionSettings.)
Public propertyStatic member ParameterizationMode Gets or sets the parameterization mode for the server connection.
Public property Password Gets or sets the password used with the logon to establish a connection with the server when SQL Server Authentication is used. (Inherited from ConnectionSettings.)
Public property PooledConnectionLifetime Gets or sets the lifetime of the pooled connection. (Inherited from ConnectionSettings.)
Public property ProcessID Gets the ID value that uniquely identifies the process associated with the referenced connection to the server.
Public property SecureConnectionString Gets or sets an encrypted version of the connection string. (Inherited from ConnectionSettings.)
Public property SecurePassword Gets or sets the secure password used with the logon to establish a connection with the server when SQL Server Authentication is used. (Inherited from ConnectionSettings.)
Public property ServerInstance Gets or sets the name of the server. (Inherited from ConnectionSettings.)
Public property ServerVersion Gets the version of the connected instance of SQL Server. (Inherited from ConnectionManager.)
Public property SqlConnectionObject Gets the connection object. (Inherited from ConnectionManager.)
Public property SqlExecutionModes Gets or sets the execution mode associated with the referenced connection to the server.
Public property StatementTimeout Gets or sets the number of seconds a statement is given to run before failing with a time-out error.
Public property TransactionDepth Gets the current transaction depth for the referenced connection.
Public property TrueLogin Gets the current logon record name used by the referenced connection.
Public property TrueName Gets the name of the server.
Public propertyStatic member UseMode Gets or sets the server connection using a specified mode.
Public property UserProfile Gets the user profile associated with the referenced connection.
Public property WorkstationId Gets or sets the ID value that uniquely identifies the workstation. (Inherited from ConnectionSettings.)

Top

Methods

  Name Description
Public method BeginTransaction Starts a transaction on the referenced connection.
Public method Cancel Cancels the connection.
Public method ChangePassword(SecureString) Changes the login password that is used with standard authentication.
Public method ChangePassword(String) Changes the logon password that is used with standard authentication.
Public method CommitTransaction Commits the current transaction on the referenced connection.
Public method Connect Connects to the instance of SQL Server. (Inherited from ConnectionManager.)
Public method Copy Returns a copy of the ServerConnection object.
Public method Disconnect Disconnects from the instance of SQL Server. (Inherited from ConnectionManager.)
Public method Equals (Inherited from Object.)
Public method ExecuteNonQuery(StringCollection) Executes a batch of statements that do not return a result set.
Public method ExecuteNonQuery(String) Executes a statement that does not return a result set.
Public method ExecuteNonQuery(StringCollection, ExecutionTypes) Executes a batch of statements with the specified execution options that does not return a result set.
Public method ExecuteNonQuery(String, ExecutionTypes) Executes a statement with the specified execution options that does not return a result set.
Public method ExecuteReader(String) Executes Transact-SQL statements that return one or multiple result sets.
Public method ExecuteReader(String, SqlCommand%) Executes a reader using a SqlDataReader object value that contains one or more result sets.
Public method ExecuteScalar(StringCollection) Executes a batch of Transact-SQL statements and returns the first column of the first row as an object value.
Public method ExecuteScalar(String) Executes a Transact-SQL statement and returns the first column of the first row as an object value.
Public method ExecuteWithResults(StringCollection) Executes a batch of Transact-SQL statements that return multiple result sets.
Public method ExecuteWithResults(String) Executes a Transact-SQL statement that returns a result set.
Public method ForceDisconnected Forces a disconnect from the instance of SQL Server. (Inherited from ConnectionManager.)
Public method GetHashCode (Inherited from Object.)
Public method GetType (Inherited from Object.)
Public method IsInFixedServerRole Tests whether the logon associated with the referenced connection is a member of the specified fixed server role.
Public methodStatic member NormalizeQuery(String) Uses a string input that represents the query text.
Public methodStatic member NormalizeQuery(String, Boolean) Normalizes the query that controls the server connection.
Public method RollBackTransaction Rolls back the current transaction.
Public method ToString Returns the referenced object name as a string. (Inherited from ConnectionSettings.)

Top

Events

  Name Description
Public event InfoMessage The event that occurs when the instance of SQL Server generates an informational message.  (Inherited from ConnectionManager.)
Public event RemoteLoginFailed This event occurs when a remote login fails to connect to the instance of SQL Server. (Inherited from ConnectionManager.)
Public event ServerMessage The event that occurs when SQL Server returns a warning or informational message on the current connection. (Inherited from ConnectionManager.)
Public event StateChange The event that occurs when the instance of SQL Server changes state.  (Inherited from ConnectionManager.)
Public event StatementExecuted The event that occurs when the instance of SQL Server executes a statement on the current connection.  (Inherited from ConnectionManager.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method ISfcConnection.Connect Returns a value that indicates the server to connect using an SFC connection.
Explicit interface implemetationPrivate method ISfcConnection.Copy Uses the SFC connection to copy the server connection.
Explicit interface implemetationPrivate method ISfcConnection.Disconnect Disconnects the connection.
Explicit interface implemetationPrivate property ISfcConnection.ServerVersion Gets the version of the server.
Explicit interface implemetationPrivate method ISfcConnection.ToEnumeratorObject Renders an enumerator object for the server connection.

Top

Remarks

The ServerConnection class is served by the Server class and replication classes such as the ReplicationServer class.

Typically, you don't have to declare an instance of the ServerConnection object because it is created automatically with the Server object. Declare a ServerConnection object variable if you want to store the connection settings for re-use. The ConnectionContext property of the Server object points to a ServerConnection object. The ServerConnection object contains properties that relate to the connection between the Server object and the instance of SQL Server. You can use it to view or modify certain aspects of the connection, such as the process ID, processing transactions, the authentication mode, execution mode, and even to run Transact-SQL commands directly.

By using the ServerConnection object, you can do the following:

  • Run Transact-SQL statements directly over the connection to the instance of SQL Server.

  • Change the connection settings, such as the authentication method.

  • Begin, commit and roll back transactions.

  • Capture the Transact-SQL statements generated by the SMO application.

Examples

For another example of this method with the Database object, see [M:Microsoft.SqlServer.Management.Smo.Database.ExecuteNonQuery(System.String].

VB

'Declare a ServerConnection object variable to specify SQL authentication, login and password.
Dim conn As New ServerConnection
conn.LoginSecure = False
conn.Login = vlogin
conn.Password = vpassword
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server(conn)
'The actual connection is made when a property is retrieved.
Console.WriteLine(srv.Information.Version)
'The connection is automatically disconnected when the Server variable goes out of scope.

PowerShell

$conn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$conn.LoginSecure = $FALSE
$conn.Login = vlogin
$conn.Password = vpassword
#Connect to the local, default instance of SQL Server
$srv = new-object Microsoft.SqlServer.Management.Smo.Server($conn)
Write-Host $srv.Information.Version

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.SqlServer.Management.Common Namespace

Other Resources

Connecting to an Instance of SQL Server

Connecting to an Instance of SQL Server