Click or drag to resize

App and Data: file handling from/to database

This example implement a SOWI App part:

  • upload a file content and save to database table field

  • download a file content from database field value and save to file

This topic contains the following sections:

Overview Class Diagram
easy Plan App Schacht Zeichnung
App
File to Database (upload)

The App based on standard SOWI App (SOWIApp.Foundation.AppStandard) and SOWI Database Connector (SOWIData.Database.Connector)

C#
 1//* read file content *
 2lTestFileContent = SIC.File.Stream.GetByte(lTestFileName);
 3
 4//* initialize App object (SOWI Database Connector) *
 5lEasyPlanAppDataSchachtZeichnung = new easyPlanApp.Data.SchachtZeichnung(
 6                  easyPlanApp.Data.Settings.Database.ConnectionString, 
 7                  easyPlanApp.Test.Settings.User, 
 8                  easyPlanApp.Test.Settings.ClientID);
 9
10//* save file content to database *
11lEasyPlanAppDataSchachtZeichnung.Insert(new easyPlanData.SchachtZeichnung(
12                  easyPlanApp.Test.Settings.ClientID,
13                  lTestFileName,
14                  lTestFileContent,
15                  "image/jpeg"));

Complete test case example: project easyPlan load test file and saving to database table

C#
 1#region --- Preparation ---
 2this.Log.WriteTitle("TEST PREPARATION");
 3
 4this.Log.Write("Declaration easyPlan App Data Schacht Zeichnung object...");
 5easyPlanApp.Data.SchachtZeichnung lEasyPlanAppDataSchachtZeichnung = null;
 6
 7this.Log.Write("Declaration test file name...");
 8string lTestFileName = this.DataDirectory + "Vario-Rapid in ResVol.jpg";
 9
10this.Log.Write("Declaration test file content...");
11byte[] lTestFileContent = null;
12
13this.Log.Write("Test prepared");
14#endregion
15
16#region --- Execution ---
17this.Log.WriteTitle("TEST EXECUTION");
18this.Log.TestStart();
19try
20{
21    lTestFileContent = SIC.File.Stream.GetByte(lTestFileName);
22    lEasyPlanAppDataSchachtZeichnung = new easyPlanApp.Data.SchachtZeichnung(
23                      easyPlanApp.Data.Settings.Database.ConnectionString, 
24                      easyPlanApp.Test.Settings.User, 
25                      easyPlanApp.Test.Settings.ClientID);
26    lEasyPlanAppDataSchachtZeichnung.Insert(new easyPlanData.SchachtZeichnung(
27                      easyPlanApp.Test.Settings.ClientID,
28                      lTestFileName,
29                      lTestFileContent));
30}
31catch (Exception ex)
32{
33    this.Log.IsTrue(ref ex);
34}
35#endregion
File from Database (download)

The App based on standard SOWI App (SOWIApp.Foundation.AppStandard) and SOWI Database Connector (SOWIData.Database.Connector)

C#
 1//* initialize App object (SOWI Database Connector) *
 2lEasyPlanAppDataSchachtZeichnung = new easyPlanApp.Data.SchachtZeichnung(
 3                  easyPlanApp.Data.Settings.Database.ConnectionString,
 4                  easyPlanApp.Test.Settings.User,
 5                  easyPlanApp.Test.Settings.ClientID);
 6
 7  //* read content from database *
 8lTestFileContent = lEasyPlanAppDataSchachtZeichnung.GetAll().Last().Content;
 9
10  //* save field content to file *
11SIC.File.Stream.Write(lTestFileName, lTestFileContent);

Complete test case example: project easyPlan download data item content and saving to in test file

C#
 1#region --- Preparation ---
 2this.Log.WriteTitle("TEST PREPARATION");
 3
 4this.Log.Write("Declaration easyPlan App Data Schacht Zeichnung object...");
 5easyPlanApp.Data.SchachtZeichnung lEasyPlanAppDataSchachtZeichnung = null;
 6
 7this.Log.Write("Declaration test file name...");
 8string lTestFileName = this.ResultDirectory + this.Log.Name.ToString() + ".jpg";
 9
10this.Log.Write("Declaration test file content...");
11byte[] lTestFileContent = null;
12
13this.Log.Write("Test prepared");
14#endregion
15
16#region --- Execution ---
17this.Log.WriteTitle("TEST EXECUTION");
18this.Log.TestStart();
19try
20{
21    lEasyPlanAppDataSchachtZeichnung = new easyPlanApp.Data.SchachtZeichnung(
22                      easyPlanApp.Data.Settings.Database.ConnectionString,
23                      easyPlanApp.Test.Settings.User,
24                      easyPlanApp.Test.Settings.ClientID);
25    lTestFileContent = lEasyPlanAppDataSchachtZeichnung.GetAll().Last().Content;
26    SIC.File.Stream.Write(lTestFileName, lTestFileContent);
27}
28catch (Exception ex)
29{
30    this.Log.IsTrue(ref ex);
31}
32this.Log.TestEnd();
33this.Log.Write("Test executed");
34#endregion
Data Model

Create SOWI Data class

  1. Requirement: Microsoft Entity Framework

    Example: install MS Entity Framework to project SOWIWeb.Foundation.TestWeb

    Package Manager Console - Command
    Install-Package EntityFramework -Version 5.0.0 -ProjectName SOWIWeb.Foundation.TestWeb
  2. Add new SOWI Data project item

    Screen Test Project Add New Data

Add File Content fields

  1. Requirement: SOWIData.Helper

    Add reference SOWIData.Helper assembly

  2. Using interface IFileContent for data file handling (see SOWIData.Helper.InterfaceIFileContent)

    C#
    public class SchachtZeichnung : SOWIData.DataStandard, SOWIData.Helper.Interface.IFileContent
    {
        //...
    }
  3. Implement file content fields

    The field Name exists in SOWIData.Standard so implement fields Content and Type

    C#
    /// <summary>
    /// File content
    /// </summary>
    public byte[] Content { get; set; }
    
    /// <summary>
    /// File type e.g. MIME content type
    /// </summary>
    public string Type { get; set; }

    Can with helper implement interface member

    ScreenSOWIDataIFile Content Implement Interface Member
  4. Optional: helpful constructor

    Implement a class constructor with parameters: filename, file content and file type

    C#
    /// <summary>
    /// Constructor with parameter
    /// </summary>
    /// <param name="pFilename">file name</param>
    /// <param name="pContent">file content</param>
    /// <param name="pType">e.g. MIME content type</param>
    public SchachtZeichnung(string pFilename, byte[] pContent, string pType = "") : base()
    {
        this.Name = System.IO.Path.GetFileName(pFilename);
        this.Content = pContent;
        this.Type = pType;
    }

A complete data model class example:

C#
[Table("SchachtZeichnung")]
public class SchachtZeichnung : SOWIData.DataStandard, SOWIData.Helper.Interface.IFileContent
{
    #region --- constructors ---

    /// <summary>
    /// Constructor without parameter
    /// </summary>
    public SchachtZeichnung() : base() { }

    /// <summary>
    /// Constructor with parameter
    /// </summary>
    /// <param name="pFilename">file name</param>
    /// <param name="pContent">file content</param>
    /// <param name="pType">e.g. MIME content type</param>
    public SchachtZeichnung(string pFilename, byte[] pContent, string pType = "") : base()
    {
        this.Name = System.IO.Path.GetFileName(pFilename);
        this.Content = pContent;
        this.Type = pType;
    }

    #endregion

    #region --- fields ---
    //* note: field Name is a standard field (see SOWIData.DataStandard) *

    /// <summary>
    /// e.g. MIME content type
    /// </summary>
    public string Type { get; set; }

    /// <summary>
    /// File Content
    /// </summary>
    public byte[] Content { get; set; }

    #endregion

}

A simple example of data of file content see SOWIData.Helper.InterfaceIFileContent

See Also