Chapter 6
Chapter 6
Chapter 6
𝐃𝐚𝐭𝐚𝐛𝐚𝐬𝐞 𝐩𝐫𝐨𝐠𝐫𝐚𝐦𝐦𝐢𝐧𝐠
The ADO.NET architecture
Most applications in most databases can’t be developed without having an interaction with the
database. Databases serve the purpose of data storage so the data can be retrieved later via either
a SQL query or a database application. Almost every software application running interacts with
either one or multiple databases. Therefore, the front end needs a mechanism to connect with
databases, and ADO.NET serves that purpose. Most of the .NET applications that require database
functionality are dependent on ADO.NET.
.NET, developers used data access technologies such as ODBC, OLE DB, and ActiveX Data
Object (ADO). With the introduction of .NET, Microsoft created a new way to work with data,
called ADO.NET.
ADO.NET is a set of classes exposing data access services to .NET programmers, providing a rich
set of components for creating distributed, data-sharing applications. ADO.NET is an integral part
of the .NET Framework and provides access to relational, XML, and application data. ADO.NET
classes are found in System.Data.dll.
ADO.NET’s connected architecture relies on a consistent database connection to access data and
perform any operations on the retrieved data. ADO.NET offers the following objects to help you
build your application with a connected architecture:
Connection: This is the main, or core, object for any database-oriented application. As
you might imagine, without knowing a data source’s statistics such as where it is located,
what database you would like to connect with, what user name and password it requires,
and so on, it would be impossible to establish a connection and perform any data-related
activity. Each .NET provider provides its own Connection object that offers features
targeted to specific data sources.
Command: This object represents the handling of statements that your application will
be using to perform data-oriented tasks, such as reading data or inserting or modifying
data. Hence, any SQL statement is actually executed via a Command object.
DataReader: DataReader involves creating an instance of the Command object and then
creating a DataReader by calling Command.ExecuteReader for data retrieval from your
data source, the returned data can be fetched in a read-only way through a DataReader
object. Data retrieval behavior of DataReader is also known as a read-forward-only fire-
hose cursor with fast speed.
Parameter: Parameter has always been an important part of any programming model.
Similarly, it is important in ADO.NET programming when it comes to passing values to
Command. A Parameter can be a value passed or returned to/from a stored procedure or
an argument passed to a SQL query.
DataAdapter: DataAdapter is the object ADO.NET exposes to bridge the gap between
connected and disconnected architectures to let applications establish a connection and
sync data into and from the data source.
If your application connects to an older version of SQL Server (6.5 or older) or to more than one kind of
database server at the same time (for example, an Access and an Oracle database connected
simultaneously), only then should you choose to use the OLE DB data provider.
No hard-and-fast rules exist; you can use both the OLE DB data provider for SQL Server and the Oracle
data provider (System.Data.OracleClient) if you want, but it’s important you choose the best provider
for your purpose. Given the performance benefits of the server-specific data providers, if you use SQL
Server, 99 percent of the time you should be using the System.Data.SqlClient classes.
Before we look at what each kind of data provider does and how it’s used, you need to be clear on their
core functionality. Each .NET data provider is designed to do the following two things very well:
Database connections are established by using the data provider’s Connection class (for example,
System.Data.SqlClient.SqlConnection). Other components such as data readers, commands, and data
adapters support retrieving data, executing SQL statements, and reading or writing to data sets or data
tables, respectively.
As you’ve seen, each data provider is prefixed with the type of data source it connects to (for instance,
the SQL Server data provider is prefixed with Sql), so its connection class is named SqlConnection. The
OLE DB data provider’s connection class is named OleDbConnection. Let’s understand the three data
providers that can be used with SQL Server.
Classes Description
Classes Description
Notice the similarity between the two data providers SqlClient and OleDb. The differences in
their implementations are transparent, and the user interface is fundamentally the same. The
ADO.NET OLE DB data provider requires that an OLE DB provider be specified in the
connection string.
Table 3 describes some OLE DB providers.
Provider Description
DB2OLEDB Microsoft OLE DB provider for DB2
SQLOLEDB Microsoft OLE DB provider for SQL Server
MicrosoftJet.OLEDB.4.0 Microsoft OLE DB provider for Access
(which uses the Jet engine)
MSDAORA Microsoft OLE DB provider for Oracle
MSDASQL Microsoft OLE DB provider for ODBC
Classes Description
Executes SQL queries, statements, or stored
OdbcCommand procedures
OdbcConnection Represents a connection to an ODBC data
source
OdbcDataAdapter Represents a bridge between a data set and a
data source
OdbcDataReader Provides a forward-only, read-only data
stream of rows from a data source
OdbcError Holds information on errors and warnings
returned by the data source
OdbcParameter Represents a command parameter
OdbcTransaction Represents a SQL transaction
LINQ Architecture
LINQ is an innovation that Microsoft made with the release of Visual Studio 2008 and .NET
Framework version 3.5 that promises to revolutionize the way developers work with data.
Microsoft has continued to improve LINQ with the recent releases of .NET 4.0/4.5 and Visual
Studio 2012. As mentioned, LINQ allows you to query various types of data sources including
relational databases, XML documents, and even in-memory data structures. LINQ supports all
these types of data stores with the help of LINQ query expressions of first-class language
constructs in C# 2012. LINQ offers the following advantages:
• LINQ offers common syntax for querying any type of data source; for example, you can
query an XML document in the same way you query a SQL database, an ADO.NET
dataset, an in-memory collection, or any other remote or local data source that you have
chosen to connect to and access by using LINQ.
• LINQ bridges the gap and strengthens the connection between relational data and the
object-oriented world.
• LINQ speeds development time by catching many errors at compile time and including
IntelliSense and debugging support.
• LINQ query expressions (unlike traditional SQL statements) are strongly typed.
LINQ consists of three major components:
• LINQ to Objects •
• LINQ to ADO.NET, which includes
o LINQ to SQL (formerly called DLinq)
o LINQ to DataSets (formerly called LINQ over DataSets)
o LINQ to Entities
• LINQ to XML (formerly called XLinq)
Table 5 LINQ Architecture
Reference
The following code describes that creating class and methods for the application.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Econtact.econtactClass
{
internal class contactClass
{
//geter setter properties
//Acts as data carreir in our application
}
catch(Exception ex)
{
}
finally
{
conn.Close();
}
return dt;
}
//Inserting data into database
}
finally
{
conn.Close();
}
return isSuccess;
}
// Method to Update Data
public bool Update(contactClass c)
{
//create a default return type and set its default value fale
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
//sql to update data in database
string sql = "UPDATE tbl_contact SET FirstName=@FirstName,
LastName=@LastName, ContactNo=@ContactNo, Address=@Address, Gender=@Gender WHERE
ContactID=@ContactID";
//creating sql command
SqlCommand cmd = new SqlCommand(sql, conn);
//create prameters to add values
cmd.Parameters.AddWithValue("@FirstName", c.FirstName);
cmd.Parameters.AddWithValue("@LastName", c.LastName);
cmd.Parameters.AddWithValue("@ContactNo", c.ContactNo);
cmd.Parameters.AddWithValue("@Address", c.Address);
cmd.Parameters.AddWithValue("@Gender", c.Gender);
cmd.Parameters.AddWithValue("@ContactID", c.ContactID);
//open database connection
conn.Open();
int rows = cmd.ExecuteNonQuery();
//if the query runs successfully then the value of rows will be
greater than zero else its value is 0
if(rows>0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch(Exception ex)
{
}
finally
{
conn.Close();
}
return isSuccess;
}
//Method to Delete Data from Databasae
}
finally
{
//connection close
conn.Close();
}
return isSuccess;
}
}
}
The following code identifies how to get the value from the input fields and how to load the data
from the application through database.
using Econtact.econtactClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Econtact
{
public partial class Econtact : Form
{
public Econtact()
{
InitializeComponent();
}
contactClass c = new contactClass();