OleDbConnection.ChangeDatabase(String) 方法

定义

更改已打开的 OleDbConnection 的当前数据库。Changes the current database for an open OleDbConnection.

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

参数

value
String

数据库名称。The database name.

实现

异常

数据库名称无效。The database name is not valid.

连接未打开。The connection is not open.

不能更改数据库。Cannot change the database.

示例

下面的示例创建一个 OleDbConnection 并显示其某些只读属性。The following example creates an OleDbConnection and displays some of its read-only properties.

static void ChangeDatabaseConnection(string connectionString)
{
   using (OleDbConnection connection = new OleDbConnection(connectionString))
   {
      try
      {
         connection.Open();
         Console.WriteLine("ServerVersion: {0} \nDatabase: {1}",
             connection.ServerVersion, connection.Database);
         
         connection.ChangeDatabase("Northwind");
         Console.WriteLine("ServerVersion: {0} \nDatabase: {1}",
            connection.ServerVersion, connection.Database);
      }
      catch (Exception ex)
      {
         Console.WriteLine(ex.Message);
      }
      // The connection is automatically closed when the
      // code exits the using block.
   }
}
Public Sub ChangeDatabaseConnection(ByVal connectionString As String)

   Using connection As New OleDbConnection(connectionString)
      Try
         connection.Open()
         Console.WriteLine("Server Version: {0} Database: {1}", _
             connection.ServerVersion, connection.Database)
         connection.ChangeDatabase("Northwind")
         Console.WriteLine("Server Version: {0} Database: {1}", _
            connection.ServerVersion, connection.Database)

      Catch ex As Exception
         Console.WriteLine(ex.Message)
      End Try
      ' The connection is automatically closed when the
      ' code exits the Using block.
   End Using
End Sub

注解

database 参数中提供的值必须是有效的数据库名称。The value supplied in the database parameter must be a valid database name. database 参数不能包含 null 值、空字符串或只包含空白字符的字符串。The database parameter cannot contain a null value, an empty string, or a string with only blank characters.

适用于

另请参阅