will explain the entity classes which is More superior than datasets and typed
this article I will just explain the basics of entity Classes and how you can
retrieve data using them.
We are using Dataset for storing database values.When Compared to Entity
Classes Dataset is very slow.As you all Know Dataset contains Collection of
Tables its takes large memory for storing these tables.It reduces productivity. DataSets keep
your data in a relational format, making them powerful and easy to use with
relational databases. Unfortunately, this means you lose out on all the
benefits of OOPS.
Problems With DataSets:
conversion can fail because:· The value could be null· The developer might be
wrong about the underlying data type (againintimate knowledge of the database
schema is required). If you are using ordinal values, who knows what column is
actually atposition X2. 2.ds.Tables(0) might return a null reference (if any
part of the DAL method or thestored procedure failed
classes provides more flexibility since they access the data using special
methods defined in
the class library.Take
advantage of OOPS Techniques such as Inheritance and Encaptulation.
Steps For Sample:
1. Create a Customer class : In this Customer Class is Used to get and set the Customer Details.It contains ContactTitle,ContactName,CompanyName,City Properties.
2.Establish the DataBase Connection :
Establish the DataBaseConnection By Using the Below Method.Here I am Using DataReader For Reading a DataRow Values and Return the DataReader Object.For Establish a Connection I am Using Connection String.Connection String is Placed in Web.config file.Here I am Using Northwind DataBase For Getting Customer Details.
3. Get Single Customers :
If you Want to Get Single Row(ie.In First Row) I am using the Following Method.Set the Datas to Datareader Object.It Returns the Method PopulateCustomer.
Please Refer attached code to GetDatasFromDataBase() method
PoulateCustomer is the Method,it is used to Get the Single Values From the DataReader Object and then set into the Customer Properties likeContactTitle.ContactName,CompanyName,City.The PopulateCustomer method simply checks that if the "ContactTitle" and "ContactName" and "CompanyName" and "City" fields are not null and if its not null adds it to the Customerobject and returns the Customer object to the calling program.
For Mapping function we use IDataRecord,insted of using SqlDataReader.
Multiple Customers :
If you Want to get Multiple Customer we need to create another one class name as CustomerCollection.Here we inherits the class CollectionBase.You can also use ArrayList or HashTables for Adding CustomerDetails.CollectionBase is the Base class for all Collections thats what we are implemented the CollectionBase Class.
Inside the CustomerCollection Class I wrote one method For getting
MultipleCustomers .It returns the CustomerCollection Object.In this
Method I am getting datas from DataReader
CollectionBase works by storing any type of object inside private Arraylists, but exposing access to these private collections through methods that
only take a specific type
I explained the basics only. There is lot more you can do with them. Its always a good idea to use Entity Classes instead of returning datasets and data Readers from the DataAccess Layer. DataSets might work if your application is small but when your application grows in size the use of datasets will create complexity