In ASP.NET Database Connection is one of the most important part. In case of web applications, lot of data are stored into database. To access that huge amount of data database connection is compulsory for any web application.
Here, let's introduce ADO.NET for this operation. ADO.NET is also a part of .NET framework. ADO.NET is a set of classes for providing access to data in .NET framework.
The primary namespaces that for ADO.NET which provide classes to interact with database are-
System.Data
System.Data.OleDb
System.Data.SqlClient
System.Data is the base namespace for ADO.NET API.
System.Data.OleDb and System.Data.SqlClient, provide set of classes which interact with database.
ADO.NET framework supports two models of data access architecture:
Connected Data Access Architecture
Disconnected Data Access Architecture
Connected Data Access Architecture
Description
Here, the web application makes the connection to the database first and then interacts with the SQL queries using the same request. Web application stays connected to the database even if it is not doing any database operation.
Below mentioned classes are used for this type of architecture-
SqlConnection
SqlCommand
SqlParameter
SqlDataReader
Disconnected Data Access Architecture
Description
Disconnected model makes the application execute the query, then retrieve the results and store it for processing. In this model, web application minimizes time to connect to database. Using one database object, data adapter and a connection object it interacts with database. The Disconnected Data Access Architecture is more flexible and powerful than the Connected Data Access Architecture.
The most important step for database connection is to modify connectionString in web.config file under solution.
[html]<connectionStrings>
<add name="spConnectionString" connectionString="server=localhost;database=spDb;uid=spUserId;password=spPassword;" />
</connectionStrings>[/html]
The above modification is needed in web.config file to connect to database.