Ejemplo n.º 1
0
        private void configureTransferOptions(Server targetServer, DTGSchemaTransfer sdbt)
        {
            if (!rbWindowsAuthentication2.Checked)
            {
                sdbt.DestinationLogin    = targetServer.ConnectionContext.Login;
                sdbt.DestinationPassword = targetServer.ConnectionContext.Password;
            }

            sdbt.CopyAllDatabaseTriggers   = chkCopyAllDatabaseTriggers.Checked;
            sdbt.CopyAllDefaults           = chkCopyAllDefaults.Checked;
            sdbt.CopyAllFullTextCatalogs   = chkCopyAllFullTextCatalogs.Checked;
            sdbt.CopyAllFullTextStopLists  = chkCopyAllFullTextStopLists.Checked;
            sdbt.CopyAllLogins             = false; // chkCopyAllLogins.Checked; // Causes The server principal 'NT SERVICE\Winmgmt' already exists.
            sdbt.CopyAllObjects            = chkCopyAllObjects.Checked;
            sdbt.CopyAllPartitionFunctions = chkCopyAllPartitionFunctions.Checked;
            sdbt.CopyAllPartitionSchemes   = chkCopyAllPartitionSchemes.Checked;
            sdbt.CopyAllPlanGuides         = chkCopyAllPlanGuides.Checked;
            sdbt.CopyAllRoles                 = chkCopyAllRoles.Checked;
            sdbt.CopyAllRules                 = chkCopyAllRules.Checked;
            sdbt.CopyAllSchemas               = chkCopyAllSchemas.Checked;
            sdbt.CopyAllSqlAssemblies         = chkCopyAllSqlAssemblies.Checked;
            sdbt.CopyAllStoredProcedures      = chkCopyAllStoredProcedures.Checked;
            sdbt.CopyAllSynonyms              = chkCopyAllSynonyms.Checked;
            sdbt.CopyAllTables                = chkCopyAllTables.Checked;
            sdbt.CopyAllUserDefinedAggregates = chkCopyAllUserDefinedAggregates.Checked;
            sdbt.CopyAllUserDefinedDataTypes  = chkCopyAllUserDefinedDataTypes.Checked;
            sdbt.CopyAllUserDefinedFunctions  = chkCopyAllUserDefinedFunctions.Checked;
            sdbt.CopyAllUserDefinedTableTypes = chkCopyAllUserDefinedTableTypes.Checked;
            sdbt.CopyAllUserDefinedTypes      = chkCopyAllUserDefinedTypes.Checked;
            sdbt.CopyAllUsers                 = false; // chkCopyAllUsers.Checked; // Causes The server principal 'NT SERVICE\Winmgmt' already exists.
            sdbt.CopyAllViews                 = chkCopyAllViews.Checked;
            sdbt.CopyAllXmlSchemaCollections  = chkCopyAllXmlSchemaCollections.Checked;
            sdbt.CopySchema = chkCopySchema.Checked;
        }
Ejemplo n.º 2
0
        protected void TransferDBSchema()
        {
            Server sourceServer = null;
            Server targetServer = null;

            string sourceDatabaseName = string.Empty;
            string targetDatabaseName = string.Empty;

            this.Invoke(new MethodInvoker(delegate
            {
                cmdCompare.Enabled = false;

                sourceDatabaseName = cboDatabase1.Text;
                targetDatabaseName = cboDatabase2.Text;

                if (rbSQLServerAuthentication1.Checked == true)
                {
                    ServerConnection conn = new ServerConnection();
                    conn.ServerInstance   = cboServer1.Text;
                    conn.LoginSecure      = false;
                    conn.Login            = txtUser1.Text;
                    conn.Password         = txtPassword1.Text;
                    sourceServer          = new Server(conn);
                }
                else
                {
                    ServerConnection conn = new ServerConnection();
                    conn.ServerInstance   = cboServer1.Text;
                    sourceServer          = new Server(conn);
                }


                if (rbSQLServerAuthentication2.Checked == true)
                {
                    ServerConnection conn = new ServerConnection();
                    conn.ServerInstance   = cboServer2.Text;
                    conn.LoginSecure      = false;
                    conn.Login            = txtUser2.Text;
                    conn.Password         = txtPassword2.Text;
                    targetServer          = new Server(conn);
                }
                else
                {
                    ServerConnection conn = new ServerConnection();
                    conn.ServerInstance   = cboServer2.Text;
                    targetServer          = new Server(conn);
                }
            }));

            try
            {
                string sourceServerVersion = sourceServer.Information.Version.ToString();
                string targetServerVersion = targetServer.Information.Version.ToString();

                Database sourceDatabase = sourceServer.Databases[sourceDatabaseName];



                //ToDo: GUI check if db exists and chkCreateTargetDatabase.Checked, confirm DROP db


                DTGSchemaTransfer sdbt = new DTGSchemaTransfer(sourceServer, sourceDatabaseName, targetServer, targetDatabaseName);

                configureTransferOptions(targetServer, sdbt);

                // SqlDbTransfer will not create the target database
                Database targetDatabase;
                if (chkCreateTargetDatabase.Checked)
                {
                    targetDatabase = sdbt.createTargetDatabase(sourceDatabase, targetServer, targetDatabaseName);
                }
                else
                {
                    targetDatabase = targetServer.Databases[targetDatabaseName];
                }



                //xfr.DestinationLoginSecure = true;
                sdbt.AddDataTransferEvent(DataTransferEvent_Handler);
                sdbt.AddProgressReportEvent(DiscoveryProgress_Handler);

                sdbt.TransferDatabaseSchema();

                this.Invoke(new MethodInvoker(() => txtScript.Clear()));

                foreach (var s in sdbt.TransferScripts())
                {
                    this.Invoke(new MethodInvoker(() => txtScript.AppendText(s + Environment.NewLine)));
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    cmdCompare.Enabled = true;
                }));
            }
        }