private void ExportServerDatabaseToEmbedded(DatabaseType databaseType, DatabaseMenuCommandParameters parameters)
        {
            string filter = DataConnectionHelper.GetSqlCeFileFilter();
            Scope scope = Scope.SchemaData;
            if (databaseType == DatabaseType.SQLite)
            {
                filter = DataConnectionHelper.GetSqliteFileFilter();
                scope = Scope.SchemaDataSQLite;
            }
            Debug.Assert(databaseType == DatabaseType.SQLite || databaseType == DatabaseType.SQLCE40, "Unexpected database type");
            try
            {
                string connectionString = parameters.DatabaseInfo != null
                    ? parameters.DatabaseInfo.ConnectionString :
                    DataConnectionHelper.PromptForConnectionString(package);
                if (!string.IsNullOrEmpty(connectionString))
                {
                    PickTablesDialog ptd = new PickTablesDialog();
                    int totalCount = 0;
                    using (IRepository repository = Helpers.DataConnectionHelper.CreateRepository(new DatabaseInfo
                        { ConnectionString = connectionString, DatabaseType = DatabaseType.SQLServer }))
                    {
                        ptd.Tables = repository.GetAllTableNamesForExclusion();
                        totalCount = ptd.Tables.Count;
                    }

                    bool? res = ptd.ShowModal();
                    if (res.HasValue && res.Value == true && (ptd.Tables.Count < totalCount))
                    {
                        string dbName;
                        string dbConnectionString = null;
                        SaveFileDialog fd = new SaveFileDialog();
                        fd.Title = "Export as";
                        fd.Filter = filter;
                        fd.OverwritePrompt = true;
                        fd.ValidateNames = true;
                        bool? result = fd.ShowDialog();
                        if (result.HasValue && result.Value == true)
                        {
                            dbName = fd.FileName;
                            try
                            {
                                if (databaseType == DatabaseType.SQLCE40)
                                {
                                    package.SetStatus("Creating SQL Server Compact database...");
                                    SqlCeScripting.SqlCeHelper4 helper = new SqlCeScripting.SqlCeHelper4();
                                    dbConnectionString = string.Format("Data Source={0};Max Database Size=4091", dbName);
                                    if (System.IO.File.Exists(dbName))
                                        File.Delete(dbName);
                                    helper.CreateDatabase(dbConnectionString);
                                }
                                if (databaseType == DatabaseType.SQLite)
                                {
                                    package.SetStatus("Creating SQLite database...");
                                    var helper = new SqliteHelper();
                                    dbConnectionString = string.Format("Data Source={0};", dbName);
                                    if (System.IO.File.Exists(dbName))
                                        File.Delete(dbName);
                                    helper.CreateDatabase(dbConnectionString);
                                }

                                BackgroundWorker bw = new BackgroundWorker();
                                List<object> workerParameters = new List<object>();
                                workerParameters.Add(dbConnectionString);
                                workerParameters.Add(connectionString);
                                workerParameters.Add(ptd.Tables);
                                workerParameters.Add(databaseType.ToString());
                                workerParameters.Add(scope.ToString());

                                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                                bw.RunWorkerCompleted += (s, ea) =>
                                {
                                    try
                                    {
                                        if (ea.Error != null)
                                        {
                                            Helpers.DataConnectionHelper.SendError(ea.Error, databaseType, false);
                                        }
                                        Helpers.DataConnectionHelper.LogUsage("DatabasesExportFromServer");
                                    }
                                    finally
                                    {
                                        bw.Dispose();
                                    }
                                };
                                bw.RunWorkerAsync(workerParameters);

                            }
                            catch (Exception ex)
                            {
                                Helpers.DataConnectionHelper.SendError(ex, databaseType, false);
                            }
                        }
                   }
                }
            }
            catch (Exception ex)
            {
                Helpers.DataConnectionHelper.SendError(ex, DatabaseType.SQLServer);
            }
        }
        public void GenerateServerDgmlFiles(object sender, ExecutedRoutedEventArgs e)
        {
            // http://www.mztools.com/articles/2007/MZ2007011.aspx
            var menuItem = sender as MenuItem;
            if (menuItem == null) return;

            var databaseInfo = menuItem.CommandParameter as DatabaseMenuCommandParameters;
            if (databaseInfo == null) return;

            bool originalValue = Properties.Settings.Default.KeepSchemaNames;
            var dte = package.GetServiceHelper(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
            try
            {
                string connectionString = databaseInfo.DatabaseInfo != null
                    ? databaseInfo.DatabaseInfo.ConnectionString :
                    DataConnectionHelper.PromptForConnectionString(package);

                if (string.IsNullOrEmpty(connectionString)) return;
                string fileName;
                PickTablesDialog ptd = new PickTablesDialog();
                using (IRepository repository = Helpers.DataConnectionHelper.CreateRepository(new DatabaseInfo { ConnectionString = connectionString, DatabaseType = DatabaseType.SQLServer }))
                {
                    ptd.Tables = repository.GetAllTableNamesForExclusion();
                }

                bool? res = ptd.ShowModal();

                if (res.HasValue && res.Value == true)
                {
                    SaveFileDialog fd = new SaveFileDialog();
                    fd.Title = "Save generated DGML file as";
                    fd.Filter = "DGML (*.dgml)|*.dgml";
                    fd.OverwritePrompt = true;
                    fd.ValidateNames = true;
                    bool? result = fd.ShowDialog();
                    if (result.HasValue && result.Value == true)
                    {
                        Properties.Settings.Default.KeepSchemaNames = true;
                        fileName = fd.FileName;
                        using (IRepository repository = Helpers.DataConnectionHelper.CreateRepository(new DatabaseInfo { ConnectionString = connectionString, DatabaseType = DatabaseType.SQLServer }))
                        {
                            try
                            {
                                var generator = Helpers.DataConnectionHelper.CreateGenerator(repository, fileName, DatabaseType.SQLServer);
                                generator.GenerateSchemaGraph(connectionString, ptd.Tables);
                                dte.ItemOperations.OpenFile(fileName);
                                dte.ActiveDocument.Activate();
                                Helpers.DataConnectionHelper.LogUsage("DatabasesScriptDGML");
                            }
                            catch (Exception ex)
                            {
                                Helpers.DataConnectionHelper.SendError(ex, DatabaseType.SQLServer, false);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Helpers.DataConnectionHelper.SendError(ex, DatabaseType.SQLServer, false);
            }
            finally
            {
                Properties.Settings.Default.KeepSchemaNames = originalValue;
            }
        }
        public void ScriptServerDatabase(object sender, ExecutedRoutedEventArgs e)
        {
            var menuItem = sender as MenuItem;
            if (menuItem == null) return;
            Scope scope = (Scope)menuItem.Tag;
            var databaseInfo = menuItem.CommandParameter as DatabaseMenuCommandParameters;
            if (databaseInfo == null) return;

            try
            {
                string connectionString =  databaseInfo.DatabaseInfo != null
                    ? databaseInfo.DatabaseInfo.ConnectionString :
                    DataConnectionHelper.PromptForConnectionString(package);
                if (!string.IsNullOrEmpty(connectionString))
                {
                    string fileName;
                    PickTablesDialog ptd = new PickTablesDialog();
                    int totalCount = 0;
                    using (IRepository repository = Helpers.DataConnectionHelper.CreateRepository(new DatabaseInfo { ConnectionString = connectionString, DatabaseType = DatabaseType.SQLServer }))
                    {
                        ptd.Tables = repository.GetAllTableNamesForExclusion();
                        totalCount = ptd.Tables.Count;
                    }

                    bool? res = ptd.ShowModal();
                    if (res.HasValue && res.Value == true && (ptd.Tables.Count < totalCount))
                    {
                        SaveFileDialog fd = new SaveFileDialog();
                        fd.Title = "Save generated database script as";
                        fd.Filter = "SQL Server Compact Script (*.sqlce)|*.sqlce|SQL Server Script (*.sql)|*.sql|All Files(*.*)|*.*";
                        if (scope == Scope.SchemaDataSQLite || scope == Scope.SchemaSQLite)
                            fd.Filter = "SQL Script (*.sql)|*.sql|All Files(*.*)|*.*";
                        fd.OverwritePrompt = true;
                        fd.ValidateNames = true;
                        bool? result = fd.ShowDialog();
                        if (result.HasValue && result.Value == true)
                        {
                            fileName = fd.FileName;
                            using (IRepository repository = Helpers.DataConnectionHelper.CreateRepository(new DatabaseInfo { ConnectionString = connectionString, DatabaseType = DatabaseType.SQLServer }))
                            {
                                try
                                {
                                    var generator = Helpers.DataConnectionHelper.CreateGenerator(repository, fd.FileName, DatabaseType.SQLServer);
                                    generator.ExcludeTables(ptd.Tables);
                                    EnvDTEHelper.ShowMessage(generator.ScriptDatabaseToFile(scope));
                                    Helpers.DataConnectionHelper.LogUsage("DatabasesScriptServer");
                                }
                                catch (Exception ex)
                                {
                                    Helpers.DataConnectionHelper.SendError(ex, DatabaseType.SQLServer, false);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Helpers.DataConnectionHelper.SendError(ex, DatabaseType.SQLServer, false);
            }
        }
        public void ScriptDatabase(object sender, ExecutedRoutedEventArgs e)
        {
            var menuItem = sender as MenuItem;
            if (menuItem == null) return;
            Scope scope = (Scope)menuItem.Tag;

            var databaseInfo = ValidateMenuInfo(sender);
            if (databaseInfo == null) return;

            try
            {
                int totalCount = 0;
                PickTablesDialog ptd = new PickTablesDialog();
                using (IRepository repository = Helpers.DataConnectionHelper.CreateRepository(databaseInfo.DatabaseInfo))
                {
                    ptd.Tables = repository.GetAllTableNamesForExclusion();
                    totalCount = ptd.Tables.Count;
                }

                bool? res = ptd.ShowModal();
                if (res.HasValue && res.Value == true && (ptd.Tables.Count < totalCount))
                {
                    SaveFileDialog fd = new SaveFileDialog();
                    fd.Title = "Save generated database script as";
                    fd.Filter = "SQL Server Compact Script (*.sqlce)|*.sqlce|SQL Server Script (*.sql)|*.sql|All Files(*.*)|*.*";
                    if (scope == Scope.SchemaDataSQLite || scope == Scope.SchemaSQLite || databaseInfo.DatabaseInfo.DatabaseType == DatabaseType.SQLite)
                    {
                        fd.Filter = "SQLite Script (*.sql)|*.sql|All Files(*.*)|*.*";    
                    }
                    fd.OverwritePrompt = true;
                    fd.ValidateNames = true;
                    bool? result = fd.ShowDialog();
                    if (result.HasValue && result.Value == true)
                    {
                        var fileName = fd.FileName;
                        using (IRepository repository = Helpers.DataConnectionHelper.CreateRepository(databaseInfo.DatabaseInfo))
                        {
                            var generator = Helpers.DataConnectionHelper.CreateGenerator(repository, fd.FileName, databaseInfo.DatabaseInfo.DatabaseType);
                            generator.ExcludeTables(ptd.Tables);
                            EnvDTEHelper.ShowMessage(generator.ScriptDatabaseToFile(scope));
                            Helpers.DataConnectionHelper.LogUsage("DatabaseScriptCe");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Helpers.DataConnectionHelper.SendError(ex, databaseInfo.DatabaseInfo.DatabaseType, false);
            }

        }