Unit 5: Advanced Server Side Issues: Database Connectivity
Unit 5: Advanced Server Side Issues: Database Connectivity
Database connectivity
For data incentive applications, we need a resilient connection to backing store. Every
languages/frameworks have their own conventions and driver sets to connect to database. In .NET,
ADO.NET (Active Data Object) has responsibility to connect to relational databases like Oracle, Sql
server, MySql etc. ADO.NET is also a part of the .NET Framework and is used to handle data access
services in .NET platform.
You can use ADO.NET to access data by using the new .NET Framework data providers which are:
Data Provider for SQL Server (System.Data.SqlClient).
Data Provider for OLEDB (System.Data.OleDb).
Data Provider for ODBC (System.Data.Odbc).
Data Provider for Oracle (System.Data.OracleClient).
ADO.NET is a set of classes that expose data access services to the .NET developer. The ADO.NET classes
are found in System.Data.dll and are integrated with the XML classes in System.Xml.dll. There are two
central components of ADO.NET classes: the DataSet, and the .NET Framework Data Provider.
Data Provider is a set of components including:
the Connection object (SqlConnection, OleDbConnection, OdbcConnection, OracleConnection)
the Command object (SqlCommand, OleDbCommand, OdbcCommand, OracleCommand)
the DataReader
object (SqlDataReader, OleDbDataReader, OdbcDataReader, OracleDataReader)
and the DataAdapter Object (SqlDataAdapter, OleDbDataAdapter, OdbcDataAdapter,
OracleDataAdapter).
DataSet object represents a disconnected cache of data which is made up of DataTables and
DataRelations that represent the result of the command.
For above set of classes we need to use raw sql queries for select, insert, update and delete:
Assuming Student table in db
--Selection
select * from Student
We also need to provide a connection string for a database used. Something like:
public string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=..\\..\\PersonDatabase.mdb";
<add name="TestEntities"
connectionString="metadata=res://*/Models.DbTest.csdl|res://*/Models.DbTest.ssdl|res://*/
Models.DbTest.msl;provider=System.Data.SqlClient;provider connection string="data
source=(LocalDb)\v11.0;initial catalog=DbTest;integrated
security=True;pooling=False;multipleactiveresultsets=True;application
name=EntityFramework"" providerName="System.Data.EntityClient" />
Today’s database development in .NET world uses ORM (Object Relational Mapping) tools like Entity
Framework (EF), NHibernate etc. These tools provide productivity to a developer abstracting ADO.NET
data access methods. We have already used EF in our basic projects using entity data model of entity
framework (EF) database-first where EF created context and model classes as:
File Handling
We already did a dictionary project where we use a plain text file as a data source. Text data is fetched
using API (Application Programming Interface) from System.IO (in .NET). This namespace provides
bunch of reader/writer classes to handle files and directories.
Instead of streamReader, we could have used BinaryReaders, TextReaders etc provided by System.IO.
Form Handling
Html forms can be handled by ASP.NET MVC pretty easily to collect some data from the user:
<form action="/test/addstudent" method="post">
<label>Name</label>
<input id="Name" name="Name" type="text" value="">
<label>Address</label>
<textarea id="Address" name="Address"></textarea>
<label>Blood Group</label>
<input id="BloodGroup" name="BloodGroup" type="text" value="">
<label>Phone Number</label>
<input data-val="true" data-val-number="The field Cell must be a number." id="Cell"
name="Cell" type="text" value="">
<label>Date of birth</label>
<input data-val="true" data-val-date="The field DOB must be a date." id="DOB"
name="DOB" type="text" value="">
<br />
<input type="submit" value="Create">
</form>
OR using Html Helpers given Razor view engine, same form can be created as:
The view file containing above code must be strongly typed (Ability to accept some model) to gather
form data as:
@model WebApp.Models.Student
Authentication
Authentication is the process of obtaining identification credentials such as name and password from a
user and validating those credentials against some authority. If the credentials are valid, the entity that
submitted the credentials is considered an authenticated identity. Once an identity has been
authenticated, the authorization process determines whether that identity has access to a given
resource.
ASP.NET implements authentication through authentication providers, the code modules that contain
the code necessary to authenticate the requestor's credentials. The topics in this section describe the
authentication providers built into ASP.NET.
Term Definition
Windows Authentication In this methodology ASP.NET web pages will use local windows users and groups to
(Integrated Windows authenticate and authorize resources. Provides information on how to use Windows
Authentication) authentication in conjunction with Microsoft Internet Information Services (IIS) authentication
to secure ASP.NET applications.
Forms Authentication Provides information on how to create an application-specific login form and perform
authentication using your own code. A convenient way to work with forms authentication is to
use ASP.NET membership and ASP.NET login controls, which together provide a way to collect
user credentials, authenticate them, and manage them, using little or no code.
Anonymous access If you do not want any kind of authentication then you will go for Anonymous
access.