Click here to Skip to main content
Licence BSD
First Posted 29 Apr 2008
Views 115,596
Downloads 3,356
Bookmarked 103 times

SQL Server to SQL Server Compact Edition Database Copy Utility

By johnnycantcode | 26 Jun 2008
Create and manage your mobile database using SQL Management Studio and export them to SQL Compact Edition databases
 
Part of The SQL Zone sponsored by
See Also

1
1 vote, 3.6%
2
1 vote, 3.6%
3
1 vote, 3.6%
4
25 votes, 89.3%
5
4.98/5 - 28 votes
2 removed
μ 4.83, σa 1.22 [?]

UPDATE

Please download the latest version of this tool from JohnnyCantCode.com. I have updated this utility based on some feedback from users.

I added the ability to select the schema you wish to copy. For most databases, this will simply be "dbo".

I fixed a bug where the application did not recognize a valid version of "System.Data.SqlServerCe.dll".

Introduction

This utility will copy the schema and data from a normal SQL Server 2000/2005 database and export it to a SQL Server Compact Edition database. The tool supports version 3.1 or 3.5 of SQL Server Compact Edition.

Background

I was working on a mobile application and needed an easy way to manage the table relationships and indexes without having to resort to scripting. I was familiar with using SQL Server Management Studio so I decided that I would write a utility that converted a normal SQL Server database to a mobile database. This way, I could continue using the tool I normally use to create and maintain my databases.

Using the Code

Feel free to take a look at the code and offer your most gracious comments. The application uses a wizard to walk you through converting a database. I have also included a normal WINFORM that also does the conversion, but I stopped development on this in lieu of the wizard, so the form is incomplete. I will be maintaining this code and writing more about it on my blog at www.JohnnyCantCode.com. You can find the original post here.

Points of Interest

This utility will copy Indexes, Primary Keys, Foreign Keys, Table structure and data. SQL Server Compact Edition does not have support for Views, Triggers nor Stored Procedures, therefore this utility does not copy these.

History

  • 1.0 Initial release

License

This article, along with any associated source code and files, is licensed under The BSD License

About the Author

johnnycantcode

Software Developer (Senior)
Gologic Tech LLC.
United States United States

Member
I work as an independent software architect and senior developer. I have worked on many large enterprise projects as well as small single user applications.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberfootnote9:28 9 Sep '11  
QuestionIs there anyone able to make it work with SQL Server 2008 R2? PinmemberEhsan.thabit9:14 21 Jun '11  
GeneralSupport for Compact edition v3.5 sp2 Pinmemberjcgk_773:32 13 Apr '11  
QuestionJohnny Can't Code? PinmemberMember 78058096:43 4 Apr '11  
Generalgetting error just-in-time (JIT) debugging instead of this dialog box Pinmemberpsnlakshmi2:40 30 Oct '10  
GeneralBad Image Format Exception Pinmemberi_lonsdale@hotmail.com23:43 15 Oct '10  
Generalproblem with varbinary Pinmemberpgarbo6:05 17 Aug '10  
GeneralThanks PinmemberUros Calakovic3:00 8 Aug '10  
QuestionException where Copying Database PinmemberEdgarVegaD5:36 14 Jan '10  
AnswerRe: Exception where Copying Database PinmemberEdgarVegaD9:14 14 Jan '10  
AnswerRe: Exception where Copying Database Pinmemberdille44:34 21 May '10  
GeneralGETUTCDATE() & Multi-Column Key's Pinmembermichael@merlot.com5:41 21 Nov '09  
GeneralFor SQL Server CE 3.5 SP1 Pinmemberkshakir11:08 23 Jul '09  
QuestionSql 2008 support? Pinmemberkhanricksteele8:26 29 Apr '09  
QuestionUnknown datatype PinmemberMember 18712530:38 19 Mar '09  
AnswerRe: Unknown datatype PinmemberMember 26515737:32 16 Jun '09  
GeneralPrimery Keys are not correct after import to SQL Compact Pinmembervoellinger1:51 26 Jan '09  
GeneralOut of memory error Pinmembercess10:36 30 Nov '08  
Generalthere is problem with NVARCHAR(MAX) PinmemberTajniak56:51 20 Nov '08  
Hi
 
this tool is really great, i just found out it doesn't work with NVARCHAR(MAX) columns. To make it works, you need to add one more case in WizardForm.cs which can look like this (works for me):
 
                    foreach (Column col in tbl.Columns)
                    {
                        if (colIdx > 0)
                            sb.Append(", ");
 
                        sb.Append("[").Append(col.Name).Append("]").Append(" ");
                        int max = 0;
                        switch (col.DataType.SqlDataType)
                        {
                            case SqlDataType.NVarCharMax:
                                max = 4000;
                                col.DataType = new DataType(SqlDataType.NVarChar);
                                col.DataType.MaximumLength = max;
                                break;
 
I know that in SQL Server Compact 3.5 the maximum value of NVARCHAR data type is 4000, so I set it to that value, I'm not sure about other versions.
 
Anyway, thanks again for this great tool!
Generaliv been messing with this for a while until i found this.TNX!!!!! Pinmemberzivzuv21:37 18 Nov '08  
Generalprimary key contraint names Pinmembermape10826:04 19 Aug '08  
GeneralRe: primary key contraint names PinmemberFrancois YACOB7:38 16 Jun '09  
GeneralThis thing rocks! Pinmembersholliday11:32 5 Aug '08  
GeneralGreat work!!! only one thing and an Idea for future work Pinmembermape10827:19 23 Jul '08  
GeneralRe: Great work!!! only one thing and an Idea for future work PinmemberFrancois YACOB7:43 16 Jun '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 26 Jun 2008
Article Copyright 2008 by johnnycantcode
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid