Presentation is loading. Please wait.

Presentation is loading. Please wait.

ADO.NET Framework.

Similar presentations


Presentation on theme: "ADO.NET Framework."— Presentation transcript:

1 ADO.NET Framework

2 Lecture Overview Creating connections to providing
Executing SQL commands using providers that Return nothing Return one value Read data one record at a time Use DataAdapters to create in-memory datasets and datatables

3 Providers Remember that ADO has different providers for each type of database Each provider contains the same full set of classes and objects to work with the underlying DB OleDb (System.Data.OleDb) Oracle (System.Data.OracleClient) SQL Server (System.Data.SqlCleint)

4 Connections (Introduction)
The OleDbConnection and SQLConnection classes give us the means to make a database connection Commands are sent over this connection The ConnectionString property contains the string that the systems uses to make the database connection It differs from provider to provider

5 Connections (Using) Call the Open and Close methods to explicitly open and close the connections Use for OleDbCommands Some objects call these methods for you

6 Connection (Example)

7 Using a Connection (Introduction)
Once we have created a connection, we send commands over that connection Most commonly using the OleDbCommand or SqlCommand objects There are properties to configure the command and connection There are methods to execute the command using the connection

8 OleDbCommand (Properties)
CommandText property stores SQL statement that will be executed against a provider Connection property stores a reference to existing OleDbConnection object Parameters property stores a reference to a Parameters collection (more later)

9 OleDbCommand (Methods)
ExecuteScalar method executes a SELECT statement Method returns the first column of the first row retrieved by the SELECT statement Additional data is discarded and no exception will occur ExecuteNonQuery method executes an SQL statement Statement does not return database rows ExecuteReader method creates a DataReader We will go through each one of these next

10 Using the OleDbCommand Object (ExecuteScalar)
The ExecuteScalar method returns a single value (first column / first row) Will not fail if you return multiple rows Use to get a max ID value

11 ExecuteScalar (Example)

12 Using the OleDbCommand Object (ExecuteNonQuery)
Use to execute INSERT, UPDATE, or DELETE commands that do not return rows

13 The DataReader (Introduction)
It’s a forward-only reader (like a stream) You read one record at a time Each time a record is read, the reader contains a reference to an array of fields

14 Using the OleDbDataReader
Place an SQL SELECT statement in the OleDbcommand Call ExecuteReader to create the forward-only reader Call Read to read each record Then call the various Get methods to get the value of each field

15 The DataReader (Example)

16 Returning a DataSet This gets a bit more complicated
We need another object called a DataAdapter Microsoft calls it a bridge between a DataSet and a DataSource Again, there are OleDb and Sql Server versions of these I’ll go through the OleDbDataAdapter and DataSet, in turn

17 OleDbDataAdapter Class (Introduction)
OleDbDataAdapter works in conjunction with the DataSet class to read and write data Commands are sent over a connection Data is retrieved into a DataSet Commands to insert, update, or delete data return nothing

18 OleDbDataAdapter (Properties)
SelectCommand property stores SQL SELECT statement InsertCommand property stores SQL INSERT statement that will insert a row into a database table UpdateCommand property stores a SQL UPDATE statement to update the contents of an existing row DeleteCommand property stores a SQL DELETE statement that will remove a row or rows

19 OleDbDataAdapter (Methods)
Fill method uses an OleDbConnection to retrieve database records Records are loaded into a DataSet Update method examines the DataSet for any added, changed, or deleted records Changes are recorded to DataSet Call AcceptChanges method on DataSet to synchronize DataSet and database

20 OleDbDataAdapter (Events)
FillError event fires if an error occurs while populating the DataSet RowUpdating event fires just before row is updated RowUpdated event fires just after a row in the data source is updated

21 OleDbDataAdapter Class

22 Filling the OleDbDataAdapter (1)

23 Definition (DataSet) It’s an in-memory representation of one or more tables in a database It’s created by a data adapter It can be accessed programmatically It contains one or more DataTable objects

24 DataSet Properties Boolean CaseSensitive property controls whether String comparisons are made in a case sensitive or case insensitive way DataSetName property defines the name of the DataSet EnforceConstraints property defines whether ADO.NET checks constraint rules when adding, changing, or deleting data Tables property returns the collection of DataTable objects in the DataSet

25 DataSet Methods (1) AcceptChanges method causes any changed records to be marked as unchanged records Use to synchronize database with DataSet Clear method removes all rows from all tables in the DataSet Clone method makes a copy of the DataSet schema (structure) Data is not copied Copy method copies both the schema and the data from a DataSet Creates an exact copy of the original DataSet

26 DataSet Methods (2) GetChanges method (overloaded) examines an existing DataSet and returns a new DataSet containing only the changed records Returns null if DataSet does not contain changes HasChanges method returns a Boolean value indicating whether the DataSet has pending changes RejectChanges method causes any pending changes to be cancelled, restoring any data to their original values

27 DataTable Class (Introduction)
A DataSet object typically contains one or more DataTable objects It’s basically a database table DataTable object contains one or more DataColumn objects representing each column

28 DataTable (Properties)
CaseSensitive property of the DataTable has the same value as the CaseSensitive property of the parent DataSet Columns property contains a reference to a DataColumnCollection Use to reference each column HasErrors property (Boolean) is set to True if any of the rows contain errors PrimaryKey property contains an array of DataColumn objects Rows property is a collection having a data type of DataRowCollection

29 DataRow Class Each row in the Rows collection has a data type of DataRow Item property gets or sets the value for a particular column in a DataRow Item property accepts one argument RowState property indicates whether the DataRow is a new row, a deleted row, or a row whose contents have been modified Delete method of the DataRow class marks the row for deletion Row is not deleted until the AcceptChanges method of the DataSet or DataTable is called

30 DataTable and Rows Relationship

31


Download ppt "ADO.NET Framework."

Similar presentations


Ads by Google