68
ADO.NET Tools and Wizards

ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Embed Size (px)

Citation preview

Page 1: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

ADO.NET Tools and Wizards

Page 2: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 2

Data Sources Window (Introduction) Use the Data Sources window to

Establish a connection Create bound control instances on a form

Page 3: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 3

Configuring a Data Source (Steps 1) Display the Data Sources window

Click to begin creating a new data source

Page 4: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 4

Configuring a Data Source (Steps 2)

Connect to adatabase

Page 5: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 5

Configuring a Data Source (Steps 3)

Create a new connection or select an existing one

Page 6: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 6

Configuring a Data Source (Steps 4)

Select the correct data source

Page 7: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 7

Configuring a Data Source (Steps 5)

Select the database filename

Page 8: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 8

Configuring a Data Source (Steps 6)

Configured connection

Page 9: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 9

Configuring a Data Source (Steps 7)

Use local database copy

Page 10: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 10

Configuring a Data Source (Steps 8)

Save the connection string

Page 11: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 11

Configuring a Data Source (Steps 9)

Select the objects to include

Page 12: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 12

Configuring a Data Source (Steps 10)

Configured data source

Page 13: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 13

Creating Bound Control Instances (Introduction) Bound control instances are created by

dragging a field (or table) from the Data Sources window to the form

Page 14: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 14

Creating Bound Control Instances (Illustration)

Page 15: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 15

Actions Performed (Introduction) Visual Studio performs a vast amount of

work behind the scenes The BindingSource was created A BindingNavigator was created

A DataSet was generated Code to populate the DataSet

The TableAdapter was created A Connection was generated

Page 16: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 16

The BindingSource (Introduction) The BindingSource was new to .NET 2.0 One purpose is to provide currency (current

record) management DataSets and DataTables do not provide

currency management It’s methods are used to locate a record in a

particular DataTable The BindingSource can also be used to add,

change, and delete list items Changes are propagated to the underlying data

source (DataSet and DataTable)

Page 17: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 17

The BindingSource (Properties) Two properties are significant The DataSource property contains the

name of the DataSet The DataMember property contains the

name of a database table from the DataSet

Page 18: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 18

The BindingSource (Illustration)

Page 19: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 19

The BindingSource Class (Members) MoveFirst, MovePrevious, MoveNext, MoveLast methods perform navigation

Current gets the current item from the database table

Page 20: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 20

The BindingNavigator (Introduction) The BindingNavigator provides a user

interface for the BindingSource Control instances are typically bound to

the navigator items

Page 21: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 21

The BindingNavigator (Properties)

Page 22: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 22

The BindingNavigator (Using) Set the BindingSource property to a

previously configured BindingSource Set the value of the various items

Page 23: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 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’s used by the BindingSource It can be accessed programmatically

Page 24: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 24

What’s in a DataSet

Page 25: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 25

Working with the Schema Definition The editor shown in the preceding

figure is used to work with Schema Definition files It is initially configured using the

underlying database and database relationships

It’s possible to add queries, relationships and other items We won't do that here

Page 26: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 26

DataSet Visual Studio created a Schema

Definition File This XML document describes the tables in

the DataSet and their relationships Visual Studio created a strongly typed DataSet class

An instance of the DataSet class was also created

Page 27: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 27

DataSet Class (Types) DataSets are either typed or untyped Untyped DataSet

Reference tables and fields through their corresponding collections

Typed DataSet Typed DataSets are derived from the System.Data.DataSet class

Contains additional properties and methods that allow references to tables and fields directly by name

Page 28: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 28

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

Page 29: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 29

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

Page 30: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 30

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

Page 31: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 31

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

Page 32: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 32

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

Page 33: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 33

DataTable (NewRow Method) The NewRow method creates a new DataRow

object Method uses schema information obtained from

the DataTable itself Calling the NewRow method creates a new DataRow

object DataRow contains the same number of DataColumn objects as exist in the underlying DataTable

Page 34: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 34

DataTable (Events) ColumnChanging event fires just before a new

value is stored in a particular column ColumnChanged event fires after a value is

stored in a column, ColumnChanged event fires after the ColumnChanging event

RowChanging event fires before a new value is stored in a row

RowChanged event fires after a new value has been stored in a row

Page 35: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 35

DataSet, DataTable, DataColumn Relationships

Page 36: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 36

DataTable (Example)

Reference a DataTable using a numeric index and string key

DataTable dtCustomers;

dtCustomers = dsCurrent.Tables[0];

dtCustomers = dsCurrent.Tables["tblNames"];

Page 37: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 37

DataRowCollection Contains the collection of rows in the DataTable Properties Count property gets the number of rows in

the collection (records in the DataTable) Count property is 1-based

Item property (indexer) returns a row having the specified index

Index is 0-based

Page 38: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 38

DataRowCollection Methods Add method adds a new DataRow to the end of the Rows

collection Clear method removes all of the DataRow objects from

the Rows collection Removes all of the rows from the underlying DataTable

Find method locates a single DataRow in the Rows collection

Remove method removes a DataRow in the Rows collection

RemoveAt method has the same purpose as the Remove method

Accepts the numeric index of the row to remove as its one argument

Page 39: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 39

DataTable and Rows Relationship

Page 40: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 40

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

Page 41: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 41

Navigating the DataTable

Page 42: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 42

Modifying Data in a DataTable Modifying the records in a DataSet and

its DataTable(s) and recording those changes back to the database is a two-step process First, add, change, and delete records in

the DataTable Second, record those changes back to the

database using the Update method of the OleDbDataAdapter

Page 43: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 43

Adding a Row to a DataTable Create a new DataRow object by calling the NewRow method of the DataTable class

The NewRow method creates a new DataRow object with the same schema as the underlying DataTable

Call the Add method of the Rows collection to add the row to the DataTable

Page 44: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 44

Adding a Row to a DataTable Example

DataRow drCurrent;

drCurrent = dtCustomers.NewRow();

drCurrent["fldCustomerNumber"] = 99;

drCurrent["fldFirstName"] = "Joe";

drCurrent["fldLastName"] = "Bob";

dtCustomers.Rows.Add(drCurrent);

Page 45: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 45

Modifying a DataRow Using an index to the Rows collection,

reference a field within that row Example

Reference the field named fldID in the first row of the DataTable named dtNames

dtCustomers.Rows[0]["fldLastName"] = "Marty";

Page 46: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 46

Deleting a Row from a DataTable To delete a row from a DataTable, call

the Delete method of the DataRow class Example 1

dtCustomers.Rows[0].Delete();

Example 2DataRow drCurrent;

drCurrent = dtCustomers.Rows[0];

drCurrent.Delete();

Page 47: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 47

The Strongly Typed DataSet Class (Introduction)

The code for the DataSet appears here

Page 48: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 48

Benefits of Strongly Typed DataSets A strongly typed DataSet is one that derives

from the System.Data.DataSet class It contains additional properties and methods to

reference the tables and fields in that DataSet Reduces coding complexity Property and method names based on the names

of the underlying database tables and fields Typed DataSets prevent runtime type

conversion errors

Page 49: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 49

Elements of a Typed DataSet (1) A typed DataSet is merely a

custom DataSet that derives from the System.Data.DataSet class This class is automatically

generated when the DataSet is generated

Do not modify this file directly

Page 50: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 50

Importance of Typed DataSets Exposes properties and methods allowing

access to each DataTable, its rows, and the fields in those rows by name

Properties and methods have data types corresponding to the data type of the underlying table, row, and field

Page 51: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 51

The TableAdapter The TableAdapter is part of the

strongly typed DataSet Is used to simplify the processing of the OleDbDataAdapter

It allows dynamic queries to be processed

Page 52: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 52

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

Page 53: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 53

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

Page 54: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 54

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

Page 55: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 55

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

Page 56: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 56

OleDbDataAdapter Class

Page 57: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 57

OleDbCommand Class Stores SQL statements

These SQL statements are used by OleDbDataAdapter to select and update rows

OleDbDataAdapter executes command automatically

Page 58: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 58

OleDbCommand Class (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 For each parameter in the Parameters collection,

the OleDbDataAdapter dynamically fills in the data that is used to add, change, or delete a database record

Page 59: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 59

OleDbCommand Class (Methods) ExecuteNonQuery method executes a SQL

statement Statement does not return database rows

ExecuteReader method creates a DataReader 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

Page 60: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 60

OleDbCommand Class Example Example

Private modbcmdSelect As New OleDbCommand()

modbcmdSelect.CommandText = "SELECT * FROM tblNames"

modbcmdSelect.Connection = modbconCurrent

First statement creates an instance of the OleDbCommand class

Second statement stores the SQL SELECT statement in CommandText property

Finally, the Connection property is set to modbconCurrent (an existing connection)

Page 61: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 61

SQL SELECT Statement (1) SQL SELECT statement selects one or

more rows (records) from a database table or query

Syntax

SELECT * | fieldlist FROM tablename WHERE conditionlist ORDER BY fieldlist

Page 62: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 62

SQL SELECT Statement (2) Syntax dissection

fieldlist argument specifies the field(s) to select Commas separate field names

tablename argument contains database table name WHERE conditionlist argument contains a condition

used to restrict the rows selected Resembles the condition in an If statement

ORDER BY fieldlist argument contains a comma-separated list to sort table data

Data may be sorted in ascending or descending order

Page 63: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 63

SQL Select Example Example

SELECT * FROM tblNames ORDER BY fldLastName

SELECT * FROM tblNames ORDER BY fldLastName, fldFirstName

SELECT fldFirstName, fldLastName FROM tblNames

Page 64: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 64

Filling the OleDbDataAdapter (1)

Page 65: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 65

SQL Query Parameters Use parameterized queries to select

specific records Conceptually similar to a procedure

accepting one or more arguments Syntax of parameters varies from provider

to provider

Page 66: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 66

OleDbParameter Class (Properties) Properties

OleDbType property maps a database type to its corresponding .NET Framework type

Size property defines the number of bytes required to store the data

Size is inferred for numeric types Set explicit size for String types

Value property stores the current value of the parameter

Page 67: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 67

OleDbParameter Class (Constructor)

Public Sub New (name As String, datatype As OleDbType, size As Integer, srccolumn As String) name argument contains the name of the

parameter datatype argument contains a value that

defines the data type of the argument size argument contains the maximum size of

the data srccolumn argument contains the name of

the column in the underlying DataTable

Page 68: ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances

Slide 68

Recording DataSetChanges (1) Explicitly record any changes back to

the database using the TableAdapter The following members are used to

record changes back to the database The Update method of the TableAdapter

records the changes made to the DataTable(s) in a DataSet

The RowState property marks whether a particular DataRow was added, changed, or deleted