SqlCeConnection.ChangeDatabase Method

Changes the current database for an open SqlCeConnection.

Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (in system.data.sqlserverce.dll)

Syntax

'Declaration
Public Overrides Sub ChangeDatabase ( _
    value As String _
)
public override void ChangeDatabase (
    string value
)
public:
virtual void ChangeDatabase (
    String^ value
) override
public void ChangeDatabase (
    String value
)
public override function ChangeDatabase (
    value : String
)

Parameters

  • value
    The database name.

Return Value

True if the database was changed successfully; otherwise, false.

Exceptions

Exception type Condition
ArgumentException

The database name is not valid.

InvalidOperationException

The connection is not open.

SqlCeException

The databases cannot be changed.

Remarks

The name supplied in the value parameter must be a valid database name. The name must be the full path to the database; for example, "\my app\myDB.sdf". The value parameter cannot be null, be empty, or contain a string with only blank characters.

ChangeDatabase applies to the new database the same properties used to open the current database, except for the password, which will be set to an empty string.

If the new database requires a password, this call will fail. If this occurs, to change databases, you must close the original connection, specify a new connection string, and then reopen the connection.

Example

The following example shows how to switch databases while preserving the original connection options.

Dim conn As New SqlCeConnection()

' Set some connection string properties e.g.:
' 
conn.ConnectionString = _
    "Persist Security Info = False; Data Source = 'SalesData.sdf';" & _
    "Password = '<password>'; File Mode = 'shared read'; " & _
    "Max Database Size = 256; Max Buffer Size = 1024"

conn.Open()

' You can change the database while preserving 
' the orignal connection options
'
conn.ChangeDatabase("SupportData.sdf")
SqlCeConnection conn = new SqlCeConnection();

// Set some connection string properties e.g.:
// 
conn.ConnectionString = 
    "Persist Security Info = False; Data Source = 'SalesData.sdf';" +
    "Password = '<password>'; File Mode = 'shared read'; " +
    "Max Database Size = 256; Max Buffer Size = 1024";

conn.Open();

// You can change the database while preserving 
// the orignal connection options
//
conn.ChangeDatabase("SupportData.sdf");

Thread Safety

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

Platforms

Development Platforms

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
Version Information
.NET Framework and NET Compact Framework
Supported in 3.5
.NET Framework
Supported in 3.0
.NET Compact Framework and .Net Framework
Supported in 2.0

See Also

Reference

SqlCeConnection Class
SqlCeConnection Members
System.Data.SqlServerCe Namespace