Click here to Skip to main content
15,884,836 members
Articles / Desktop Programming / Windows Forms

SQL Server to SQL Server Compact Edition Database Copy Utility

Rate me:
Please Sign up or sign in to vote.
4.95/5 (39 votes)
26 Jun 2008BSD2 min read 346.9K   16.8K   123   59
Create and manage your mobile database using SQL Management Studio and export them to SQL Compact Edition databases
Image 1

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


Written By
Software Developer (Senior) Gologic Tech LLC.
United States United States
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.

Comments and Discussions

 
QuestionIndex was outside the bounds of the array. Pin
jame0130-Jun-15 22:46
jame0130-Jun-15 22:46 
QuestionImport or Convert a mdf file to sdf Pin
HassanFad29-Jan-15 0:30
HassanFad29-Jan-15 0:30 
Question.Exe Pin
Member 1028672113-May-14 2:09
Member 1028672113-May-14 2:09 
SuggestionNVarCharMAX support Pin
Khaari5-Nov-13 23:23
professionalKhaari5-Nov-13 23:23 
Questionprovide .exe file for this project also Pin
Member 942577126-Jul-13 8:11
Member 942577126-Jul-13 8:11 
GeneralGreat tool - after some manual changes :-) Pin
dolbysys18-Feb-13 21:16
dolbysys18-Feb-13 21:16 
GeneralRe: Great tool - after some manual changes :-) Pin
pmiossec27-Feb-13 12:45
pmiossec27-Feb-13 12:45 
GeneralRe: Great tool - after some manual changes :-) Pin
ronan_mc25-Apr-13 13:07
ronan_mc25-Apr-13 13:07 
GeneralRe: Great tool - after some manual changes :-) Pin
pmiossec28-Apr-13 1:43
pmiossec28-Apr-13 1:43 
Questiongeht nicht! Pin
Member 250914025-Jan-13 4:56
Member 250914025-Jan-13 4:56 
AnswerRe: geht nicht! Pin
dolbysys18-Feb-13 21:20
dolbysys18-Feb-13 21:20 
QuestionMdf----_> sdf and sdf ----> mdf Pin
cjgallardo9629-Oct-12 3:03
cjgallardo9629-Oct-12 3:03 
Questionasm.GetType("System.Data.SqlServerCe.SqlCeEngine"); returns null Pin
bradut15-Oct-12 11:25
professionalbradut15-Oct-12 11:25 
AnswerRe: asm.GetType("System.Data.SqlServerCe.SqlCeEngine"); returns null Pin
Jeff Wharton2-Jan-13 18:01
Jeff Wharton2-Jan-13 18:01 
QuestionMy vote of 5 Pin
SPI24-Aug-12 7:20
SPI24-Aug-12 7:20 
GeneralMy vote of 5 Pin
eyedia27-Apr-12 9:40
eyedia27-Apr-12 9:40 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey17-Apr-12 2:21
professionalManoj Kumar Choubey17-Apr-12 2:21 
GeneralMy vote of 5 Pin
footnote9-Sep-11 8:28
footnote9-Sep-11 8:28 
QuestionIs there anyone able to make it work with SQL Server 2008 R2? Pin
Ehsan.thabit21-Jun-11 8:14
Ehsan.thabit21-Jun-11 8:14 
GeneralSupport for Compact edition v3.5 sp2 Pin
jcgk_7713-Apr-11 2:32
jcgk_7713-Apr-11 2:32 
GeneralRe: Support for Compact edition v3.5 sp2 Pin
Member 849633121-Mar-12 9:40
Member 849633121-Mar-12 9:40 
QuestionJohnny Can't Code? Pin
Member 78058094-Apr-11 5:43
Member 78058094-Apr-11 5:43 
Generalgetting error just-in-time (JIT) debugging instead of this dialog box Pin
psnlakshmi30-Oct-10 1:40
psnlakshmi30-Oct-10 1:40 
HI
I´m trying to copy a SQL Server 2005 DB to a SQL Compact DB using the this tool, everything goes fine but when I do the last step I get this error
See the end of this message for details 

on invoking 
just-in-time (JIT) debugging instead of 

this dialog box.

************** Exception Text 

**************
System.Reflection.TargetInvocationExcept

ion: Exception has been thrown by the 
target of an invocation. ---> 

System.ArgumentException: Invalid value 
for key 'locale identifier'.  ---> 

System.FormatException: Input string was 
not in a correct format.
   at 

System.Number.StringToNumber(String str, 

NumberStyles options, NumberBuffer& 

number, NumberFormatInfo info, Boolean 

parseDecimal)
   at System.Number.ParseInt32(String s, 

NumberStyles style, NumberFormatInfo 
info)
   at 

System.Data.SqlServerCe.ConStringUtil.Se

tIntType(String value, String key)
   --- End of inner exception stack 

trace ---
   at 
System.Data.SqlServerCe.ConStringUtil.Se

tIntType(String value, String key)
   at 

System.Data.SqlServerCe.ConStringUtil.Se
tLocale(Hashtable table)
   at 

System.Data.SqlServerCe.ConStringUtil.Ve
rifyValues(Hashtable values)
   at 

System.Data.SqlServerCe.ConStringUtil.Pa
rseConnectionString(String& 

connectionString)
   at 
System.Data.SqlServerCe.SqlCeEngine.set_

LocalConnectionString(String value)
   at 

System.Data.SqlServerCe.SqlCeEngine..cto

r(String connectionString)
   --- End of inner exception stack 

trace ---
   at 
System.RuntimeMethodHandle._InvokeConstr

uctor(Object[] args, SignatureStruct& 

signature, IntPtr declaringType)
   at 

System.RuntimeMethodHandle.InvokeConstru
ctor(Object[] args, SignatureStruct 

signature, RuntimeTypeHandle 
declaringType)
   at 

System.Reflection.RuntimeConstructorInfo

.Invoke(BindingFlags invokeAttr, Binder 

binder, Object[] parameters, CultureInfo 
culture)
   at 

System.RuntimeType.CreateInstanceImpl(Bi

ndingFlags bindingAttr, Binder binder, 
Object[] args, CultureInfo culture, 

Object[] activationAttributes)
   at 
System.Activator.CreateInstance(Type 

type, BindingFlags bindingAttr, Binder 
binder, Object[] args, CultureInfo 

culture, Object[] activationAttributes)
   at GLT.SqlCopy.WizardForm.DoCopy()
   at 

GLT.SqlCopy.WizardForm.UpdateWizard()
   at 

GLT.SqlCopy.WizardForm.btnNext_Click(Obj
ect sender, EventArgs e)
   at 

System.Windows.Forms.Control.OnClick(Eve
ntArgs e)
   at 

System.Windows.Forms.Button.OnClick(Even
tArgs e)
   at 

System.Windows.Forms.Button.OnMouseUp(Mo
useEventArgs mevent)
   at 

System.Windows.Forms.Control.WmMouseUp(M

essage& m, MouseButtons button, Int32 
clicks)
   at 

System.Windows.Forms.Control.WndProc(Mes
sage& m)
   at 

System.Windows.Forms.ButtonBase.WndProc(
Message& m)
   at 

System.Windows.Forms.Button.WndProc(Mess
age& m)
   at 

System.Windows.Forms.Control.ControlNati

veWindow.OnMessage(Message& m)
   at 

System.Windows.Forms.Control.ControlNati

veWindow.WndProc(Message& m)
   at 

System.Windows.Forms.NativeWindow.Callba
ck(IntPtr hWnd, Int32 msg, IntPtr 

wparam, IntPtr lparam)


************** Loaded Assemblies 

**************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 

(REDBITS.050727-1400)
    CodeBase: 
file:///C:/WINDOWS/Microsoft.NET/Framewo

rk/v2.0.50727/mscorlib.dll
----------------------------------------
GLT.SqlCopy
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: 

file:///C:/Documents%20and%20Settings/la

xmi/Desktop/SQL2005%20express%20ToSQLCom

pact%20Convert/GLT.SqlCopy.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 

(REDBITS.050727-1400)
    CodeBase: 
file:///C:/WINDOWS/assembly/GAC_MSIL/Sys

tem.Windows.Forms/2.0.0.0__b77a5c561934e

089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 

(REDBITS.050727-1400)
    CodeBase: 
file:///C:/WINDOWS/assembly/GAC_MSIL/Sys

tem/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 

(REDBITS.050727-1400)
    CodeBase: 
file:///C:/WINDOWS/assembly/GAC_MSIL/Sys

tem.Drawing/2.0.0.0__b03f5f7f11d50a3a/Sy

stem.Drawing.dll
----------------------------------------
Accessibility
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 

(REDBITS.050727-1400)
    CodeBase: 
file:///C:/WINDOWS/assembly/GAC_MSIL/Acc

essibility/2.0.0.0__b03f5f7f11d50a3a/Acc

essibility.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 

(REDBITS.050727-1400)
    CodeBase: 
file:///C:/WINDOWS/assembly/GAC_MSIL/Sys

tem.Configuration/2.0.0.0__b03f5f7f11d50

a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 

(REDBITS.050727-1400)
    CodeBase: 
file:///C:/WINDOWS/assembly/GAC_MSIL/Sys

tem.Xml/2.0.0.0__b77a5c561934e089/System

.Xml.dll
----------------------------------------
Microsoft.SqlServer.Smo
    Assembly Version: 9.0.242.0
    Win32 Version: 9.00.4035.00
    CodeBase: 

file:///C:/WINDOWS/assembly/GAC_MSIL/Mic

rosoft.SqlServer.Smo/9.0.242.0__89845dcd

8080cc91/Microsoft.SqlServer.Smo.dll
----------------------------------------
Microsoft.SqlServer.ConnectionInfo
    Assembly Version: 9.0.242.0
    Win32 Version: 9.00.4035.00
    CodeBase: 

file:///C:/WINDOWS/assembly/GAC_MSIL/Mic

rosoft.SqlServer.ConnectionInfo/9.0.242.

0__89845dcd8080cc91/Microsoft.SqlServer.

ConnectionInfo.dll
----------------------------------------
System.Data
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 

(REDBITS.050727-1400)
    CodeBase: 
file:///C:/WINDOWS/assembly/GAC_32/Syste

m.Data/2.0.0.0__b77a5c561934e089/System.

Data.dll
----------------------------------------
Microsoft.SqlServer.SmoEnum
    Assembly Version: 9.0.242.0
    Win32 Version: 9.00.4035.00
    CodeBase: 

file:///C:/WINDOWS/assembly/GAC_MSIL/Mic

rosoft.SqlServer.SmoEnum/9.0.242.0__8984

5dcd8080cc91/Microsoft.SqlServer.SmoEnum

.dll
----------------------------------------
System.Transactions
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 

(REDBITS.050727-1400)
    CodeBase: 
file:///C:/WINDOWS/assembly/GAC_32/Syste

m.Transactions/2.0.0.0__b77a5c561934e089

/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 

(REDBITS.050727-1400)
    CodeBase: 
file:///C:/WINDOWS/assembly/GAC_32/Syste

m.EnterpriseServices/2.0.0.0__b03f5f7f11

d50a3a/System.EnterpriseServices.dll
----------------------------------------
Microsoft.SqlServer.SqlEnum
    Assembly Version: 9.0.242.0
    Win32 Version: 9.00.4035.00
    CodeBase: 

file:///C:/WINDOWS/assembly/GAC_MSIL/Mic

rosoft.SqlServer.SqlEnum/9.0.242.0__8984

5dcd8080cc91/Microsoft.SqlServer.SqlEnum

.dll
----------------------------------------
Microsoft.SqlServer.BatchParser
    Assembly Version: 9.0.242.0
    Win32 Version: 2005.090.4035.00
    CodeBase: 

file:///C:/WINDOWS/assembly/GAC_32/Micro

soft.SqlServer.BatchParser/9.0.242.0__89

845dcd8080cc91/Microsoft.SqlServer.Batch

Parser.dll
----------------------------------------
System.Data.SqlServerCe
    Assembly Version: 9.0.242.0
    Win32 Version: 3.0.5300.0
    CodeBase: 

file:///C:/WINDOWS/assembly/GAC_MSIL/Sys

tem.Data.SqlServerCe/9.0.242.0__89845dcd

8080cc91/System.Data.SqlServerCe.dll
----------------------------------------

************** JIT Debugging 

**************
To enable just-in-time (JIT) debugging, 

the .config file for this
application or computer (machine.config) 

must have the
jitDebugging value set in the 

system.windows.forms section.
The application must also be compiled 

with debugging
enabled.

For example:

<configuration>
    <system.windows.forms 

jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any 

unhandled exception
will be sent to the JIT debugger 

registered on the computer
rather than be handled by this dialog 
box.


Help me any body?

Note: i downlod the SQLToSQLCompactCopy.zip from
http://www.johnnycantcode.com/page/SQL-Server-to-SQL-Server-Compact-Edition-Database-Copy.aspx[^]
GeneralBad Image Format Exception Pin
i_lonsdale@hotmail.com15-Oct-10 22:43
i_lonsdale@hotmail.com15-Oct-10 22:43 
Generalproblem with varbinary Pin
pgarbo17-Aug-10 5:05
pgarbo17-Aug-10 5:05 

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

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