+ All Categories
Home > Documents > ADO .NET

ADO .NET

Date post: 03-Nov-2015
Category:
Upload: shivuhc
View: 213 times
Download: 0 times
Share this document with a friend
Description:
NET
14
ADO .NET
Transcript
  • ADO .NET

  • ADO.NET is the new database technology of the .NET (Dot Net) platform, and it builds on Microsoft ActiveX Data Objects (ADO).ADO is a language-neutral object model that is the keystone of Microsoft's Universal Data Access strategy.ADO.NET is an integral part of the .NET Compact Framework, providing access to relational data, XML documents, and application data. ADO.NET defines DataSet and DataTable objects which are optimized for moving disconnected sets of data across intranets and Internets, including through firewalls. It also includes the traditional Connection and Command objects, as well as an object called a DataReader that resembles a forward-only, read-only ADO recordset. 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)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 The Command objectThe DataReader objectThe DataAdapter objectDataSet object represents a disconnected cache of data which is made up of DataTables and DataRelations that represent the result of the commandADO.NETObject ModelDataSourceData Provider

    ConnectionDataAdapterCommandDataReaderDataSet

  • Connection ObjectIn ADO .NET, use the connection object toPrepare the ConnectionStringCall Open() on the connectionConnection StringString of Key - Value pairs delimited by semicolonThe components of connection string are as followsData Source: Used by the connection object to identify the data source and to establish the connection. Name or network address of the instanceIntegrated Security or Trusted_Connection: indicates the current windows account credentials that are used for authenticationDatabase or initial catalog: The name of database to establish connectionUser ID or uid: Indicates the name of the userPassword or pwd: Verifies the password settings

  • Connection AuthenticationThe Connection string is used to specify the connection authenticationThe two forms of authentication are as followsSQL ServerIntegrated windowsSQL Server authentication: Specifies user id and password in the connection stringE.g. Data Source=SomeMachine;Initial Catalog=pubs;uid=sa;pwd=***Integrated windows authentication: Uses windows login credentialsE.g. Data Source=SomeMachine;Database=pubs;Integrated Security=TrueDisconnectingIt is mandatory to close the connection after its useUsing Close() method of the connection object

  • Command ObjectThe command object is the core of ADO .NET data processing. It can be used to process the SELECT, INSERT, UPDATE and DELETE statements. These commands are executed after opening a connection to the databaseThe types of commands are as followsExecuteReader: Returns a DataReader object as a stream of dataExecuteScalar: Returns a singleton valueExecuteNonQuery: Returns the number of rows affected by the SQL statementsCommandsExecuteReaderExecuteScalarExecuteNonQuery

  • DataReaderDataReader is also called a firehose cursor. It does not have a public constructor. Only the classes in the same assembly (System.Data) can access the constructor.The features of the DataReader are as followsRetrieves a read-only and forward-only stream of dataCreates command objects by using ExecuteReader methodRepresents a live connectionStores only one row at a time in memoryResults are stored in the network buffer of the clientUses NextResult method for multiple result setsTo access the column values:Use column ordinals myReader[0],myReader[1], and so onUse column names myReader[EmpName]The purpose of calling the Close() method is toFill the output and return parametersSet the RecordsAffected property of the DataReader

  • DataAdapterDataAdapters are used to communicate between the data source and DataSetThe DataAdapter useConnection object to connect a data sourceCommand object to retrieve and update dataThe Fill method to populate data to the DataSetThe Update method help to send the changes to the database

    E.g. Reading the DataSet

    E.g. Mutiple Recordsets

  • DataSetAn in-memory representation of data provides a consistent relational programming model by using the DataSet, which is independent of the data source.A DataSet, a resident of the System.Data namespace, is most precisely defined as a provider-neutral, in-memory, and disconnected relational data structure. It provides support for the standard view, add, remove, and update data operations for the data it represents, and it isnt limited only to database data. The DataSet is used to:Track the changes made to its dataStore the relations between multiple tables by using the objects such as DataTable and DataRelationCollectionRead and Write Extensible Markup Language (XML) files

  • DataSet Object ModelDataSet is composite of several components, includes:

    DataSetDataRelationCollectionDataTableCollectionDataTableDataRowCollectionDataRowDataViewConstraintsPrimaryKeyDataColumnCollectionDataColumn

  • DataViewA DataView provides a customized view of a single DataTableE.g. DataView empView = new DataView(empDS.Tables[EmpInfo])The features of DataView are as followsAllows multiple views for a single DataTableSupports data binding on Win Forms and Web FormsAccesses the DataTable through the DataTable.DefaultView propertyControls the modifications by the AllowDelete, AllowNew, and AllowEdit propertiesSort PropertyThe sort property takes a single or multiple columns with the ASC or DESCE.g. empView.Sort = EmpID,EmpName ASC;Allow DefaultSort propertyThe AllowDefalutSort sorts the column in ascending order on the primary key

  • DataView (Contd.)Searching PropertyThe Search criteria passed to the methods is based on sort keys.

    FilteringThe expressions with the WHERE clause are assigned to the filter expression

  • DataRelationDataRelation defines the relationship between two different Data Table objects. It relates Data Tables through DataColumnsE.g. DataColumn parentCol; DataColumn childCol; childCol = empDeptDS.Tables[EmpInfo].Columns[Deptno]; parentCol = empDeptDS.Tables[DeptInfo].Columns[Deptno]; DataRelation empdeptRel = new DataRelation(EmpDept,parentCol,childCol); empdeptDS.Relations.Add(empdeptRel);Navigating RelationshipDataRelation allows navigation from one DataTable to another. It retrieves rows of related tables by using DataRow.GetChildRows or DataRow.GetParentRows

  • THANK YOU


Recommended