Click here to Skip to main content
Licence BSD
First Posted 29 Apr 2008
Views 125,991
Downloads 4,672
Bookmarked 106 times

SQL Server to SQL Server Compact Edition Database Copy Utility

By | 26 Jun 2008 | Article
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

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
Generalprimary key contraint names Pinmembermape10825:04 19 Aug '08  
GeneralRe: primary key contraint names PinmemberFrancois YACOB6:38 16 Jun '09  
GeneralThis thing rocks! Pinmembersholliday10:32 5 Aug '08  
GeneralGreat work!!! only one thing and an Idea for future work Pinmembermape10826:19 23 Jul '08  
GeneralRe: Great work!!! only one thing and an Idea for future work PinmemberFrancois YACOB6:43 16 Jun '09  
GeneralType Conversion errors Pinmembergratajik13:25 15 Jul '08  
GeneralON DELETE ... ON UPDATE ... PinmemberHoang Cuong22:21 12 Jul '08  
Your tool is very useful, it help me much.
 
I have a note that it lacks ON DELETE, ON UPDATE actions when creating FK constraints. I made a slight change in your code and it works fine for me.
 
void DoCopy()
{
    ...
    							
    string fkSql = "ALTER TABLE [{0}] ADD CONSTRAINT [{1}] FOREIGN KEY([{2}]) REFERENCES [{3}] ([{4}]) ON DELETE {5} ON UPDATE {6} ";
    ...
    string createFKSql = String.Format(fkSql, 
	tbl.Name, 
	fk.Name, 
	"{0}", 
	fk.ReferencedTable, 
	sourceDb.Tables[fk.ReferencedTable].Indexes[fk.ReferencedKey].IndexedColumns[0].Name,
	fk.DeleteAction.ToSql(), fk.UpdateAction.ToSql());
 
    ...
}
 
internal static class ForeignKeyActionExtensions
{
	public static string ToSql(this ForeignKeyAction action)
	{
		switch (action)
		{
			case ForeignKeyAction.Cascade: return "CASCADE";
			case ForeignKeyAction.NoAction: return "NO ACTION";
			case ForeignKeyAction.SetDefault: return "SET DEFAULT";
			case ForeignKeyAction.SetNull: return "SET NULL";
		}
		throw new InvalidEnumArgumentException("action", (int)action, typeof(ForeignKeyAction));
	}
}

Generaldata transfer PinmemberShaikh Sahrif19:18 28 Jun '08  
GeneralBrilliant PinmemberJohnny J.20:44 26 Jun '08  
GeneralError after conversion.. Opening converted file with SQL Management Studio PinmembermGxAlander17:46 5 Jun '08  
GeneralRe: Error after conversion.. Opening converted file with SQL Management Studio Pinmemberjohnnycantcode9:34 12 Jun '08  
GeneralNot working with SQL2000 Pinmemberoldieman22:28 2 Jun '08  
GeneralVisual Studio Pinmembergnassar2:26 2 Jun '08  
GeneralRe: Visual Studio Pinmemberjohnnycantcode9:35 12 Jun '08  
GeneralSchema Setting + SqlCe Version PinmemberCharl Victor23:27 12 May '08  
GeneralRe: Schema Setting + SqlCe Version Pinmemberjohnnycantcode3:22 13 May '08  
AnswerRe: Schema Setting + SqlCe Version PinmemberCharl Victor5:17 14 May '08  
GeneralTriggers and SP PinmemberMember 40695463:13 7 May '08  
GeneralRe: Triggers and SP Pinmemberjohnnycantcode2:31 8 May '08  
GeneralRe: Triggers and SP PinmemberMember 406954610:24 8 May '08  
GeneralRe: Triggers and SP PinmemberCharles K. Kincaid7:58 23 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.120529.1 | Last Updated 26 Jun 2008
Article Copyright 2008 by johnnycantcode
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid