Software & Finance





C# - Student Database Handling using Flat File System





I have come up with a simple menu driven version that can be used to add, edit, view and delete the student information system using flat file (binary file) system.


Refer to the following links for the same program in different languages:

1. Program in Turbo C++ for Menu Driven Student DB handling using Flat File

2. Program in Visual C++ for Menu Driven Student DB handling using Flat File

3. Program in C# for Menu Driven Student DB handling using Flat File

The complete source code and executable linkes are given at the bottom of this page

 

 

Source Code


using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

 

 

namespace CSStudentFileHandling

{

    class CursosPos

    {

        [StructLayout(LayoutKind.Sequential)]

        struct POSITION

        {

            public short x;

            public short y;

        }

 

        [DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

        private static extern int GetStdHandle(int nStdHandle);

 

        [DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

        private static extern int SetConsoleCursorPosition(int hConsoleOutput, POSITION dwCursorPosition);

 

        [DllImport("kernel32.dll", EntryPoint = "FillConsoleOutputCharacter", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

        private static extern int FillConsoleOutputCharacter(int hConsoleOutput, byte cCharacter, int nLength, POSITION dwWriteCoord, ref int lpNumberOfCharsWritten);

 

        public static void clrscr()

        {

            int STD_OUTPUT_HANDLE = -11;

            POSITION pos;

            pos.x = pos.y = 0;

            int writtenbytes = 0;

            FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), 32, 80 * 25, pos, ref writtenbytes);

        }

 

        public static void gotoxy(int x, int y)

        {

            int STD_OUTPUT_HANDLE = -11;

            POSITION pos;

            pos.x = (short)x;

            pos.y = (short)y;

            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

        }

 

        public static void gotoxy(short x, short y)

        {

            int STD_OUTPUT_HANDLE = -11;

            POSITION pos;

            pos.x = x;

            pos.y = y;

            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

        }

    };

 

    class Program

    {

 

    struct CStudent

    {

        public string name;

        public Int32 age;

        public string dept;

    };

 

    struct CStudents

    {

        string m_fileName;

        public int m_nMaxStudents;

        public CStudent [] m_studList;

 

        public CStudents(string filename)

        {

            m_nMaxStudents = 0;

            m_fileName = filename;

            m_studList = new CStudent[100];

        }

 

        public void AddRecord(string name, Int32 age, string dept)

        {

            int pos = m_nMaxStudents;

            m_studList[pos].name = name;

            m_studList[pos].dept = dept;

            m_studList[pos].age = age;

            m_nMaxStudents++;

            WriteRecords();

        }

 

        public void EditRecord(int pos, string name, Int32 age, string dept)

        {

            m_studList[pos].name = name;

            m_studList[pos].dept = dept;

            m_studList[pos].age = age;

            WriteRecords();

        }

 

        public void DeleteRecord(int pos)

        {

            m_nMaxStudents--;

 

            for(int i = pos; i < m_nMaxStudents; i++)

            {

                m_studList[i] = m_studList[i + 1];

            }

            WriteRecords();

        }

 

        public int ReadRecords()

        {

            try

            {

                System.IO.BinaryReader istream = new System.IO.BinaryReader(System.IO.File.Open(m_fileName, System.IO.FileMode.Open));

 

                if (istream == null)

                    return -1;

 

                Byte[] buf = new Byte[4096];

                int nTotalRecordsRead = 0;

 

                for (int i = 0; i < 100; i++)

                {

                    int t = istream.PeekChar();

                    if (t == -1)

                        break;

                        

                    m_studList[i].name = istream.ReadString();

                    m_studList[i].age = istream.ReadInt32();

                    m_studList[i].dept = istream.ReadString();

                    nTotalRecordsRead++;

                }

 

                istream.Close();

                m_nMaxStudents = nTotalRecordsRead;

 

                return nTotalRecordsRead;

            }

            catch(Exception e)

            {

                return -1;

            }

        }

 

        public int WriteRecords()

        {

            System.IO.BinaryWriter ostream = new System.IO.BinaryWriter(System.IO.File.Open(m_fileName, System.IO.FileMode.OpenOrCreate));

 

            if (ostream == null)

                return -1;

 

            int nTotalRecordsWritten = 0;

            char [] buf = new char[4096];

 

            for(int i = 0; i < m_nMaxStudents; i++)

            {

                ostream.Write(m_studList[i].name);

                ostream.Write(m_studList[i].age);

                ostream.Write(m_studList[i].dept);

                nTotalRecordsWritten++;

            }

 

            ostream.Close();

 

            return nTotalRecordsWritten;

        }

    };

 

 

    public static void ViewRecords()

    {

        theStudents.ReadRecords();

       

        CursosPos.clrscr();

           

        CursosPos.gotoxy(10,4);

        Console.Write("Welcome to Student Database Application");

 

        CursosPos.gotoxy(10,5);

        Console.Write("___________________________________________");

 

        CursosPos.gotoxy(10,6);

        Console.Write("SNo Student Name       Age    Department   ");

 

        CursosPos.gotoxy(10,7);

        Console.Write("___________________________________________");

 

        int pos = 8;

        // Enable Paging

        for(int i = 0; i < theStudents.m_nMaxStudents; i++)

        {

            CursosPos.gotoxy(10,pos);

            Console.Write(i + 1);

            CursosPos.gotoxy(14,pos);

            Console.Write(theStudents.m_studList[i].name);

            CursosPos.gotoxy(33,pos);

            Console.Write(theStudents.m_studList[i].age);

            CursosPos.gotoxy(42,pos);

            Console.Write(theStudents.m_studList[i].dept);

            pos++;

        }

        CursosPos.gotoxy(10,pos++);

        Console.Write("___________________________________________");

        pos++;

        CursosPos.gotoxy(10,pos++);

    }

 

 

    public static void InputRecords()

    {

        while(true)

        {

            CursosPos.clrscr();

           

            CursosPos.gotoxy(10,4);

            Console.Write("Welcome to Student Database Application");

 

            CursosPos.gotoxy(10,5);

            Console.Write("___________________________________________");

 

            CursosPos.gotoxy(10,6);

            Console.Write("Student Name: ");

 

            CursosPos.gotoxy(10,7);

            Console.Write("Age: ");

 

            CursosPos.gotoxy(10,8);

            Console.Write("Departement: ");

 

            CursosPos.gotoxy(10,9);

            Console.Write("___________________________________________");

 

            CursosPos.gotoxy(23,6);

            string name;

            name = Console.ReadLine();

 

            CursosPos.gotoxy(17,7);

            int age;

            age = Convert.ToInt32(Console.ReadLine());

 

            CursosPos.gotoxy(23,8);

            string dept;

            dept = Console.ReadLine();

 

            theStudents.AddRecord(name, age, dept);

 

            CursosPos.gotoxy(10,11);

            Console.Write("Do you want to add another record (Y/N)? ");

            char ch = Convert.ToChar(Console.ReadLine());

 

            if(ch == 'Y' || ch == 'y')

                continue;

            else

                break;

        }

    }

 

 

    public static void EditRecords()

    {

        ViewRecords();

        Console.Write("Enter the serial number you want to edit: ");

        int m;

        m = Convert.ToInt32(Console.ReadLine());

 

        if(m >= 1 && m <= theStudents.m_nMaxStudents)

        {

            CursosPos.clrscr();

            CursosPos.gotoxy(10,4);

            Console.Write("Welcome to Student Database Application");

 

            CursosPos.gotoxy(10,5);

            Console.Write("___________________________________________");

 

            CursosPos.gotoxy(10,6);

            Console.Write("Student Name: ");

 

            CursosPos.gotoxy(10,7);

            Console.Write("Age: ");

 

            CursosPos.gotoxy(10,8);

            Console.Write("Departement: ");

 

            CursosPos.gotoxy(10,9);

            Console.Write("___________________________________________");

 

            CursosPos.gotoxy(23,6);

            string name;

            name = Console.ReadLine();

 

 

            CursosPos.gotoxy(17,7);

            int age;

            age = Convert.ToInt32(Console.ReadLine());

 

            CursosPos.gotoxy(23,8);

            string dept;

            dept = Console.ReadLine();

 

            theStudents.EditRecord(m - 1, name, age, dept);       

            CursosPos.gotoxy(10,12);

            Console.Write("Record updated. Press any key to return to Main Menu");

            char ch = Convert.ToChar(Console.ReadLine());

        }

        else

        {

            CursosPos.gotoxy(10,12);

            Console.Write("Invalid Entry. Press any key to return to Main Menu");

            char ch = Convert.ToChar(Console.ReadLine());

        }

 

    }

 

    public static void DeleteRecords()

    {

        ViewRecords();

        Console.Write("Enter the serial number you want to delete: ");

        int m;

        m = Convert.ToInt32(Console.ReadLine());

        if(m >= 1 && m <= theStudents.m_nMaxStudents)

        {

            theStudents.DeleteRecord(m - 1);

            Console.Write("          Record deleted. Press any key to return to Main Menu");

            char ch = Convert.ToChar(Console.ReadLine());

        }

        else

        {

            Console.Write("          Invalid Entry. Press any key to return to Main Menu");

            char ch = Convert.ToChar(Console.ReadLine());

        }

    }

 

    static public int DisplayMainMenu()

    {

       

        CursosPos.clrscr();

        CursosPos.gotoxy(10, 4);

        Console.Write("Welcome to Student Database Application");

 

        CursosPos.gotoxy(10, 5);

        Console.Write("___________________________________________");

 

        CursosPos.gotoxy(10,6);

        Console.Write("1. Add Student Record");

 

        CursosPos.gotoxy(10, 7);

        Console.Write("2. Edit Student Record");

 

        CursosPos.gotoxy(10, 8);

        Console.Write("3. View Student Record");

 

        CursosPos.gotoxy(10, 9);

        Console.Write("4. Delete Student Record");

 

        CursosPos.gotoxy(10, 10);

        Console.Write("5. Exit");

 

        CursosPos.gotoxy(10, 11);

        Console.Write("___________________________________________");

 

        CursosPos.gotoxy(10, 13);

        Console.Write("Enter your Selection: ");

        int m = -1;

        m = Convert.ToInt32(Console.ReadLine());

 

        return m;

    }

 

 

    static CStudents theStudents;

        static void Main(string[] args)

        {

            theStudents = new CStudents("c:\\kathir.bin");

            theStudents.ReadRecords();

 

 

            while (true)

            {

                int selection = DisplayMainMenu();

                switch (selection)

                {

                    case 1:

                        InputRecords();

                        break;

                    case 2:

                        EditRecords();

                        break;

                    case 3:

                        {

                            ViewRecords();

                            Console.Write("Press any key to return to Main Manu: ");

                            char ch = Convert.ToChar(Console.ReadLine());

                        }

                        break;

                    case 4:

                        DeleteRecords();

                        break;

 

                    case 5:

                    default:

                        return;

                };

            }

        }

    }

}

Click here to download the C# source code and executable file. 

 

Output