public void ExportServerDatabaseTo40(object sender, ExecutedRoutedEventArgs e) { var menuItem = sender as MenuItem; if (menuItem == null) { return; } var databaseInfo = menuItem.CommandParameter as DatabasesMenuCommandParameters; if (databaseInfo == null) { return; } var treeViewItem = databaseInfo.DatabasesTreeViewItem; try { DataSource sqlDataSource = new DataSource("MicrosoftSqlServer", "Microsoft SQL Server"); sqlDataSource.Providers.Add(DataProvider.SqlDataProvider); DataConnectionDialog dcd = new DataConnectionDialog(); dcd.DataSources.Add(sqlDataSource); dcd.SelectedDataProvider = DataProvider.SqlDataProvider; dcd.SelectedDataSource = sqlDataSource; if (DataConnectionDialog.Show(dcd) == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(dcd.ConnectionString)) { PickTablesDialog ptd = new PickTablesDialog(); int totalCount = 0; using (IRepository repository = RepoHelper.CreateServerRepository(dcd.ConnectionString)) { ptd.Tables = repository.GetAllTableNamesForExclusion(); totalCount = ptd.Tables.Count; } ptd.Owner = Application.Current.MainWindow; bool?res = ptd.ShowDialog(); if (res.HasValue && res.Value == true && (ptd.Tables.Count < totalCount)) { string sdfName; SaveFileDialog fd = new SaveFileDialog(); fd.Title = "Export as"; fd.Filter = "SQL Server Compact Database (*.sdf)|*.sdf|All Files(*.*)|*.*"; fd.OverwritePrompt = true; fd.ValidateNames = true; bool?result = fd.ShowDialog(); if (result.HasValue && result.Value == true) { sdfName = fd.FileName; using (IRepository repository = RepoHelper.CreateServerRepository(dcd.ConnectionString)) { try { string scriptRoot = System.IO.Path.GetTempFileName(); string tempScript = scriptRoot + ".sqlce"; var generator = RepoHelper.CreateGenerator(repository, tempScript); generator.ExcludeTables(ptd.Tables); SetStatus("Scripting server database..."); generator.ScriptDatabaseToFile(Scope.SchemaData); SetStatus("Creating SQL Server Compact database..."); ISqlCeHelper helper = RepoHelper.CreateHelper(); string sdfConnectionString = string.Format("Data Source={0};Max Database Size=4091", sdfName); if (System.IO.File.Exists(sdfName)) { File.Delete(sdfName); } helper.CreateDatabase(sdfConnectionString); BackgroundWorker bw = new BackgroundWorker(); List <string> parameters = new List <string>(); parameters.Add(sdfConnectionString); parameters.Add(tempScript); parameters.Add(scriptRoot); bw.DoWork += new DoWorkEventHandler(bw_DoWork); bw.RunWorkerCompleted += (s, ea) => { try { if (ea.Error != null) { MessageBox.Show(Helpers.DataConnectionHelper.ShowErrors(ea.Error)); } else { MessageBox.Show("Database successfully exported"); } } finally { bw.Dispose(); } }; bw.RunWorkerAsync(parameters); } catch (Exception ex) { MessageBox.Show(Helpers.DataConnectionHelper.ShowErrors(ex)); } } } } dcd.Dispose(); } } catch (Exception ex) { MessageBox.Show(Helpers.DataConnectionHelper.ShowErrors(ex)); } }