Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Event Driving programming

Chapter 7: Database Programming

Event driving Programming(C#) by Dagne W. 1


What is ADO.NET?
 ADO.NET is not a different technology. In simple terms, you can think
of ADO.NET, as a set of classes(Framework), that can be used to
interact with data sources like Databases and XML files. This data can,
then be consumed in any .NET application.
 ADO stands for Microsoft ActiveX Data Objects. • The following, a few
of the different types of .NET applications that use ADO.NET to
connect to a database, execute commands, and retrieve data. • ASP.NET
Web Applications • Windows Applications • Console Applications
 Dot Net Data Providers:
 Data Provider for SQL Server-System.Data.SqlClient
 Data Provider for Oracle-System.Data.OracleClient
 Data Provider for OLEDB-System.Data.OleDb
 Data Provider for ODBC-System.Data.Odbc

Event driving Programming(C#) by Dagne W. 2


What is ADO.NET…

Event driving Programming(C#) by Dagne W. 3


NET Framework Data Provider for SQL Server
 Class
o SqlConnection: It is used to create SQL Server connection.
This class cannot be inherited.
o SqlCommand: It is used to execute database queries. This
class cannot be inherited.
o SqlDataAdapter: It represents a set of data commands and
a database connection that are used to fill the DataSet. This
class cannot be inherited.
o SqlDataReader: It is used to read rows from a SQL Server
database. This class cannot be inherited.
 error is

Event driving Programming(C#) by Dagne W. 4


NET Framework Data Provider for SQL Server
 Object
o Connection: It is used to establish a connection to a specific
data source.
o Command: It is used to execute queries to perform
database operations.
o DataReader: It is used to read data from data source. The
DbDataReader is a base class for all DataReader objects.
o DataAdapter: It populates a DataSet and resolves updates
with the data source. The base class for all DataAdapter
objects is the DbDataAdapter class.
 error is

Event driving Programming(C#) by Dagne W. 5


for MySQL Server (wamp,xampp)

Class
o Connectionstring string connectionstring =
"datasource=localhost;port=3306;username=root;password
=;database=login";
o MySqlConnection: It is used to create MySQL Server
connection. This class cannot be inherited.
o MySqlCommand: It is used to execute database queries.
This class cannot be inherited.
o MySqlDataAdapter: It represents a set of data commands
and a database connection that are used to fill the DataSet.
This class cannot be inherited.
o MySqlDataReader: It is used to read rows from a MySQL
Server database. This class cannot be inherited
Event driving Programming(C#) by Dagne W. 6
for MySQL Server (wamp,xampp)

 Object
o Connection: It is used to establish a connection to a specific
data source.
o Command: It is used to execute queries to perform
database operations.
o DataReader: It is used to read data from data source. The
DataReader is a base class for all DataReader objects.
o DataAdapter: It populates a DataSet and resolves updates
with the data source. error is

Event driving Programming(C#) by Dagne W. 7


for OleDb (access Database)
 Classes
 Connection string for access database
Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\myFolder\myAccessFile.accdb;
 Persist Security Info=False;
 OleDb Connection: It is used to create OleDb Server
connection. This class cannot be inherited.
 OleDb Command: It is used to execute database queries.
This class cannot be inherited.
 OleDb DataAdapter: It represents a set of data commands
and a database connection that are used to fill the DataSet.
This class cannot be inherited.
 OleDb DataReader:It is used to read rows from a OleDb
database. errorEvent
is driving Programming(C#) by Dagne W. 8
for OleDb (access Database)
 Objects
o Connection: It is used to establish a connection to a specific
data source.
o Command: It is used to execute queries to perform
database operations.
o DataReader: It is used to read data from data source. The
DataReader is a base class for all DataReader objects.
o DataAdapter: It populates a DataSet and resolves updates
with the data source. The base class for all DataAdapter
objects is the DataAdapter class.

Event driving Programming(C#) by Dagne W. 9


Sql connection object…
 Sql connection allow us to establish a connection to SQL
server. • Properties of the SqlConnection object •

 ConnectionString:Gets or Sets the string used to open SQL


Server database • isa name-value pair.

 • Contains the data source (where the server is located), the


database name, user Id (user name) and Passwordfor the
database. •

Event driving Programming(C#) by Dagne W. 10


Sql connection object…
 Connection Time out get the time to wait trying to
establish a connection before terminating the attempt and
generating an error. •

 Methods:

 Open():Opens a database connection with the property


settings specified by the ConnectionString.

 • Close()Closes the connection to the database. This is the


preferred way to close any opened connections.

 • ChangeDatabase(string database ):Changes the current


database for anEvent
open System.Data.SqlClient.SqlConnection 11
driving Programming(C#) by Dagne W.
Sql connection object…

Event driving Programming(C#) by Dagne W. 12


Sql Connection Object…
as possible. • Connections should be closed in the finally block, or using,
the Using statement. •

The 2 uses of using statement in C# are

• To import a namespace. Example: using System;

• To close connections properly as shown

Event driving Programming(C#) by Dagne W. 13


SQL command
SqlCommand in ADO.NET •

In this session we will learn

1. Purpose of SqlCommandobject

2. Creating an instance of the SqlCommandClass

3. When and how to use ExecuteReader(), ExecuteScalar()


and ExecuteNonQuery() methods of the sqlcommand
object. • What is SqlCommand? • SqlCommandClass is
used to prepare an SQL statement or StoredProcedure
that we want to execute on a SQL Server database
Event driving Programming(C#) by Dagne W. 14
SQL command object properties

SqlCommand object–Properties.

 Connection:Gets or Sets the SqlConnection used by this


instance of SqlCommand class

 CommandText:Gets or sets the Transact –SQL statement,


table name or stored procedure to execute at the data
source

 CommandType:Gets or sets a value indicating how the


SqlCommand.CommandTextproperty is interpreted(
StoredProcedureor CommandType.TableDirect)

Event driving Programming(C#) by Dagne W. 15


SqlCommand object –Methods. •
The most commonly used methods of the SqlCommand class

1. ExecuteReader-use when the T-SQL statement returns more than a


single value. For example, if the query returns rows of data. Returns
SqlDataReaderobject

2. ExecuteNonQuery–Use when you want to perform an Insert, Update


or Deleteoperation. Since the return type is Object, we can cast to any
type the object

3. ExecuteScalar–Use when the query returns a single(Scalar) value. For


example, queries that return the total number of rows in a table.

Event driving Programming(C#) by Dagne W. 16


SqlCommand object –Methods…

Event driving Programming(C#) by Dagne W. 17


SqlCommand -Example

Event driving Programming(C#) by Dagne W. 18


SqlCommand –Example…

Event driving Programming(C#) by Dagne W. 19


SQL Injection Prevention…

Event driving Programming(C#) by Dagne W. 20


SQL Injection Prevention
 SQL Injection Prevention: Preventing SQL injection using
Parameterized queries and Stored procedures.

 How to execute stored procedures and parameterized


queries using ADO.NET command object. Using
Parameterize queries:

Event driving Programming(C#) by Dagne W. 21


SQL Injection Prevention…

Event driving Programming(C#) by Dagne W. 22


Sql data reader
 Sql data reader reads data in the most efficient manner possible.
 SqlDataReader is read-only and forward-only, meaning once you read a record
and to the next record, there is no way to go back to the previous record.

 The forward-only nature of SqlDataReader is what make it an efficient choice


to read data.

 It is also not possible to change the data using SqlDataReader.

 SqlDataReaderis connection oriented, meaning it requires an active


connection to the data source, while reading data.

 instance of SqlDataReader cannot be created using the new operator•

 The SqlCommand object’s Executereader()method creates and returns an


instance of SqlDataReader.

Event driving Programming(C#) by Dagne W. 23


Sql Data reader…

Event driving Programming(C#) by Dagne W. 24


SQL data reader-read()

Event driving Programming(C#) by Dagne W. 25


SQL data reader-read()…

Event driving Programming(C#) by Dagne W. 26


SqlDataReader object’s NextResut() Method

 Retrieving two or more result sets using the SqlDataReader


Object’s NextResult() method.

 To loop thru records with in a result-set use the Read()


method.

 To loop thru between result-sets with in the


SqlDataReader use the NextResult() method.

 Example: Consider the following SQL query which returns


two Result Sets Select * From tblStudent; Select * From
tblDepartment
Event driving Programming(C#) by Dagne W. 27
SqlDataAdapter and DataSet
SqlDataAdapterand DataSetin ADO.NET

• Recall SqlDataReader is connection oriented, meaning it requires an


active and open connection to the data source.

• SqlDataAdapter and DataSet provides us with disconnected data access


model.

• When we create an instance of SqlCommand object, we pass in the


following 2 parameters to the constructor of the SqlCommand class. 1.
The Command we want to execute 2. The Connectionon which we want
to execute the Command Along same lines, when creating an instance
of the SqlDataAdapter, we specify 1. The SqlCommand that we want to
execute 2. The Connectionon which we want to execute the Command
Event driving Programming(C#) by Dagne W. 28
SQL data adapter Example

Event driving Programming(C#) by Dagne W. 29


DataSet
 DataSet is Provider independent;it’s present within the namespace
System.Data;
 A DataSet is an in-memory representation of a database.
 (Database is stored in Hard disk whereas Dataset stored in memory of
the web server)
 The Fill() method of the DataAdapter object will take care all about
 Opening the connection the Database
 Executing the sqlcommand on the Database
 Reads the data and fills that within the DataSet
 Closing the connection immediately
 DataAdapter object can do all DML operations.
 Can Update, Delete, Insert data

Event driving Programming(C#) by Dagne W. 30


Data set
.

Event driving Programming(C#) by Dagne W. 31


.

Thank you!!!

Event driving Programming(C#) by Dagne W. 32

You might also like