Download presentation
Presentation is loading. Please wait.
Published byMilo Shepherd Modified over 8 years ago
1
Active Data Objects Using.Net ADO.Net Farooq Ahmed Amna Umber Summayya Shehzad
2
Agenda ADO.Net Overveiw –Name-spaces –Shared Classess –Database Specific classess Database Connectivity –Efficient Connectivity –Methods Commands Data Reader
3
OverView
4
Why ADO.NET? ADO works great, but –Requires Windows ® –Recordsets don’t travel well across the Internet –Connected behavior is hard to work with ADO.NET solves these problems –Uses XML under the covers for all data transport –XML has no runtime/transport requirements No special code required to marshal across the Internet ADO.NET requires new mindset –All data access disconnected –All data transport uses XML
5
What Is ADO.NET? ADO.NET is a collection of classes, interfaces, structures, and enumerated types that manage data access from relational data stores within the.NET Framework.These collections are organized into namespaces: System.Data, System.Data.OleDb ADO.NET is an evolution from ADO. –Does not share the same object model, but shares many of the same paradigms and functionality!
6
ADO.NET Architecture Business Tier Data Tier Presentation Tier Windows Forms Web Forms Business to Business Data Object (Class) DataSet Internet Intranet Data Adapter (BizTalk, for example) XML MyApp.Exe IE
7
Namespace Architechture The ADO.NET Object Model Objects of System.Data.NET data providers ADO.NET namespace hierarchy Organizes the object model Includes: ○ System.Data System.Data.OleDb System.Data.Common System.Data.SqlClient System.Data.SqlTypes
8
ADO.NET ADO.NET-related Namespaces System.Data.OleDb.SqlClient.SqlTypes.Common
9
ADO.NET Application ODBC Driver ODBCRDBMS ODBC OLE DB RDBMS.NET Data Provider Connection Command Data Reader Data Adapter OLE DB OLE DB Provider SQL Server
10
Classes : System.Data Constraint DataColoumn DataException DataRelation DataRow DataRowBuilder DataSet DataTable DataTableCollection DataView Entity key Entity Exception
11
Shared classes(System.data) Shared classes are actually the classes which are used regardless of type of Database engine we are accessing as sql,Oracle(oleDb)or access. –The class hierarchy is as : Constraint dataColoumn dataRelation dataRow dataSet dataTable
12
Database specific classess are included in the system.data.sqlClient &.OleDB Database Specific Classes
13
Database Connectivity
14
Simple Example Connection Establishment –Pass all data in a string –Create a OLeDB/SQL connection/RDBMS connection –Create dataset and data Adapter to get and collect data –Now initialize the data table object(dataRow) to access the data.
15
DEMO
16
Using Database Connections Efficiently We use database connections by using: –Exception handling –Using Block Statement
17
Commands
18
Structure DbCommandOleDbCommand ExecuteReader ExecuteScalar ExeecuteNonQuery SqlCommand ExecuteReader ExecuteScalar ExeecuteNonQuery ExecuteXMLReader OdbcCommand ExecuteReader ExecuteScalar ExeecuteNonQuery OracleCommand ExecuteReader ExecuteScalar ExeecuteNonQuery
19
Command Object Properties CommandType Command Text Parameter Connection
20
Command Type Enumerations: –Text –StoredProcedure –TableDirect
21
Command Text Depends on CommandType Contains a string that represents either the command/query, procedure names or tables.
22
Parameters The Parameters property is a collection of zero or more appropriate DbParameter instances. The use of the Parameters property is only necessary if the CommandText property holds a parameterized query.
23
Parameters(2) SqlParameter parameter = new SqlParameter(); parameter.ParameterName = "@CategoryName"; parameter.SqlDbType = SqlDbType.NVarChar; parameter.Direction = ParameterDirection.Input; parameter.Value = categoryName; command.Parameters.Add(parameter);
24
Connection The Connection property of a command object must be set to an appropriate connection object.
25
Command Objects (2) Used to execute commands to a database across a data connection. Used to execute stored procedures, SQL commands or return complete table directories.
26
ExecuteNonQuery() Executes commands that have no return values, e.g. DDL and DML statements.
27
ExecuteScalar() Returns a single value from database query
28
ExecuteReader() Returns a DataReader object.
29
ExecuteXMLReader() Returns an XMLReader.
30
Stored Procedures Stored Procedures are a set of sql commands which are compiled and are stored inside the database. When executing a sql command, the command is parsed, optimization is done and then the command is executed.
31
Stored Procedures(2) Without a return parameter: –Creating a procedure Create PROCEDURE CreateRow AS INSERT into TABLE2 values (001, ‘Farooq’);
32
Stored Procedures(3) –Calling the procedure SqlConnection SqlConn1 = new SqlConnection(); SqlConn1.ConnectionString = “ ”; SqlCommand SqlComm1 = new SqlCommand(); SqlComm1.Connection = SqlConn1; SqlComm1.CommandType = CommandType.StoredProcedure; SqlComm1.CommandText = "CreateRow"; SqlConn1.Open(); SqlConn1.Close(); Console.ReadLine();
33
Stored Procedures(4) With a return parameter –Creating a procedure CREATE PROCEDURE CountRecords AS SELECT Count(*) from TABLE1
34
Stored Procedures(5) –Calling the procedure SqlConnection SqlConn1 = new SqlConnection(); SqlConn1.ConnectionString = “ ”; SqlCommand SqlComm1 = new SqlCommand(); SqlComm1.Connection = SqlConn1; SqlComm1.CommandType = CommandType.StoredProcedure; SqlComm1.CommandText = "CountRecords"; SqlConn1.Open(); Console.WriteLine("Calling the procedure returns :" + SqlComm1.ExecuteScalar()); SqlConn1.Close(); Console.ReadLine();
35
The Data Reader
36
DataReader Object The DataReader object provides a forward- only, read-only, connected stream recordset from a database. These objects cannot be directly instantiated. The SqlCommand.ExecuteReader method returns a SqlDataReader object. ted.
37
DataReader Object (2) The DataReader can provide rows of data directly to application logic when you do not need to keep the data cached in memory. Requires the exclusive use of an open Connection object for the lifetime of the DataReader.
38
THANK YOU
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.