Ejemplo n.º 1
0
    /////////////////////////////////////////////////////////////////////////////////////

    #region [ Load/Save Database Methods ]

    /// <summary>
    /// Loads database from file.
    /// </summary>
    ///
    private void LoadDatabase(string filename, string caption, bool silent = false)
    {
        string progressInfo = "Loading database from '" + filename + "'...";

        // Remove children from client area (probably referring to the contents
        // of the current database)
        //
        UnloadAllMdiChildForms();

        // Create progress bar
        //
        MyProgressBar progress = new MyProgressBar(this, progressInfo);

        Exception         lastException = null;
        VideoRentalOutlet loadedStore   = null;
        string            elapsedTime   = string.Empty;

        // Deserialize database using our file stream with progress info.
        //
        try
        {
            FileInfo fileInfo   = new FileInfo(filename);
            long     fileLength = fileInfo.Exists ? fileInfo.Length : 0;

            using (FileStreamWithProgressBar fs = new FileStreamWithProgressBar(
                       filename, FileMode.Open, progress, fileLength)
                   )
            {
                loadedStore = VideoRentalOutlet.Deserialize(fs);

                fs.StopTimer();
                elapsedTime = " in " + fs.VerboseElapsedTime;
            }
        }
        catch (Exception ex)
        {
            lastException = ex;
        }

        progress.Quit();

        if (loadedStore != null)
        {
            VideoStore          = loadedStore;
            VideoStore.Changed += VideoStore_Changed;

            string info = VideoStore.TotalRecordsCount.ToString()
                          + " records loaded from '" + filename + "'" + elapsedTime;

            if (silent)
            {
                this.InfoMessage = info;
            }
            else
            {
                info += "\n\n\n" + VideoStore.StatisticsInfo(info.Length) + "\n";

                MessageBox.Show(info, caption,
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            UpdateTitle();
        }
        else if (lastException != null)
        {
            MessageBox.Show(
                "There was an error while loading database.\n\n"
                + "Filename: " + filename + "\n\n"
                + "System Message:\n\n"
                + lastException.Message,
                caption, MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
    }
Ejemplo n.º 2
0
    /////////////////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Serializes database to file.
    /// </summary>
    ///
    private void SaveDatabase(string filename, string caption, bool silent = false)
    {
        // Create progress bar

        string progressInfo = "Saving database to '" + filename + "'...";

        // Create progress bar
        //
        MyProgressBar progress = new MyProgressBar(this, progressInfo);

        Exception lastException = null;
        bool      succeded      = false;
        string    elapsedTime   = string.Empty;

        // Serialize database using our file stream with progress info.
        //
        try
        {
            FileInfo fileInfo   = new FileInfo(filename);
            long     fileLength = fileInfo.Exists ? fileInfo.Length : 0;

            // Anticipate file length based on records count (if file length is 0
            // or there was significant increase in database)
            //
            fileLength = Math.Max(VideoStore.TotalRecordsCount * 160, fileLength);

            using (FileStreamWithProgressBar fs = new FileStreamWithProgressBar(
                       filename, FileMode.Create, progress, fileLength)
                   )
            {
                VideoStore.Serialize(fs);

                succeded = true;
                fs.StopTimer();
                elapsedTime = " in " + fs.VerboseElapsedTime;
            }
        }
        catch (Exception ex)
        {
            lastException = ex;
        }

        progress.Quit();

        // Report status
        //
        if (succeded)
        {
            string info = VideoStore.TotalRecordsCount.ToString()
                          + " records saved to '" + filename + "'" + elapsedTime;

            if (silent)
            {
                this.InfoMessage = info;
            }
            else
            {
                MessageBox.Show(
                    info + "\n\n\n" + VideoStore.StatisticsInfo(info.Length) + "\n",
                    caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        else if (lastException != null)
        {
            MessageBox.Show(
                "There was an error while saving database.\n\n"
                + "Filename: " + filename + "\n\n"
                + "System Message:\n" + lastException.Message,
                caption, MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
    }