Storing and Retrieving Text File Using SQL Server Database in WPF

In this article we will see how to store a text file into a database and retrieve the text file from the Database. The text file will be stored into the database in the form of an array of bytes and the array of bytes will be retrieved into a text file from the database. Now we will look into the system requirements.

System Requirements

  • Microsoft Visual Studio 2010.
  • SQL Server 2008 R2.

We will first look into the design part first, the following snapshot shows the design of the application.

Design of Application

In this article I have used the code first approach to create the database. I have taken only one class named Text Data as shown below:

public class TextData

{

    public int Id { get; set; }

    public string FilePath { get; set; }

    public byte[] TextToByte { get; set; }


In the preceding class the Id property is the primary key. The File Path property will take the path of the file from the system that you will browse and the TextToByte property will store the text contents from the text file into the array of bytes.
 
Procedure

There are three button events in total that occurr, namely browse event, store file event and retrieve file event as shown in the snapshot above. We will see how all three events work one by one.
 
1. Browse Button Event

When you click on the "Browse" button, an Open Dialog Box will open to select the text file. The path of the text file will be copied into the text box. The code behind for the Browse button is shown below.
    

private void Browse_Click(object sender, RoutedEventArgs e)

{

     Database DB=new Database();

     TextData exam=new TextData();

     OpenFileDialog openFileDialog1 = new OpenFileDialog();

     openFileDialog1.ShowDialog();

     openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

     openFileDialog1.DefaultExt = ".txt";

     txtFilePath.Text = openFileDialog1.FileName;

}

In the code above I am creating an object for the Open Dialog class (the namespace is "Microsoft.Win32"), then I am calling the "ShowDialog" method of the OpenFileDialog Class.The Open dialog box is as shown in the following snapshot.

Open Dialog Box in WPF

After Selecting the text file, when you click on the "Open" button the system path of the text file is stored in the text box as shown in the following snapshot.

Selecting Text File

Store Button Event

After selecting the text file, when you click on the store file button, the text file contents will be stored into the database in the form of an array of bytes. The Code behind for the store file event is shown below.
 

private void Store_Click(object sender, RoutedEventArgs e)

{

    Database DB = new Database();

    TextData exam = new TextData();

    exam.FilePath = txtFilePath.Text;

    exam.TextToByte = File.ReadAllBytes(txtFilePath.Text);

    DB.TextFile.Add(exam);

    DB.SaveChanges();

}

The following snapshot shows how the text file contents is converted into an array of bytes and the database details.

Text File Content in Database

Retrieve Button Event

When you click on the retrieve button, the text file contents is stored in the database in the form of an array of bytes, again encoded into text using the following code.

private void Retrieve_Click(object sender, RoutedEventArgs e)

{  

    Database DB = new Database();

    TextData exam = new TextData();

    var result = (from t in DB.TextFile

                where t.FilePath==txtFilePath.Text

                select t.TextToByte).FirstOrDefault();

    var ss = System.Text.Encoding.Default.GetString(result);

    richTextBox1.AppendText(ss);



The encoded text file contents will be added to the rich text box using the code above of the retrieve button event. The following snapshot shows the file contents in a rich text box.

Retrieve File Contents From Database
 
Summary

In this article I have shown how to store a text file into a database in the form of an array of bytes and again retrieve the array of bytes into text form. If you have any queries then please do comment and thank you for reading my article.

Up Next
    Ebook Download
    View all
    Learn
    View all