1 / 25

17. Data Access

17. Data Access. ADO.Net Architecture New Features of ADO.NET Visual Studio Visual Studio Wizards and Designers Demonstration: Creating Data Components Lab 17: Accessing Data Using ADO.NET. Code Samples. ADO.NET Architecture. What Is ADO.NET? What Is a Connected Environment?

Download Presentation

17. Data Access

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 17. Data Access • ADO.Net Architecture • New Features of ADO.NET • Visual Studio Visual Studio Wizards and Designers • Demonstration: Creating Data Components • Lab 17: Accessing Data Using ADO.NET Code Samples

  2. ADO.NET Architecture • What Is ADO.NET? • What Is a Connected Environment? • What Is a Disconnected Environment? • What Is the ADO.NET Object Model? • What Is the DataSet Class? • What Is the .NET Data Provider?

  3. What Is ADO.NET? ADO.NET is a data access technology. It provides: • A set of classes, interfaces, structures, and enumerations that manage data access from within the .NET Framework • An evolutionary, more flexible successor to ADO • A system designed for disconnected environments • A programming model with advanced XML support

  4. What Is a Connected Environment? • A connected environment is one in which users are constantly connected to a data source • Advantages: • Environment is easier to secure • Concurrency is more easily controlled • Data is more likely to be current than in other scenarios • Disadvantages: • Must have a constant network connection • Scalability

  5. What Is a Disconnected Environment? • In a disconnected environment, a subset of data from a central data store can be copied and modified independently, and the changes merged back into the central data store • Advantages • You can work at any time that is convenient for you, and can connect to a data source at any time to process requests • Other users can use the connection • A disconnected environment improves the scalability and performance of applications • Disadvantages • Data is not always up to date • Change conflicts can occur and must be resolved

  6. .NET Data Provider DataTableCollection Connection DataAdapter DataTable Transaction SelectCommand DataRowCollection InsertCommand Command DataColumnCollection Parameters UpdateCommand ConstraintCollection DeleteCommand DataReader DataRelationCollection XML Database What Is the ADO.NET Object Model? DataSet

  7. Manages the connection to a database Executes a query command on the database Exchanges data between the data set and the database Provides efficient access to astream of read-only data What Is the .NET Data Provider? Database Connection Command DataAdapter DataReader

  8. Creating an Application That Uses ADO.NET to Access Data • How to Specify the Database Connection • How to Specify the Database Command • How to Create the DataAdapter Object • How to Create a DataSet Object • How to Bind a DataSet to a DataGrid • How to Use the Data Wizards in Visual Studio .NET

  9. How to Specify the Database Connection • Use the Connection object to: • Choose the connection type • Specify the data source • Open the connection to the data source • Use the connection string to specify all of the options for your connection to the database, including the account name, database server, and database name string connectionStr = @"Data Source=localhost; Integrated Security=SSPI; Initial Catalog=northwind";

  10. How to Specify the Database Command • Create a string containing SQL statements • Remember that Verbatim strings can make this much easier! • Examples of SQL statements: • SELECT * FROM Customers • SELECT CustomerName FROM Customers • SELECT * FROM Customers WHERE Country = 'Mexico' string commandStr=@"SELECT CustomerName, CompanyName FROM Customers";

  11. How to Create the DataAdapter Object DataSet Data source DataAdapter DataTable Fill Update DataAdapter DataTable Fill Update

  12. DataSet Tables Table Columns Column Constraints Constraint Rows Row Relations Relation Object Collection What Is the DataSet Class? • DataSets consist of one or more tables and relations • Loaded from one or more data adapters • Created as you work • Loaded from XML • Loaded from other DataSets • Tables contain columns, constraints, and rows • All are collections!

  13. How to Bind a DataSet to a DataGrid To bind programmatically DataGrid dataGrid1 = new DataGrid(); sqlDataAdapter1.Fill(dataSet1, "Customers"); sqlDataAdapter2.Fill(dataSet1, "Orders"); dataGrid1.DataSource = dataSet1;

  14. Changing Database Records • How to Access Data in a DataSet Object • How to Update a Database in ADO.NET • How to Create a Database Record • How to Update a Database Record • How to Delete a Database Record

  15. DataRowobjects How to Access Data in a DataSet Object DataTable objects DataColumnobjects DataColumnobjects

  16. Client Server Database DataSet DataAdapter Fill Data Update InsertCommand UpdateCommand DeleteCommand How to Update a Database in ADO.NET Data DataTable

  17. How to (Manually) Create a Database Record Create a new row that matches the table schema Add the new row to the dataset Update the database DataRow myRow = dataTable.NewRow(); dataTable.Rows.Add( myRow ); sqlDataAdapter1.Update( dataSet );

  18. How to (Manually) Delete a Database Record • Delete the row from the dataset • Update the database • Accept the changes to the dataset dataTable.Rows[0].Delete(); dataAdapter.Update(dataSet); dataSet.AcceptChanges();

  19. Data Reader • Data Readers manage the stream of results from a SELECT statement or Stored Procedure • Data Readers are read only • Data Readers only move forwards through the data – they cannot go backwards • No other database operation can be performed while a data reader is open

  20. Using a DataReader with a SELECT Statement string queryString = "SELECT FirstName, LastName FROM dbo.Employees"; //Create the command objects SqlCommand com = new SqlCommand(queryString, conn); //Call the command's ExecuteReader method SqlDataReader dr = com.ExecuteReader(); //Loop through the datareader to output the employee names while (dr.Read()) { listBox1.Items.Add(dr.GetString(0) + " " + dr.GetString(1)); }

  21. Using a DataReader to call a Stored Procedure //Create the command object and set its properties SqlCommand com = new SqlCommand(); com.Connection = conn; com.CommandText = "CustOrderHist"; com.CommandType = CommandType.StoredProcedure; //Create the parameter object and add it to the command's collection SqlParameter param = new SqlParameter("@CustomerID", SqlDbType.NVarChar, 5); param.Value = txtID.Text; com.Parameters.Add(param);

  22. Creating a Two Tier Application • Create the data tier • Create a new class library project • Create a class to represent a business entity, say customers • Add methods that access the database, using ADO.Net • Add properties that expose attributes, say name, phone … • Create the presentation tier • Create a new Windows Application project • Add a reference to the above data project • Create an object from class • Use the object’s properties to populate controls on a form • Use the object’s methods to access the database

  23. New Features of ADO.NET 2.0 • Multiple active result sets • One connection can host more than one result set • Reduces number of connections on a database • Asynchronous operations • Begin a data access operation, continue with other tasks, and then later end the original operation • Batch updates • Update multiple rows in one batch • Use the UpdateBatchSize property of the DataAdapter

  24. Visual Studio Wizards and Designers • Data Source Configuration Wizard • TableAdapter Configuration Wizard • TableAdapter Query Configuration Wizard • Preview Data Dialog Box

  25. Lab 17: Accessing Data Using ADO.NET • Exercise 1: Retrieving Data into a Data Set • Exercise 2:Updating the Database from a Data Set • Exercise 3: Retrieving Data with a Data Reader • Exercise 4: Calling a Stored Procedure

More Related