コード例 #1
0
        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 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 connectionString = dcd.ConnectionString;
                    string fileName;

                    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))
                    {
                        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(*.*)|*.*";
                        fd.OverwritePrompt = true;
                        fd.ValidateNames   = true;
                        bool?result = fd.ShowDialog();
                        if (result.HasValue && result.Value == true)
                        {
                            fileName = fd.FileName;
                            using (IRepository repository = RepoHelper.CreateServerRepository(connectionString))
                            {
                                var generator = RepoHelper.CreateGenerator(repository, fd.FileName);
                                generator.ExcludeTables(ptd.Tables);
                                System.Windows.Forms.MessageBox.Show(generator.ScriptDatabaseToFile(scope));
                            }
                        }
                    }
                }
                dcd.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Helpers.DataConnectionHelper.ShowErrors(ex));
            }
        }
コード例 #2
0
        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));
            }
        }
コード例 #3
0
        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 DatabasesMenuCommandParameters;

            if (databaseInfo == null)
            {
                return;
            }
            var  treeViewItem  = databaseInfo.DatabasesTreeViewItem;
            bool originalValue = Properties.Settings.Default.KeepServerSchemaNames;

            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 connectionString = dcd.ConnectionString;
                    string fileName;

                    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))
                    {
                        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.KeepServerSchemaNames = true;
                            fileName = fd.FileName;
#if V35
                            using (IRepository repository = new ServerDBRepository(connectionString, Properties.Settings.Default.KeepServerSchemaNames))
#else
                            using (IRepository repository = new ServerDBRepository4(connectionString, Properties.Settings.Default.KeepServerSchemaNames))
#endif
                            {
                                var generator = RepoHelper.CreateGenerator(repository, fileName);
                                generator.GenerateSchemaGraph(connectionString, ptd.Tables);
                                MessageBox.Show(string.Format("Saved {0}", fileName));
                            }
                        }
                    }
                    dcd.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Helpers.DataConnectionHelper.ShowErrors(ex));
            }
            finally
            {
                Properties.Settings.Default.KeepServerSchemaNames = originalValue;
            }
        }