Beispiel #1
0
        private static Form OpenDatabase(string fileName, string password)
        {
            try
            {
                return(new MainForm(fileName, password));
            }
            catch (LiteException ex)
            {
                if (ex.ErrorCode == LiteException.DATABASE_WRONG_PASSWORD)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        MessageBox.Show(ex.Message, fileName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    var passwordForm = new PasswordForm();
                    return(passwordForm.ShowDialog() != DialogResult.OK
                        ? null
                        : OpenDatabase(fileName, passwordForm.EnteredPassword));
                }
                var moreInfo = string.Empty;
                switch (ex.ErrorCode)
                {
                case LiteException.INVALID_DATABASE:
                    if (DetectIs090(fileName))
                    {
                        moreInfo =
                            @"Databases created by LiteDB v0.9 are not supported for viewing. Consider upgrading the files.";
                    }
                    else if (DetectIs104(fileName))
                    {
                        moreInfo =
                            @"Databases created by LiteDB v1 are not supported for viewing. Consider upgrading the files.";
                    }
                    break;

                case LiteException.INVALID_DATABASE_VERSION:
                    moreInfo =
                        @"Databases created by LiteDB v2.0.0-rc are not supported for viewing. Consider upgrading the files.";
                    break;
                }
                MessageBox.Show(
                    ex.Message + (string.IsNullOrEmpty(moreInfo) ? string.Empty : Environment.NewLine + moreInfo),
                    fileName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    @"Selected file is not a valid LiteDB Database file." + Environment.NewLine + ex.Message, fileName,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return(null);
        }
        private static Form OpenDatabase(string fileName, string password, bool upgrade = false)
        {
            try
            {
                return(new MainForm(fileName, password, upgrade));
            }
            catch (LiteException liteException)
            {
                if (liteException.ErrorCode == LiteException.DATABASE_WRONG_PASSWORD)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        MessageBox.Show(liteException.Message, fileName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    var passwordForm = new PasswordForm();
                    return(passwordForm.ShowDialog() != DialogResult.OK
                        ? null
                        : OpenDatabase(fileName, passwordForm.EnteredPassword));
                }
                var moreInfo = string.Empty;
                switch (liteException.ErrorCode)
                {
                case LiteException.INVALID_DATABASE:
                    if (DetectIs090(fileName))
                    {
                        moreInfo =
                            @"Databases created by LiteDB v0.9 are not supported for viewing. Consider upgrading the files.";
                    }
                    else if (DetectIs104(fileName))
                    {
                        moreInfo =
                            @"Databases created by LiteDB v1 are not supported for viewing. Consider upgrading the files.";
                    }
                    break;

                case LiteException.INVALID_DATABASE_VERSION:
                    if (!upgrade)
                    {
                        moreInfo =
                            @"Databases created by LiteDB v2.0 are not supported for viewing. Consider upgrading the files. Should we try upgrading the file?";
                        if (
                            MessageBox.Show(moreInfo, fileName, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
                                            MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            var newFileAddress = Path.ChangeExtension(fileName, ".v3" + Path.GetExtension(fileName));
                            try
                            {
                                File.Copy(fileName, newFileAddress, false);
                            }
                            catch (Exception copyExceptionex)
                            {
                                moreInfo = copyExceptionex.Message + Environment.NewLine +
                                           "Failed to copy the file prior to upgrading.";
                                MessageBox.Show(moreInfo, fileName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return(null);
                            }
                            return(OpenDatabase(newFileAddress, password, true));
                        }
                    }
                    else
                    {
                        moreInfo =
                            @"Databases created by LiteDB v2.0 are not supported for viewing. Consider upgrading the files.";
                    }
                    break;
                }
                MessageBox.Show(
                    liteException.Message +
                    (string.IsNullOrEmpty(moreInfo) ? string.Empty : Environment.NewLine + moreInfo),
                    fileName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (NotSupportedException notSupportedException)
            {
                MessageBox.Show(notSupportedException.Message, fileName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception exception)
            {
                MessageBox.Show(
                    @"Selected file is not a valid LiteDB Database file." + Environment.NewLine + exception.Message,
                    fileName,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return(null);
        }