예제 #1
0
        private void btnCreateDatabase_Click(object sender, EventArgs e)
        {
            try
            {
                if (_dbServer.Exists(cmbTargetDatabase.Text))
                {
                    _loggingWindow.WriteFormat("{0} already exists on server.", cmbTargetDatabase.Text);

                    MessageBox.Show("Database already exists on server!",
                                    "Database Exists",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Stop);
                    return;
                }

                var targetIndigo = new IndigoInfo(
                    cmbTargetDatabase.Text,
                    cmbTargetCollation.Text,
                    tbMasterKey.Text,
                    new System.IO.DirectoryInfo(tbExportPath.Text),
                    ckbExportKeys.Checked,
                    new System.IO.DirectoryInfo(tbKeyExportPath.Text),
                    tbExportKey.Text,
                    true);

                var confirmResult = MessageBox.Show(String.Format("This will create new database {0} on {1}, are you sure?", targetIndigo.DatabaseName, _dbServer.ServerName),
                                                    "Confirm Create Database",
                                                    MessageBoxButtons.YesNo,
                                                    MessageBoxIcon.Question);

                if (confirmResult == DialogResult.No)
                {
                    return;
                }

                _parentForm.Disbale();
                var ui = TaskScheduler.FromCurrentSynchronizationContext();

                CancellationTokenSource cts = new CancellationTokenSource();
                Task.Factory.StartNew(() => TaskProgress(cts.Token));

                Task.Factory.StartNew(() => _dbServer.CreateNewDatabase(targetIndigo))
                .ContinueWith(task =>
                {
                    _parentForm.Enable();
                    cts.Cancel();
                    progressBar.Value = 0;
                    cmbTargetDatabase.Items.Clear();
                    var dbs = _dbServer.DatabaseList().ToArray();
                    cmbTargetDatabase.Items.AddRange(dbs);
                }, ui);
            }
            catch (Exception ex)
            {
                _loggingWindow.Write(ex.ToString());
            }
        }
예제 #2
0
        private void btnConnectSource_Click(object sender, EventArgs e)
        {
            cmbTargetDatabase.Items.Clear();
            cmbSourceDbs.Items.Clear();
            cmbTargetCollation.Items.Clear();

            string sqlInstance = "local";

            if (!String.IsNullOrWhiteSpace(tbSourceServer.Text))
            {
                sqlInstance = tbSourceServer.Text;
            }

            CancellationTokenSource cts = new CancellationTokenSource();

            Task.Factory.StartNew(() => TaskProgress(cts.Token));
            tbSourceServer.Enabled = btnConnectSource.Enabled = false;

            try
            {
                if (_source != null && _source.Connected) //Disconnect From Instance
                {
                    _loggingWindow.WriteFormat("Disconnecting from {0}...", sqlInstance);

                    var ui = TaskScheduler.FromCurrentSynchronizationContext();

                    Task.Factory.StartNew(() => _source.Disconnect())
                    .ContinueWith(task =>
                    {
                        if (task.Exception != null)
                        {
                            tbSourceServer.Enabled = true;
                            _loggingWindow.Write(task.Exception.Flatten().ToString());
                        }
                        else
                        {
                            _loggingWindow.WriteFormat("Disconnected from {0}.", sqlInstance);
                            btnConnectSource.Text = "Connect";

                            gbCreateDB.Enabled     = gbMigrationOptions.Enabled = false;
                            tbSourceServer.Enabled = true;
                            tbSqlInfo.Text         = _source.ServerInfo;
                        }
                        cts.Cancel();
                        btnConnectSource.Enabled = true;
                    }, ui);
                }
                else //Connect to instance
                {
                    _source = new Source.SourceDAL(tbSourceServer.Text, CommandTimeout, _loggingWindow);

                    _loggingWindow.WriteFormat("Connecting to {0}...", sqlInstance);

                    var ui = TaskScheduler.FromCurrentSynchronizationContext();

                    Task.Factory.StartNew(() => _source.Connect())
                    .ContinueWith(task =>
                    {
                        if (task.Exception != null)
                        {
                            tbSourceServer.Enabled = true;
                            _loggingWindow.Write(task.Exception.Flatten().ToString());
                        }
                        else
                        {
                            _loggingWindow.WriteFormat("Connected to {0}.", sqlInstance);

                            var dbs = _source.DatabaseList().ToArray();
                            cmbTargetDatabase.Items.AddRange(dbs);
                            cmbSourceDbs.Items.AddRange(dbs);

                            cmbTargetCollation.Items.AddRange(_source.ServerCollations.ToArray());
                            cmbTargetCollation.SelectedText = _source.DefaultServerCollation;

                            tbSqlInfo.Text = _source.ServerInfo;

                            btnConnectSource.Text = "Disconnect";

                            gbCreateDB.Enabled = gbMigrationOptions.Enabled = true;
                        }
                        cts.Cancel();
                        btnConnectSource.Enabled = true;
                    }, ui);
                }
            }
            catch (AggregateException aex)
            {
                tbLogWindow.Text += aex.Flatten().ToString();
                cts.Cancel();
                progressBar.Value = 0;
            }
            catch (Exception ex)
            {
                tbLogWindow.Text += ex;
                cts.Cancel();
            }
        }
예제 #3
0
        public bool CreateNewDatabase(IndigoInfo targetIndigo)
        {
            _loggingWindow.Write("------------------------------------------- Create Database -------------------------------------------");
            var isDone = false;

            try
            {
                Database targetNewDB = new Database(_server, targetIndigo.DatabaseName);
                targetNewDB.Collation = targetIndigo.Collation;
                targetNewDB.Create();

                double total = ExecuteScripts(targetIndigo, ScriptTypes.Create, false);

                _loggingWindow.WriteFormat("Done creating schema, total time: {0}.", TimeSpan.FromMilliseconds(total).ToString(@"hh\:mm\:ss\.fff"));
                _loggingWindow.Write("Validating Schema.");

                _server.Databases.Refresh();
                var newDB = _server.Databases[targetIndigo.DatabaseName];

                if (newDB != null)
                {
                    StringBuilder sb  = new StringBuilder();
                    var           dir = Directory.CreateDirectory(Path.Combine(targetIndigo.ExportPath.FullName, DateTime.Now.ToString("yyyyMMddhhmmss")));

                    //Tables
                    foreach (Table table in newDB.Tables)
                    {
                        sb.AppendLine(table.Name);
                    }
                    sb.AppendLine("Total: " + newDB.Tables.Count);
                    File.WriteAllText(Path.Combine(dir.FullName, "tables.txt"), sb.ToString());

                    //Views
                    sb.Clear();
                    int viewCount = 0;
                    foreach (View view in newDB.Views)
                    {
                        if (view.Schema == "dbo")
                        {
                            viewCount++;
                            sb.AppendLine(view.Name);
                        }
                    }
                    sb.AppendLine("Total: " + viewCount);
                    File.WriteAllText(Path.Combine(dir.FullName, "views.txt"), sb.ToString());

                    //SPs
                    int spCount = 0;
                    sb.Clear();
                    foreach (StoredProcedure sp in newDB.StoredProcedures)
                    {
                        if (sp.Schema != "sys")
                        {
                            spCount++;
                            sb.AppendLine(sp.Name);
                        }
                    }

                    sb.AppendLine("Total: " + spCount);
                    File.WriteAllText(Path.Combine(dir.FullName, "storedProcs.txt"), sb.ToString());
                    sb.Clear();

                    //MasterKey
                    sb.Clear();
                    sb.AppendLine("Master Key: created " + newDB.MasterKey.CreateDate);

                    //SymmetricKeys
                    foreach (SymmetricKey key in newDB.SymmetricKeys)
                    {
                        sb.AppendLine("SymmetricKey: " + key.Name + " " + key.EncryptionAlgorithm + " " + key.KeyLength);
                    }

                    sb.AppendLine("Total SymmetricKeys: " + newDB.SymmetricKeys.Count);


                    //Certificates
                    foreach (Certificate cert in newDB.Certificates)
                    {
                        sb.AppendLine("Certificate:" + cert.Name + " " + cert.ExpirationDate + " " + cert.PrivateKeyEncryptionType);

                        if (targetIndigo.ExportEncryption)
                        {
                            cert.Export(Path.Combine(targetIndigo.ExportEncryptionPath.FullName, String.Format("{0}_{1}.crt", targetIndigo.DatabaseName, cert.Name)),
                                        Path.Combine(targetIndigo.ExportEncryptionPath.FullName, String.Format("{0}_{1}_pvtKey", targetIndigo.DatabaseName, cert.Name)),
                                        targetIndigo.ExportEncryptionPassword);
                        }
                    }

                    sb.AppendLine("Total Certificates: " + newDB.Certificates.Count);
                    File.WriteAllText(Path.Combine(dir.FullName, "encryption.txt"), sb.ToString());

                    sb.Clear();

                    string MasterKeyFile = Path.Combine(targetIndigo.ExportEncryptionPath.FullName, String.Format("{0}_MaskterKey", targetIndigo.DatabaseName));
                    //BackupKeys
                    if (targetIndigo.ExportEncryption)
                    {
                        newDB.MasterKey.Export(MasterKeyFile, targetIndigo.ExportEncryptionPassword);
                    }

                    //Load Defaults
                    _loggingWindow.Write("Loading lookup data.");

                    total = ExecuteScripts(targetIndigo, ScriptTypes.LoadLookup, true);

                    _loggingWindow.WriteFormat("Done loading lookup data, elapsed time {0}ms", TimeSpan.FromMilliseconds(total).ToString(@"hh\:mm\:ss\.fff"));

                    //Create Enterprise
                    if (targetIndigo.CreateEnterprise)
                    {
                        _loggingWindow.Write("Create enterprise data.");

                        total = ExecuteScripts(targetIndigo, ScriptTypes.CreateEnterprise, true);

                        _loggingWindow.WriteFormat("Done creating enterprise data, elapsed time {0}ms", TimeSpan.FromMilliseconds(total).ToString(@"hh\:mm\:ss\.fff"));
                    }


                    //if (newDB.Tables.Count == 155 &&
                    //        spCount == 355 &&
                    //        viewCount == 13 &&
                    //        newDB.SymmetricKeys.Count == 3 &&
                    //        newDB.Certificates.Count == 3)
                    //    isDone = true;
                    isDone = true;
                }
            }
            catch (Exception ex)
            {
                _loggingWindow.Write(ex.ToString());
            }

            if (isDone)
            {
                _loggingWindow.WriteFormat("Done creating {0}", targetIndigo.DatabaseName);
            }
            else
            {
                _loggingWindow.WriteFormat("Failed creating {0}", targetIndigo.DatabaseName);
            }

            //var completionSource = new TaskCompletionSource<bool>();
            //completionSource.SetResult(isDone);
            //return completionSource.Task;
            return(isDone);
        }