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 值、空字符串 (“”) 或仅包含空字符的字符串。

适用于

另请参阅