ServerConnection 類別

ServerConnection 物件代表伺服器的連接。

繼承階層

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

命名空間:  Microsoft.SqlServer.Management.Common
組件:  Microsoft.SqlServer.ConnectionInfo (在 Microsoft.SqlServer.ConnectionInfo.dll 中)

語法

'宣告
Public NotInheritable Class ServerConnection _
    Inherits ConnectionManager _
    Implements ISfcConnection
'用途
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

ServerConnection 型別公開下列成員。

建構函式

  名稱 說明
公用方法 ServerConnection() 初始化 ServerConnection 類別的新執行個體。
公用方法 ServerConnection(SqlConnection) 使用指定的連接參數,初始化 ServerConnection 類別的新執行個體。
公用方法 ServerConnection(String) 使用指定的伺服器執行個體,初始化 ServerConnection 類別的新執行個體。
公用方法 ServerConnection(SqlConnectionInfo) 使用指定的連接資訊,初始化 ServerConnection 類別的新執行個體。
公用方法 ServerConnection(String, String, SecureString) 使用指定的伺服器執行個體和登入認證,初始化 ServerConnection 類別的新執行個體。
公用方法 ServerConnection(String, String, String) 使用指定的伺服器執行個體和登入認證,初始化 ServerConnection 類別的新執行個體。

回到頁首

屬性

  名稱 說明
公用屬性 ApplicationName 取得或設定已建立與伺服器連接的應用程式名稱。 (繼承自 ConnectionSettings。)
公用屬性 AutoDisconnectMode 取得或設定與 SQL Server 執行個體之參考連接的自動中斷連接模式。 (繼承自 ConnectionManager。)
公用屬性 BatchSeparator 取得或設定在參考的連接中用來分隔批次的字串值。
公用屬性 CapturedSql 取得在擷取模式下執行連接時所擷取的 Transact-SQL 陳述式。 (繼承自 ConnectionManager。)
公用屬性 ConnectAsUser 取得或設定 Boolean 屬性值,這個值指定是否會使用模擬來建立連接。 (繼承自 ConnectionSettings。)
公用屬性 ConnectAsUserName 取得或設定使用模擬進行連接時的使用者名稱。 (繼承自 ConnectionSettings。)
公用屬性 ConnectAsUserPassword 取得或設定使用模擬進行連接時的使用者密碼。 (繼承自 ConnectionSettings。)
公用屬性 ConnectionString 取得或設定建立與指定之資料來源的連接時所使用的連接字串。 (繼承自 ConnectionSettings。)
公用屬性 ConnectionType 取得要在伺服器中使用的連接類型。
公用屬性 ConnectTimeout 取得或設定連接逾時期限 (以秒為單位)。 (繼承自 ConnectionSettings。)
公用屬性 DatabaseEngineType 取得 Database Engine 類型。 (繼承自 ConnectionManager。)
公用屬性 DatabaseName 取得或設定連接所需的資料庫。 (繼承自 ConnectionSettings。)
公用屬性 EncryptConnection 取得或設定指定連線是否經過加密的 Boolean 屬性值。 (繼承自 ConnectionSettings。)
公用屬性 FixedServerRoles 取得與參考連接相關聯的固定伺服器角色。
公用屬性 InUse 取得或設定 Boolean 屬性值,這個值會指定是否正在使用連接。 (繼承自 ConnectionManager。)
公用屬性 IsForceDisconnected 取得 Boolean 值,這個值指定是否已強制中斷連接。 (繼承自 ConnectionManager。)
公用屬性 IsOpen 取得指定連接目前是否開啟的 Boolean 屬性值。 (繼承自 ConnectionManager。)
公用屬性 LockTimeout 取得或設定鎖定目前交易時連接逾時所需的時間 (以秒為單位)。 (繼承自 ConnectionManager。)
公用屬性 Login 取得或設定建立與伺服器執行個體的連接時所需要的登入。 (繼承自 ConnectionSettings。)
公用屬性 LoginSecure 取得或設定 Boolean 屬性,這個屬性指定要使用 Windows 驗證還是 SQL Server 驗證來建立伺服器連接。 (繼承自 ConnectionSettings。)
公用屬性 MaxPoolSize 取得或設定建立與伺服器的連接時,指定的連接在連接集區中接受的連接數目上限。 (繼承自 ConnectionSettings。)
公用屬性 MinPoolSize 取得或設定建立與伺服器的連接時,指定的連接在連接集區中接受的連接數目下限。 (繼承自 ConnectionSettings。)
公用屬性 MultipleActiveResultSets 取得或設定 Boolean 屬性值,這個值指定是否允許一個以上作用中的結果集。 (繼承自 ConnectionSettings。)
公用屬性 NetworkProtocol 取得或設定用來建立與伺服器之連接的網路通訊協定。 (繼承自 ConnectionSettings。)
公用屬性 NonPooledConnection 取得或設定指定連接是否可共用的 Boolean 屬性值。 (繼承自 ConnectionSettings。)
公用屬性 PacketSize 取得或設定透過網路在用戶端與伺服器之間傳送的資料封包 (以位元組為單位)。 (繼承自 ConnectionSettings。)
公用屬性靜態成員 ParameterizationMode 取得或設定伺服器連接的參數化模式。
公用屬性 Password 取得或設定使用 SQL Server 驗證時,建立伺服器連接之登入所使用的密碼。 (繼承自 ConnectionSettings。)
公用屬性 PooledConnectionLifetime 取得或設定共用連接的存留期間。 (繼承自 ConnectionSettings。)
公用屬性 ProcessID 取得識別碼值,這個值可唯一識別與伺服器之參考連接相關聯的處理序。
公用屬性 SecureConnectionString 取得或設定連接字串的加密版本。 (繼承自 ConnectionSettings。)
公用屬性 SecurePassword 取得或設定使用 SQL Server 驗證時,建立伺服器連接之登入所使用的安全密碼。 (繼承自 ConnectionSettings。)
公用屬性 ServerInstance 取得或設定伺服器的名稱。 (繼承自 ConnectionSettings。)
公用屬性 ServerVersion 取得連接之 SQL Server 執行個體的版本。 (繼承自 ConnectionManager。)
公用屬性 SqlConnectionObject 取得連接物件。 (繼承自 ConnectionManager。)
公用屬性 SqlExecutionModes 取得或設定與伺服器之參考連接相關聯的執行模式。
公用屬性 StatementTimeout 取得或設定陳述式因為逾時錯誤而失敗之前可以執行的秒數。
公用屬性 TransactionDepth 取得參考連接目前的交易深度。
公用屬性 TrueLogin 取得參考連接目前所使用的登入記錄名稱。
公用屬性 TrueName 取得伺服器的名稱。
公用屬性靜態成員 UseMode 取得或設定使用指定模式的伺服器連接。
公用屬性 UserProfile 取得與參考連接相關聯的使用者設定檔。
公用屬性 WorkstationId 取得或設定可唯一識別工作站的識別碼值。 (繼承自 ConnectionSettings。)

回到頁首

方法

  名稱 說明
公用方法 BeginTransaction 在參考的連接上啟動交易。
公用方法 Cancel 取消連接。
公用方法 ChangePassword(SecureString) 變更與標準驗證搭配使用的登入密碼。
公用方法 ChangePassword(String) 變更與標準驗證搭配使用的登入密碼。
公用方法 CommitTransaction 認可參考連接上目前的交易。
公用方法 Connect 連接到 SQL Server 的執行個體。 (繼承自 ConnectionManager。)
公用方法 Copy 傳回 ServerConnection 物件的複本。
公用方法 Disconnect 中斷與 SQL Server執行個體的連接。 (繼承自 ConnectionManager。)
公用方法 Equals (繼承自 Object。)
公用方法 ExecuteNonQuery(StringCollection) 執行不會傳回結果集的陳述式批次。
公用方法 ExecuteNonQuery(String) 執行不會傳回結果集的陳述式。
公用方法 ExecuteNonQuery(StringCollection, ExecutionTypes) 執行具有指定的執行選項而且不會傳回結果集的陳述式批次。
公用方法 ExecuteNonQuery(String, ExecutionTypes) 執行具有指定的執行選項而且不會傳回結果集的陳述式。
公用方法 ExecuteReader(String) 執行傳回一個或多個結果集的 Transact-SQL 陳述式。
公用方法 ExecuteReader(String, SqlCommand%) 使用包含一個或多個結果集的 SqlDataReader 物件值,執行讀取器。
公用方法 ExecuteScalar(StringCollection) 執行 Transact-SQL 陳述式的批次,並以物件值傳回第一個資料列的第一個資料行。
公用方法 ExecuteScalar(String) 執行 Transact-SQL 陳述式,並以物件值傳回第一個資料列的第一個資料行。
公用方法 ExecuteWithResults(StringCollection) 執行傳回多個結果集的 Transact-SQL 陳述式批次。
公用方法 ExecuteWithResults(String) 執行傳回結果集的 Transact-SQL 陳述式。
公用方法 ForceDisconnected 強制中斷與 SQL Server 執行個體的連接。 (繼承自 ConnectionManager。)
公用方法 GetHashCode (繼承自 Object。)
公用方法 GetType (繼承自 Object。)
公用方法 IsInFixedServerRole 測試與參考連接相關聯的登入是否為指定之固定伺服器角色的成員。
公用方法靜態成員 NormalizeQuery(String) 使用代表查詢文字的字串輸入。
公用方法靜態成員 NormalizeQuery(String, Boolean) 將控制伺服器連接的查詢正規化。
公用方法 RollBackTransaction 回復目前的交易。
公用方法 ToString 傳回字串格式的參考物件名稱。 (繼承自 ConnectionSettings。)

回到頁首

事件

  名稱 說明
公用事件 InfoMessage SQL Server 的執行個體產生資訊訊息時所發生的事件。  (繼承自 ConnectionManager。)
公用事件 RemoteLoginFailed 當遠端登入無法連接到 SQL Server 的執行個體時,就會發生這個事件。 (繼承自 ConnectionManager。)
公用事件 ServerMessage SQL Server 在目前的連接上傳回警告或資訊訊息時所發生的事件。 (繼承自 ConnectionManager。)
公用事件 StateChange SQL Server 的執行個體變更狀態時所發生的事件。  (繼承自 ConnectionManager。)
公用事件 StatementExecuted SQL Server 的執行個體在目前的連接上執行陳述式時所發生的事件。  (繼承自 ConnectionManager。)

回到頁首

明確 繼承 實作

  名稱 說明
明確介面實作私用方法 ISfcConnection.Connect 傳回值,這個值表示使用 SFC 連接來連接的伺服器。
明確介面實作私用方法 ISfcConnection.Copy 使用 SFC 連接來複製伺服器連接。
明確介面實作私用方法 ISfcConnection.Disconnect 中斷連接。
明確介面實作私用屬性 ISfcConnection.ServerVersion 取得伺服器的版本。
明確介面實作私用方法 ISfcConnection.ToEnumeratorObject 轉譯伺服器連接的列舉值物件。

回到頁首

備註

ServerConnection 類別是由 Server 類別為服務,並複製類別 (例如 ReplicationServer 分類。

一般而言,,因為它會自動建立與伺服器物件,您就必須宣告 ServerConnection 物件的執行個體。 如果您想要儲存重複使用的連接設定,請 ServerConnection 宣告物件變數。 伺服器物件上的屬性設定為 ConnectionContext ServerConnection 物件的。 ServerConnection 物件包含與伺服器物件 SQL Server和執行個體之間的連接相關聯的屬性。 您可以使用它來檢視或修改連接的某些層面,例如處理序識別碼,管理交易,驗證模式,執行模式和直接即使執行 Transact-SQL 命令。

使用 ServerConnection 物件,您可以執行下列動作:

  • 執行 Transact-SQL 陳述式直接在 SQL Server對執行個體的連接。

  • 變更連接設定,例如驗證方法。

  • 開始進行,而且會回復交易。

  • 擷取 SMO 應用程式所產生的 Transact-SQL 陳述式。

範例

如需有關這個方法的另一個執行個體與 Database 物件,請參閱 [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

執行緒安全性

這個型別的任何公用 static (在 Visual Basic 中為 Shared) 成員都是執行緒安全的。並不是所有的執行個體成員都保證可以用於所有的執行緒。

請參閱

參考

Microsoft.SqlServer.Management.Common 命名空間

其他資源

連接到 SQL Server 的執行個體

連接到 SQL Server 的執行個體