Monday, September 23, 2013

SQL Server Compact Code Snippet #20 : change database password

The SqlCeEngine class (in the System.Data.SqlServerCe ADO.NET provider) includes a number of useful methods for handling global database chores, like verifying the integrity of the database, and shrink the database file. It also includes the Compact method, which creates a new database file (optionally in-place) of an existing database file. During this process, you can specify a number of database creation parameters, namely the collation order, the case sensitivity or the password of the database. In code:

    using (SqlCeEngine engine = 
new SqlCeEngine(@"Data Source=C:\temp\mysecretdb.sdf;Password=oldpassword"))
{
engine.Compact("Data Source=;Password=newpassword");
}


Notice the special data source key used, specifying that the new database should be created in-place.

No comments: