PARTNER

Senin, 20 September 2010

How to Migrate/Downsize a SQL Server database to SQL Server Compact 4.0 (and 3.5)

Release 3.0.0.6 of my SQL Compact schema and data script utility on Codeplex contains a command line utility, named Export2SqlCe, that allows you to export schema and data from a SQL Server 2005/2008 database in a SQL Compact compatible SQL format.
The Export2SqlCe tool does not create a SQL Compact sdf database file, but just creates a T-SQL script , that you can run with a tool like my SqlCeCmd Codeplex utility or SQL Server Management Studio 2008. This approach gives you the flexibility to modify the script in a text editor before creating the sdf file – but requires an extra step.
Below are the steps required to migrate a SQL Server database (tables and table indexes/constraints only) to a SQL Server Compact database.
First, run Export2SqlCe against your SQL Server database (2005 and 2008 are supported), using a command line similar to:
Export2sqlce "Data Source=(local);Initial Catalog=AdventureworksLT;Integrated Security=True" AW.sqlce
image
This will create a file named C:\aw.sqlce – let’s have a look:
image
This file contains a script to create tables, data and constraints (indexes and foreign keys), all in SQL Server Compact 3.5/4.0 compatible T-SQL script.
Then you can either open the aw.sqlce script in SQL Server Management Studio 2008 or use sqlcecmd to create the sdf file and populate the file based on the script – meaning the whole process can be run from a batch file and completely automated!
(To manage SQL Server 4.0 databases, use SqlCeCmd40.exe, for 3.5 use SqlCeCmd.exe)
First create the database:
sqlcecmd40 -d "Data Source=C:\aw.sdf" -e create
Then run the generated script against the database:
sqlcecmd40 -d "Data Source=C:\aw.sdf" -i aw.sqlce > log.txt
Examine the log.txt to ensure no errors occurred - search for “error code” – please let me know if you encounter any errors, so they can possibly be fixed in an update to the utility.
Entire batch file:
Export2sqlce "Data Source=(local);Initial Catalog=AdventureworksLT;Integrated Security=True" AW.sqlce
sqlcecmd40 -d "Data Source=C:\aw.sdf" -e create
sqlcecmd40 -d "Data Source=C:\aw.sdf" -i aw.sqlce > log.txt
 
Source : http://erikej.blogspot.com 
Share:

0 komentar:

Posting Komentar

Blog Archive

Blogger templates