Exemplo n.º 1
0
        internal void OptimizeDetailsList(Int32 detailsIistID)
        {
            _job = new OptimizingJob();
            _job.Load(Properties.Settings.Default.CutOptimaConnectionString,
                      new Int32[] { detailsIistID });
            _progressForm = new ProgressForm(new ProgressSource(_job));
            _progressForm.StartPosition = FormStartPosition.CenterParent;
            _progressForm.Cancel       += OnCancel;
            _progressForm.Show();

            _job.AsyncExecute();
            while (!_job.Join(100))
            {
                Application.DoEvents();
            }

            _progressForm.Close();

            if (_job.Status == StatusType.Error)
            {
                MessageBox.Show(_job.Error.Message);
            }
            else if (_job.Status == StatusType.Completed)
            {
                ShowCuttingResult(_job.Result);
            }
        }
Exemplo n.º 2
0
        public void CmdNewDatabase(IWin32Window owner)
        {
            if (_new_db_dialog == null)
            {
                _new_db_dialog = new NewDatabaseForm();
            }
            if (_new_db_dialog.ShowDialog(owner) != DialogResult.OK)
            {
                return;
            }

            if (!CmdCheckDuplicates(owner, _new_db_dialog.Server, _new_db_dialog.LocationType,
                                    _new_db_dialog.DbLocation))
            {
                return;
            }

            ProgressForm progressFrm = new ProgressForm(null);

            progressFrm.Text   = strings.DatabaseIsBeingCreated;
            progressFrm.Status = strings.ConnectingToTheDatabaseServer;
            progressFrm.Show(owner);
            progressFrm.Update();

            var builder = new System.Data.SqlClient.SqlConnectionStringBuilder();

            builder.DataSource         = _new_db_dialog.Server;
            builder.IntegratedSecurity = _new_db_dialog.windows_auth_cb.Checked;
            builder.UserID             = _new_db_dialog.username_tb.Text;
            builder.Password           = _new_db_dialog.password_tb.Text;
            var          connection = new System.Data.SqlClient.SqlConnection(builder.ConnectionString);
            var          cmd        = connection.CreateCommand();
            bool         createOk   = false;
            bool         dbCreated  = false;
            String       dbName     = "";
            DialogResult dr;

            try
            {
                while (true)
                {
                    try
                    {
                        connection.Open();
                        break;
                    }
                    catch (DbException ex)
                    {
                        dr = MessageBox.Show(owner, strings.CouldntConnectToTheDatabaseServer + ": " + ex.Message, null,
                                             MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                        switch (dr)
                        {
                        case DialogResult.Retry:
                            continue;

                        case DialogResult.Cancel:
                            return;

                        default:
                            Debug.Fail("Invalid case: " + dr);
                            return;
                        }
                    }
                }
                progressFrm.Progress = 10;
                progressFrm.Status   = strings.CreatingDatabase;
                progressFrm.Update();

                // создание новой базы
                //
                if (_new_db_dialog.LocationType == LocationType.Name)
                {
                    dbName          = _new_db_dialog.DbLocation;
                    cmd.CommandText = String.Format("CREATE DATABASE [{0}]", dbName);
                }
                else if (_new_db_dialog.LocationType == LocationType.Path)
                {
                    dbName = Path.GetFileNameWithoutExtension(_new_db_dialog.DbLocation);
                    // TODO: Check for " [ ] symbols in dbName
                    String fileName = dbName;
                    // TODO: Check for ' symbol in fileName
                    String dbPath = Path.GetFullPath(_new_db_dialog.DbLocation);
                    // TODO: Check for ' symbol in dbPath
                    String logName = Path.GetFileNameWithoutExtension(_new_db_dialog.DbLocation) + "_log";
                    // TODO: Check for ' symbol in logName
                    String logPath = Path.GetDirectoryName(_new_db_dialog.DbLocation) + '\\' +
                                     logName + ".LDF";
                    // TODO: Check for ' symbol in logPath
                    cmd.CommandText = String.Format("CREATE DATABASE [{0}] ON PRIMARY " +
                                                    "(NAME = N'{1}', FILENAME = N'{2}') " +
                                                    "LOG ON " +
                                                    "(NAME = N'{3}', FILENAME = N'{4}')", dbName, fileName, dbPath,
                                                    logName, logPath);
                }
                while (true)
                {
                    try
                    {
                        cmd.ExecuteNonQuery();
                        break;
                    }
                    catch (DbException ex)
                    {
                        dr = MessageBox.Show(owner, strings.CouldntCreateDatabase + ": " + ex.Message, null,
                                             MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                        switch (dr)
                        {
                        case DialogResult.Retry:
                            continue;

                        case DialogResult.Cancel:
                            return;

                        default:
                            Debug.Fail("Invalid case: " + dr);
                            return;
                        }
                    }
                }
                dbCreated            = true;
                progressFrm.Progress = 50;
                progressFrm.Update();

                try
                {
                    UseDb(connection, dbName);
                }
                catch (DbException ex)
                {
                    MessageBox.Show(owner, strings.CouldntSwitchToDatabase + ": " + ex.Message, null,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                progressFrm.Progress = 55;
                progressFrm.Update();

                // выполнение пакета создания схемы
                //
                string[] statements = Properties.Resources.CreateDB.Split(new string[] { "GO" },
                                                                          StringSplitOptions.RemoveEmptyEntries);
                int delta = 45 / statements.Length;
                foreach (string stmt in statements)
                {
                    cmd.CommandText = stmt;
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (DbException ex)
                    {
                        MessageBox.Show(owner, strings.CouldntCreateDatabaseSchema + ": " + ex.Message, null,
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    progressFrm.Progress += delta;
                    progressFrm.Update();
                }
                progressFrm.Progress = 100;
                progressFrm.Update();
                createOk = true;
            }
            finally
            {
                progressFrm.Close();
                if (!createOk && dbCreated)
                {
                    cmd.CommandText = "USE master DROP DATABASE [" + dbName + "]";
                }
                connection.Close();
            }
            builder                    = new System.Data.SqlClient.SqlConnectionStringBuilder();
            builder.DataSource         = _new_db_dialog.Server;
            builder.IntegratedSecurity = _new_db_dialog.windows_auth_cb.Checked;
            builder.UserID             = _new_db_dialog.username_tb.Text;
            builder.Password           = _new_db_dialog.password_tb.Text;
            if (_new_db_dialog.LocationType == LocationType.Name)
            {
                builder.InitialCatalog = _new_db_dialog.DbLocation;
            }
            else
            {
                builder.AttachDBFilename = _new_db_dialog.DbLocation;
            }
            AddDatabase(builder.ConnectionString);
            MessageBox.Show(owner, strings.DatabaseCreatedSuccessfully, strings.Success, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }