Home

File-Based Applications

 

File-Based Application

 

Introduction to Databases

A file-based database (also called a flat file database) is an application that stores one or various lists of information in regular, traditional text-based files.

To create a file-based application, you can use the C# language, Microsoft Visual Studio, or Microsoft Visual Web Developer, in combination with the .NET Framework.

 

Using Windows Controls and Accessories

To create an aesthetically user-friendly web site, you can use the various web controls that are implemented in the .NET Framework. 

The files of a database are located in a directory on a server and accessed by the visitors of your web page. Normally, it is you, the database developer, who create and manage the directory or directories used by the application.

Another characteristic of a database is that users do not create files. This means that there is no actual file processing on the part of the users. You, the database developer, provide a means of accessing the database. Then, the user adds, edits, or deletes values.

When creating a file-based database, one the first actions you should perform consists of setting up the directory where the file(s) of your application would be located. If you already know (and you should know) where and how the web site would be accessed, you can manually or programmatically create a folder.

The .NET Framework supports the creation and management of directories through various classes. The main class used to deal with directories is called Directory. Besides the Directory class, the .NET Framework provides support for folders through a class named DirectoryInfo. To use it, declare a variable of type DirectoryInfo using its constructor to initialize it.

To programmatically create a directory using the static Directory class, you can call its CreateDirectory() method that is overloaded with two versions. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Directory.CreateDirectory(@"E:\Bethesda Car Rental");
    }
}

To create a folder using the DirectoryInfo class, call its Create() method that comes in two versions. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;    

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(@"E:\Bethesda Car Rental");
        dirInfo.Create();
    }
}

When you call either the Directory.CreateDirectory() method or the DirectoryInfo.Create() method, if the directory does not exist, it would be created. If the directory exists already, nothing would happen.

Before performing any operation on a directory, you should first make sure it exists. To get this information, you can call the Directory.Exists() method that returns a Boolean value. This method takes as argument the path to the directory.

The Files of a File-Based Application

 

Introduction

In the .NET Framework, file processing is primarily supported through the System.IO namespace that is filled with various classes to deal with files and directories (folders). The most fundamental class of the System.IO namespace and used to perform file processing is called File. The abstract and sealed File class contains all necessary methods used to create a file, check the existence of a file, write information to a file, read information from a file, or manipulate the system attributes of a file.

Another one of the fundamental file processing classes is called Stream. This is mainly an abstract class that lays a foundation for other stream-oriented classes. One of the classes that derives from Stream is called FileStream.

Creating a File

To create a new file, you can use the File class, call one of the versions of its Create() method that takes an argument as the name of, or the path to, the file and returns a FileStream object.

 
 
 

Besides File, you can use the StreamWriter class to create a file. To do this, declare a variable of type StreamWriter and initialize it using one of its constructors.

Writing to a File

One of the most routine operations performed on a class consists of writing information to it. And one of the most useful classes in this domain is called StreamWriter. The StreamWriter class is derived from the TextWriter class.

To create a file using the StreamWriter class, you can declare a StreamWriter variable and initialize it using one of its constructors. After creating the file, you can write information to it by calling the Write() or the WriteLine() method. Make sure you close the stream after using it. Also make sure you use exception handling in your code.

 
 

Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        StreamWriter stmWrite =
		new StreamWriter(Server.MapPath(txtFileSave.Text + "mpl"));

        stmWrite.WriteLine(txtEmployeeNumber.Text);
        stmWrite.WriteLine(txtDateHired.Text);
        stmWrite.WriteLine(txtFirstName.Text);
        stmWrite.WriteLine(txtLastName.Text);
        stmWrite.WriteLine(ddlGenders.Text);
        stmWrite.WriteLine(txtHourlySalary.Text);

        stmWrite.Close();

        txtEmployeeNumber.Text = "";
        txtDateHired.Text      = DateTime.Today.ToString();
        txtFirstName.Text      = "";
        txtLastName.Text       = "";
        ddlGenders.Text        = "Unknown";
        txtHourlySalary.Text   = "0.00";
        txtFileSave.Text       = "";
        txtFileOpen.Text       = "";
    }
}

 File Processing

Besides StreamWriter, to create a file and write information to it, you can use the BinaryWriter class. You start by declaring a BinaryWriter variable and initialize it using one of its constructors, passing a Stream-based object.

Reading From a File

Before exploring the contents of a file, you must first open it. To open a file using the File class, you can call its Open method that is overloaded with three versions. If the information in the file is raw text, you can call the OpenText() method. After opening a file, you can read its content.

To support the ability to read from a file, you can use the StreamReader class that is derived from the TextReader class. To use it, declare a variable of type StreamReader and use one of its constructors to specify the name of, or the path to, the file. To read information from the file, you can call its Read() or its ReadLine() method. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;    

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        StreamWriter stmWrite = 
        	new StreamWriter(Server.MapPath(txtFileSave.Text + "mpl"));
        stmWrite.WriteLine(txtEmployeeNumber.Text);
        stmWrite.WriteLine(txtDateHired.Text);
        stmWrite.WriteLine(txtFirstName.Text);
        stmWrite.WriteLine(txtLastName.Text);
        stmWrite.WriteLine(ddlGenders.Text);
        stmWrite.WriteLine(txtHourlySalary.Text);

        stmWrite.Close();

        txtEmployeeNumber.Text = "";
        txtDateHired.Text      = DateTime.Today.ToString();
        txtFirstName.Text      = "";
        txtLastName.Text       = "";
        ddlGenders.Text        = "Unknown";
        txtHourlySalary.Text   = "0.00";
        txtFileSave.Text       = "";
        txtFileOpen.Text       = "";
    }
    protected void btnOpen_Click(object sender, EventArgs e)
    {
        using (StreamReader stmReader =
            new StreamReader(Server.MapPath(txtFileOpen.Text + "mpl")))
        {

            txtEmployeeNumber.Text = stmReader.ReadLine();
            txtDateHired.Text = stmReader.ReadLine();
            txtFirstName.Text = stmReader.ReadLine();
            txtLastName.Text = stmReader.ReadLine();
            ddlGenders.Text = stmReader.ReadLine();
            txtHourlySalary.Text = stmReader.ReadLine();
        }
    }
}

Instead of StreamReader, you can use the BinaryReader class to read information from a file.

 

 

   
 

Home Copyright © 2009-2011 C# Key

 

 

<%@ Page Language="C#"
    AutoEventWireup="true"
    CodeFile="index.aspx.cs"
    Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Exercise</title>

</head>
<body>
<h3>Employees Records</h3>

<form id="frmExercise" runat="server">

<table style="width:500px;">
  <tr>
    <td style="width:100px;">Employee #:</td>
    <td style="width:100px;">
      <asp:TextBox ID="txtEmployeeNumber" runat="server" Width="120px"></asp:TextBox>
    </td>
    <td>&nbsp;</td>
    <td>Date Hired:</td>
    <td>
      <asp:TextBox ID="txtDateHired" runat="server" Width="120px"></asp:TextBox>
      </td>
    </tr>
    <tr>
      <td>First Name:</td>
      <td>
        <asp:TextBox ID="txtFirstName" runat="server" Width="120px"></asp:TextBox>
      </td>
        <td>&nbsp;</td>
        <td>Last Name:</td>
        <td>
      <asp:TextBox ID="txtLastName" runat="server" Width="120px"></asp:TextBox>
        </td>
    </tr>
    <tr>
      <td>Gender:</td>
      <td>
        <asp:DropDownList ID="ddlGenders" runat="server" Width="120px">
          <asp:ListItem>Male</asp:ListItem>
          <asp:ListItem>Female</asp:ListItem>
          <asp:ListItem>Unknown</asp:ListItem>
          </asp:DropDownList>
      </td>
      <td>&nbsp;</td>
      <td>Hourly Salary:</td>
      <td>
        <asp:TextBox ID="txtHourlySalary" runat="server" Width="120px"></asp:TextBox>
      </td>
    </tr>
    <tr>
      <td colspan="5"><hr /></td>
    </tr>
    <tr>
      <td>
        <asp:Button ID="btnSave" runat="server" Text="Save" Width="97px" />
      </td>
      <td style="width:100px;">
        <asp:TextBox ID="txtFileSave" runat="server" Width="120px"></asp:TextBox>
      </td>
      <td style="width:100px;">
          &nbsp;</td>
      <td style="width:100px;">
        <asp:Button ID="btnOpen" runat="server" Text="Open" Width="97px" />
      </td>
      <td>
        <asp:TextBox ID="txtFileOpen" runat="server" Width="120px"></asp:TextBox>
      </td>
    </tr>
    </table>

</form>
</body>
</html>