Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances."— Presentation transcript:

1 ADO.NET Tools and Wizards

2 Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances on a form

3 Slide 3 Configuring a Data Source (Steps 1) Display the Data Sources window Click to begin creating a new data source

4 Slide 4 Configuring a Data Source (Steps 2) Connect to a database

5 Slide 5 Configuring a Data Source (Steps 3) Create a new connection or select an existing one

6 Slide 6 Configuring a Data Source (Steps 4) Select the correct data source

7 Slide 7 Configuring a Data Source (Steps 5) Select the database filename

8 Slide 8 Configuring a Data Source (Steps 6) Configured connection

9 Slide 9 Configuring a Data Source (Steps 7) Use local database copy

10 Slide 10 Configuring a Data Source (Steps 8) Save the connection string

11 Slide 11 Configuring a Data Source (Steps 9) Select the objects to include

12 Slide 12 Configuring a Data Source (Steps 10) Configured data source

13 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

14 Slide 14 Creating Bound Control Instances (Illustration)

15 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

16 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 )

17 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

18 Slide 18 The BindingSource (Illustration)

19 Slide 19 The BindingSource Class (Members) MoveFirst, MovePrevious, MoveNext, MoveLast methods perform navigation Current gets the current item from the database table

20 Slide 20 The BindingNavigator (Introduction) The BindingNavigator provides a user interface for the BindingSource Control instances are typically bound to the navigator items

21 Slide 21 The BindingNavigator (Properties)

22 Slide 22 The BindingNavigator (Using) Set the BindingSource property to a previously configured BindingSource Set the value of the various items

23 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

24 Slide 24 What’s in a DataSet

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 Slide 35 DataSet, DataTable, DataColumn Relationships

36 Slide 36 DataTable (Example) Reference a DataTable using a numeric index and string key DataTable dtCustomers; dtCustomers = dsCurrent.Tables[0]; dtCustomers = dsCurrent.Tables["tblNames"];

37 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

38 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

39 Slide 39 DataTable and Rows Relationship

40 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

41 Slide 41 Navigating the DataTable

42 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

43 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

44 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);

45 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";

46 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 2 DataRow drCurrent; drCurrent = dtCustomers.Rows[0]; drCurrent.Delete();

47 Slide 47 The Strongly Typed DataSet Class (Introduction) The code for the DataSet appears here

48 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

49 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

50 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

51 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

52 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

53 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

54 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

55 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

56 Slide 56 OleDbDataAdapter Class

57 Slide 57 OleDbCommand Class Stores SQL statements These SQL statements are used by OleDbDataAdapter to select and update rows OleDbDataAdapter executes command automatically

58 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

59 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

60 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)

61 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

62 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

63 Slide 63 SQL Select Example Example SELECT * FROM tblNames ORDER BY fldLastName SELECT * FROM tblNames ORDER BY fldLastName, fldFirstName SELECT fldFirstName, fldLastName FROM tblNames

64 Slide 64 Filling the OleDbDataAdapter (1)

65 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

66 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

67 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

68 Slide 68 Recording DataSet Changes (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


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

Similar presentations


Ads by Google