Tutorials » AspNet tutorials »

Basic Database operations using ADO.NET

This article talks about the basic database operations using the ADO.NET classes.

The basic database operations performed on a database table are:

1. Create (insert record)
2. Read (select data)
3. Update (update existing data)
4. Delete (delete records)

The above operations are sometimes called "CRUD" (Create, Read, Update, Delete)

Most of the simple applications you develop will have the screens to perform the above operations.

For example, assume you are developing an application to manage the list of employees in a company. The first thing you may do is, create a database table called "Employees" in your database. Next thing is to develop couple of screens.

1. Create Employee

This screen will have the fields to accept various details of an Employee. User has to enter all the details and press a button to save all details to database.

This page will be using an SQL Query which will look something like this:

insert into EMPLOYEES (id, name, age, salary, designation) values (1, 'john', 30, 25000, 'Vice President')

2. A screen (page) which displays the list of all employees.

This page will be using an SQL Query which will look something like this:

select * from EMPLOYEES

The above query will return all the records from the database table. You will use some code in the page to display the records in a user friendsly manner. ASP.NET provides a control called "DataGrid" which can be used to display large number of records properly formatted.

3. Screen to update the details of an existing employee.

Typically, you can view the list of employees from the screen in step 2 and select a user to edit and update the details. This Update page will allow you to enter all details of the employee.

This page will be using an SQL Query similar to this:

update EMPLOYEES set name = 'john', age = 30, salary = 25000, designation = 'Vice President' where id = 1

4. The Delete screen will allow you delete any existing employee.

The sql query will be something like this:

delete EMPLOYEES where id = 1

Even though most of the applications allow all the above 4 operations for most of the tables, it is not necessary to have 4 pages each. Some people design the screens such a way that all 4 operations can be performed in one page. My personal preferance is 2 pages - 1 page to display the data and delete selected records and 1 page to create and update the record. Since the create and update page need to have the same set of fields, I prefer to use the same page for both operations. I will pass a query string to identify whether I have to insert a record or update.

All of the tables in your database does not require screens to perform CRUD operations. Only specific tables will need such screens. Generally, the tables which gets data from users of the application will need the screens to perform CRUD operations. Here is the general rule: "If you need to capture data from user to a table, you must provide the screens to perform the basic operations - create, read, update and delete".

Next Chapter: Database operations - Learn more
Previous Chapter: Core classes in ADO.NET
More Chapters: ASP.NET Tutorials
More Tutorials: Tutorial Index

Top Contributors
    Last 7 Daysmore...

    Awards & Gifts

    Online Members

    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India