In this article we will see how to store Text File
into Database and Retrieve Text File from the Database. In this article I will
show how to store text file into database in the form of array of bytes and
retrieving array of bytes into text file from the database. Now we will look
into the System requirements.
System
Requirements:
·
Microsoft Visual Studio 2010.
·
SQL Server 2008 R2.
First, we will look into design part first, the
below snapshot shows the 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 above class Id property is
the primary key, File Path property will take the path of file from system
which you will browse and TextToByte property will store the text contents from
text file in the form of array of bytes.
Procedure:
There are totally three button
events are occurring namely browse event, store file event and retrieve file
event as shown in above first snapshot. We will see the working of all three
events one by one.
1:
Browse Button Event:
When you click on browse button an
Open Dialog Box will open to select the text file, the path of text file will
be copied into text box. The code behind for browse button as 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 above code I am creating
an object for open dialog class (Namespace is Microsoft.Win32), then I am
calling show dialog method of Open dialog Class.Open dialog box is as shown in
below snapshot.
After Selecting Text File, When
you click on open button the system path of text file is stored in text box as
shown in below snapshot.
2:
Store Button Event:
After selecting the text file
when you click on store file button, the text file contents will be stored into
database in the form of array of bytes. Code behind for 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 below snapshot shows how text
file contents converted into array of bytes and the database details
3:Retrieve
Button Event:
When you click on the retrieve
button, the text file contents stored in Database in the form of array of bytes
again encoded into text using below 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 above code of retrieve button event. The
below snapshot shows file contents in a rich text box.
Summary:
In this article I have shown how
to store text file into database in the form of array of bytes and again
retrieving array of bytes into text form. If you any queries please do comment
and thank you for reading my article.