How To Create Universal DAL to connect with any database
I am new to .net i need a universal database accessing dal layer with it must be coding friendly
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Narasimha Reddy ChennupalliPosted Jul 4, 2015, 1:40 PM
Create one Interface to access for different databases. The below steps help you to maintain universal DAL.
Steps:
1. Create Interface with the name IDataAccess
Public Interface IDataAccess
{
void DbConnection();
DataTable GetAllData();
bool InsertData();
bool UpdateData();
bool DeleteData();
}
2. Implement the interface for Sql database access with the class name DataAcessSql.cs and implement all the methods of IDataAccess interface
Public class DataAccessSql:IDataAccess
{
Public void DbConnection()
{
// write the code for Sql connection
}
Public DataTable GetAllData()
{
// write the code to get data from Sql database.
}
Public bool InsertData()
{
// write the code for inserting data into Sql database.
}
Public bool UpdateData()
{
// write the code for updating data into Sql database.
}
Public bool DeleteData()
{
// write the code for deleting data from Sql database.
}
}
3. Implement the interface for Oracle database access with the class name DataAcessOracle.cs and implement all the methods of IDataAccess interface
Public class DataAccessOracle:IDataAccess
{
Public void DbConnection()
{
// write the code for Oracle database connection
}
Public DataTable GetAllData()
{
// write the code to get data from Oracle database.
}
Public bool InsertData()
{
// write the code for inserting data into Oracle database.
}
Public bool UpdateData()
{
// write the code for updating data into Oracle database.
}
Public bool DeleteData()
{
// write the code for deleting data from Oracle database.
}
}
In this way we can maintain universal DA. We cannot get any conflicts by using different databases.
I hope this will help you to matain universal DAL, let me know if you have any queries on this.