Ejemplo n.º 1
0
        public void GenerateDataDiffScript(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                var menuInfo = ValidateMenuInfo(sender);
                var package  = ParentWindow.Package as SqlCeToolboxPackage;

                if (menuInfo == null)
                {
                    return;
                }

                Dictionary <string, DatabaseInfo> databaseList = DataConnectionHelper.GetDataConnections(package, true, false);
                foreach (KeyValuePair <string, DatabaseInfo> info in DataConnectionHelper.GetOwnDataConnections())
                {
                    if (!databaseList.ContainsKey(info.Key))
                    {
                        databaseList.Add(info.Key, info.Value);
                    }
                }
                foreach (KeyValuePair <string, DatabaseInfo> info in databaseList)
                {
                    string sourceType = string.Empty;
                    switch (info.Value.DatabaseType)
                    {
                    case DatabaseType.SQLCE35:
                        sourceType = "3.5";
                        break;

                    case DatabaseType.SQLCE40:
                        sourceType = "4.0";
                        break;

                    case DatabaseType.SQLServer:
                        sourceType = "Server";
                        break;
                    }
                    info.Value.Caption = string.Format("{0} ({1})", info.Value.Caption, sourceType);
                }

                var cd = new CompareDialog(menuInfo.DatabaseInfo.Caption, databaseList, menuInfo.Name);

                var result = cd.ShowModal();
                if (!result.HasValue || !result.Value || (cd.TargetDatabase.Key == null))
                {
                    return;
                }
                var target       = cd.TargetDatabase;
                var source       = new KeyValuePair <string, DatabaseInfo>(menuInfo.DatabaseInfo.ConnectionString, menuInfo.DatabaseInfo);
                var editorTarget = target;
                if (editorTarget.Value.DatabaseType == DatabaseType.SQLServer)
                {
                    editorTarget = source;
                }

                using (var sourceRepository = DataConnectionHelper.CreateRepository(source.Value))
                {
                    using (var targetRepository = DataConnectionHelper.CreateRepository(target.Value))
                    {
                        var generator = DataConnectionHelper.CreateGenerator(targetRepository, target.Value.DatabaseType);
                        try
                        {
                            var script = SqlCeDiff.CreateDataDiffScript(sourceRepository, menuInfo.Name, targetRepository, menuInfo.Name, generator);

                            if (package != null)
                            {
                                var sqlEditorWindow = package.CreateWindow <SqlEditorWindow>();
                                var editorControl   = sqlEditorWindow.Content as SqlEditorControl;
                                if (editorControl != null)
                                {
                                    editorControl.ExplorerControl = ParentWindow.Content as ExplorerControl;
                                    editorControl.DatabaseInfo    = editorTarget.Value;
                                    editorControl.SqlText         = script;
                                }
                            }
                            DataConnectionHelper.LogUsage("TableScriptDataDiff");
                        }
                        catch (Exception ex)
                        {
                            DataConnectionHelper.SendError(ex, source.Value.DatabaseType, false);
                        }
                    }
                }
            }
            catch (ArgumentException ae)
            {
                EnvDteHelper.ShowError(ae.Message);
            }
            catch (Exception ex)
            {
                DataConnectionHelper.SendError(ex, DatabaseType.SQLCE35, false);
            }
        }
        public void GenerateDiffScript(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                var databaseInfo = ValidateMenuInfo(sender);
                if (databaseInfo == null)
                {
                    return;
                }

                var databaseList = DataConnectionHelper.GetDataConnections(package, true, false);
                foreach (var info in DataConnectionHelper.GetOwnDataConnections())
                {
                    if (!databaseList.ContainsKey(info.Key))
                    {
                        databaseList.Add(info.Key, info.Value);
                    }
                }
                foreach (var info in databaseList)
                {
                    var sourceType = string.Empty;
                    switch (info.Value.DatabaseType)
                    {
                    case DatabaseType.SQLCE35:
                        sourceType = "3.5";
                        break;

                    case DatabaseType.SQLCE40:
                        sourceType = "4.0";
                        break;

                    case DatabaseType.SQLServer:
                        sourceType = "Server";
                        break;
                    }
                    info.Value.Caption = string.Format("{0} ({1})", info.Value.Caption, sourceType);
                }

                var cd = new CompareDialog(databaseInfo.DatabaseInfo.Caption, databaseList);

                var result = cd.ShowModal();
                if (!result.HasValue || !result.Value || (cd.TargetDatabase.Key == null))
                {
                    return;
                }
                var target = cd.TargetDatabase;
                var swap   = cd.SwapTarget;

                var source = new KeyValuePair <string, DatabaseInfo>(databaseInfo.DatabaseInfo.ConnectionString, databaseInfo.DatabaseInfo);
                if (swap)
                {
                    source = target;
                    target = new KeyValuePair <string, DatabaseInfo>(databaseInfo.DatabaseInfo.ConnectionString, databaseInfo.DatabaseInfo);
                }

                var editorTarget = target;
                if (editorTarget.Value.DatabaseType == DatabaseType.SQLServer)
                {
                    editorTarget = source;
                }

                using (var sourceRepository = Helpers.RepositoryHelper.CreateRepository(source.Value))
                {
                    var generator = DataConnectionHelper.CreateGenerator(sourceRepository, databaseInfo.DatabaseInfo.DatabaseType);
                    using (var targetRepository = Helpers.RepositoryHelper.CreateRepository(target.Value))
                    {
                        try
                        {
                            SqlCeDiff.CreateDiffScript(sourceRepository, targetRepository, generator, Properties.Settings.Default.DropTargetTables);

                            var sqlEditorWindow = package.CreateWindow <SqlEditorWindow>();
                            var editorControl   = sqlEditorWindow.Content as SqlEditorControl;
                            if (editorControl != null)
                            {
                                editorControl.ExplorerControl = _parentWindow.Content as ExplorerControl;
                                Debug.Assert(editorControl != null);
                                editorControl.DatabaseInfo = editorTarget.Value;

                                var explain = @"-- This database diff script contains the following objects:
-- - Tables:  Any that are not in the destination
-- -          (tables that are only in the destination are NOT dropped, unless that option is set)
-- - Columns: Any added, deleted, changed columns for existing tables
-- - Indexes: Any added, deleted indexes for existing tables
-- - Foreign keys: Any added, deleted foreign keys for existing tables
-- ** Make sure to test against a production version of the destination database! ** " + Environment.NewLine + Environment.NewLine;

                                if (swap)
                                {
                                    explain += "-- ** Please note that the soruce and target have been swapped! ** " + Environment.NewLine + Environment.NewLine;
                                }

                                editorControl.SqlText = explain + generator.GeneratedScript;
                            }
                            DataConnectionHelper.LogUsage("DatabaseScriptDiff");
                        }
                        catch (Exception ex)
                        {
                            DataConnectionHelper.SendError(ex, DatabaseType.SQLCE35);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DataConnectionHelper.SendError(ex, DatabaseType.SQLCE35);
            }
        }
        public void GenerateDiffScript(object sender, ExecutedRoutedEventArgs e)
        {
            var databaseInfo = ValidateMenuInfo(sender);

            if (databaseInfo == null)
            {
                return;
            }

            try
            {
                SortedDictionary <string, string> databaseList = Helpers.DataConnectionHelper.GetDataConnections();
                foreach (KeyValuePair <string, string> info in databaseList)
                {
                    if (!databaseList.ContainsKey(info.Key))
                    {
                        databaseList.Add(info.Key, info.Value);
                    }
                }

                CompareDialog cd = new CompareDialog(databaseInfo.Caption, databaseList);

                bool?result = cd.ShowDialog();
                if (result.HasValue && result.Value == true && (cd.TargetDatabase.Key != null))
                {
                    var target = cd.TargetDatabase.Value;
                    var source = databaseInfo.Connectionstring;

                    var editorTarget = target;
                    using (IRepository sourceRepository = RepoHelper.CreateRepository(source))
                    {
                        var generator = RepoHelper.CreateGenerator(sourceRepository);
                        using (IRepository targetRepository = RepoHelper.CreateRepository(target))
                        {
                            try
                            {
                                SqlCeDiff.CreateDiffScript(sourceRepository, targetRepository, generator, Properties.Settings.Default.DropTargetTables);

                                string explain = @"-- This database diff script contains the following objects:
-- - Tables:  Any that are not in the destination
-- -          (tables that are only in the destination are not dropped)
-- - Columns: Any added, deleted, changed columns for existing tables
-- - Indexes: Any added, deleted indexes for existing tables
-- - Foreign keys: Any added, deleted foreign keys for existing tables
-- ** Make sure to test against a production version of the destination database! ** " + Environment.NewLine + Environment.NewLine;
                                databaseInfo.Connectionstring = cd.TargetDatabase.Value;
                                databaseInfo.Caption          = cd.TargetDatabase.Key;
                                OpenSqlEditorToolWindow(databaseInfo, explain + generator.GeneratedScript);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(Helpers.DataConnectionHelper.ShowErrors(ex));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Helpers.DataConnectionHelper.ShowErrors(ex));
            }
        }