共用方式為


OdbcConnection.ChangeDatabase(String) 方法

定義

變更與開啟之 OdbcConnection 相關聯的目前資料庫。

public:
 override void ChangeDatabase(System::String ^ value);
public:
 virtual void ChangeDatabase(System::String ^ value);
public override void ChangeDatabase (string value);
public void ChangeDatabase (string value);
override this.ChangeDatabase : string -> unit
abstract member ChangeDatabase : string -> unit
override this.ChangeDatabase : string -> unit
Public Overrides Sub ChangeDatabase (value As String)
Public Sub ChangeDatabase (value As String)

參數

value
String

資料庫名稱。

實作

例外狀況

資料庫名稱無效。

連接未開啟。

無法變更資料庫。

範例

下列範例會 OdbcConnection 建立 並變更目前的資料庫。

private static void CreateOdbcConnection()
{
    string connectionString = "Driver={SQL Native Client};Server=(local);Trusted_Connection=Yes;Database=AdventureWorks;";

    using (OdbcConnection connection = new OdbcConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: " + connection.ServerVersion
            + "\nDatabase: " + connection.Database);
        connection.ChangeDatabase("master");
        Console.WriteLine("ServerVersion: " + connection.ServerVersion
            + "\nDatabase: " + connection.Database);
        Console.ReadLine();
    }
}
Private Sub CreateOdbcConnection()

    Dim connectionString As String = _
       "Driver={SQL Native Client};Server=(local);Trusted_Connection=Yes;Database=AdventureWorks;"

    Using connection As New OdbcConnection(connectionString)
        With connection
            .Open()
            Console.WriteLine("ServerVersion: " & .ServerVersion _
               & vbCrLf + "Database: " & .Database)
            .ChangeDatabase("master")
            Console.WriteLine("ServerVersion: " & .ServerVersion _
               & vbCrLf + "Database: " & .Database)
            Console.ReadLine()
        End With
    End Using
End Sub

備註

參數 value 必須包含有效的資料庫名稱,而且不能包含 null 值、空字串 (“”) 或只有空白字元的字串。

適用於

另請參閱