2310 b 10


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

2310 b 10

  1. 1. Module 10: Accessing Data with Microsoft ADO.NET
  2. 2. Overview <ul><li>Introduction to Using ADO.NET </li></ul><ul><li>Connecting to a Database </li></ul><ul><li>Accessing Data with DataSets </li></ul><ul><li>Using Multiple Tables </li></ul><ul><li>Accessing Data with DataReaders </li></ul>
  3. 3. Lesson: Introduction to Using ADO.NET <ul><li>Multimedia: The ADO.NET Object Model </li></ul><ul><li>Using DataSets vs. DataReaders </li></ul><ul><li>Practice: When to Use DataSets or DataReaders </li></ul>
  4. 4. Multimedia: The ADO.NET Object Model
  5. 5. Using DataSets vs. DataReaders Supported by Visual Studio .NET tools Slower access Forward-only Bind to one control only Based on one SQL statement from one database Read-only Manually coded Faster access Forward and backward scanning of data Bind to multiple controls Includes multiple tables from different databases Read/write access to data DataReader DataSet Disconnected Connected
  6. 6. Practice: When to Use DataSets or DataReaders <ul><li>Students will: </li></ul><ul><ul><li>Select the best data access choice for given scenarios </li></ul></ul><ul><li>Time: 5 Minutes </li></ul>
  7. 7. Lesson: Connecting to a Database <ul><li>SQL Server Security </li></ul><ul><li>Creating the Connection </li></ul><ul><li>Demonstration: Setting SQL Server Security </li></ul>
  8. 8. SQL Server Security Client Send the username and password in clear text. Do not send the username and password. Just send that the user has been authenticated. Mixed mode authentication Windows only authentication SQL Server Only ASPNET account is granted access Web Server Windows authentication or… SQL Server Each user account added to SQL Server logins group Web Server Default ASP.NET settings Here is the username and password
  9. 9. Creating the Connection <ul><li>Using SqlConnection </li></ul><ul><li>Setting connection string parameters </li></ul><ul><ul><li>Connection timeout </li></ul></ul><ul><ul><li>Data source </li></ul></ul><ul><ul><li>Initial catalog </li></ul></ul><ul><ul><li>Integrated security </li></ul></ul>Dim strConn As String = &quot;data source=localhost; &quot; & _ &quot;initial catalog=northwind; integrated security=true&quot; Dim conn As New SqlConnection(strConn) <ul><ul><li>Password </li></ul></ul><ul><ul><li>Persist security info </li></ul></ul><ul><ul><li>Provider </li></ul></ul><ul><ul><li>User ID </li></ul></ul>string strConn = &quot;data source=localhost; &quot; + &quot;initial catalog=northwind; integrated security=true&quot;; SqlConnection conn = new SqlConnection(strConn);
  10. 10. Demonstration: Setting SQL Server Security <ul><li>Open SQL Server Enterprise Manager </li></ul><ul><li>Set authentication mode </li></ul><ul><li>Test with integrated security </li></ul><ul><li>Test with mixed mode security </li></ul>
  11. 11. Lesson: Accessing Data with DataSets <ul><li>Creating a DataAdapter </li></ul><ul><li>Creating a DataSet </li></ul><ul><li>Demonstration: Programmatically Using a DataSet </li></ul><ul><li>Using a DataView </li></ul><ul><li>Practice: Organizing Code to Create a DataSet </li></ul><ul><li>Binding a DataSet to a List-Bound Control </li></ul><ul><li>Instructor-Led Practice: Displaying a DataSet </li></ul><ul><li>Handling Errors </li></ul>
  12. 12. Creating a DataAdapter <ul><li>Store the query in a DataAdapter </li></ul><ul><li>The DataAdapter constructor sets the SelectCommand property </li></ul><ul><li>Set the InsertCommand, UpdateCommand, and DeleteCommand properties if needed </li></ul>Dim da As New SqlDataAdapter _ (&quot;select * from Authors&quot;, conn) da.SelectCommand.CommandText da.SelectCommand.Connection SqlDataAdapter da = new SqlDataAdapter (&quot;select * from Authors&quot;,conn); da.SelectCommand.CommandText; da.SelectCommand.Connection;
  13. 13. Creating a DataSet <ul><li>Create and populate a DataSet with DataTables </li></ul><ul><ul><li>Fill method executes the SelectCommand </li></ul></ul><ul><li>Access a DataTable </li></ul>Dim ds As New DataSet() da.Fill(ds, &quot;Authors&quot;) Dim r As DataRow Dim str As String For Each r in _ ds.Tables(&quot;Authors&quot;).Rows str &= r(2) str &= r(&quot;au_lname&quot;) Next ds.Tables(&quot;Authors&quot;).Rows.Count DataSet ds = new DataSet(); da.Fill(ds, &quot;Authors&quot;); ds.Tables[&quot;Authors&quot;].Rows.Count; string str=&quot;&quot;; foreach(DataRow r in ds.Tables[&quot;Authors&quot;].Rows) { str += r[2]; str += r[&quot;au_lname&quot;]; }
  14. 14. Demonstration: Programmatically Using a DataSet <ul><li>Create a Connection </li></ul><ul><li>Create DataAdapter </li></ul><ul><li>Create DataSet </li></ul><ul><li>Read data from DataSet programmatically </li></ul>
  15. 15. Using a DataView <ul><li>A DataView can be customized to present a subset of data from a DataTable </li></ul><ul><li>The DefaultView property returns the default DataView of the table </li></ul><ul><li>Setting up a different view of a DataSet </li></ul>DataView dv = new DataView(ds.Tables[&quot;Authors&quot;]); dv.RowFilter = &quot;state = 'CA'&quot;; Dim dv As DataView = ds.Tables(&quot;Authors&quot;).DefaultView Dim dv As New DataView (ds.Tables(&quot;Authors&quot;)) dv.RowFilter = &quot;state = 'CA'&quot; DataView dv = ds.Tables[&quot;Authors&quot;].DefaultView;
  16. 16. Practice: Organizing Code to Create a DataSet <ul><li>Student will: </li></ul><ul><ul><li>Reorder lines of code to create a DataSet </li></ul></ul><ul><li>Time: 5 Minutes </li></ul>
  17. 17. Binding a DataSet to a List-Bound Control <ul><li>Create the control </li></ul><ul><li>Bind to a DataSet or a DataView </li></ul>dg.DataSource = ds dg.DataMember = &quot;Authors&quot; dg.DataBind() <asp:DataGrid id=&quot;dg&quot; runat=&quot;server&quot; /> dg.DataSource = ds; dg.DataMember = &quot;Authors&quot;; dg.DataBind() ;
  18. 18. Instructor-Led Practice: Displaying a DataSet <ul><li>Create a Connection </li></ul><ul><li>Create a DataAdapter </li></ul><ul><li>Create a DataSet </li></ul><ul><li>Create a DataView </li></ul><ul><li>Bind both the DataSet and DataView to DataGrid controls </li></ul>
  19. 19. Handling Errors <ul><li>Connection will not open </li></ul><ul><ul><li>Connection string is invalid </li></ul></ul><ul><ul><li>Server or database not found </li></ul></ul><ul><ul><li>Login failed </li></ul></ul><ul><li>DataAdapter cannot create a DataSet </li></ul><ul><ul><li>Invalid SQL syntax </li></ul></ul><ul><ul><li>Invalid table or field name </li></ul></ul>Code Example
  20. 20. Lesson: Using Multiple Tables <ul><li>Storing Multiple Tables </li></ul><ul><li>Creating Relationships </li></ul><ul><li>Programmatically Navigating Between Tables Using Relationships </li></ul><ul><li>Visually Navigating Between Tables Using Relationships </li></ul><ul><li>Instructor-Led Practice: Displaying Data from Multiple Tables </li></ul>
  21. 21. Storing Multiple Tables <ul><li>Add the first table </li></ul><ul><li>Add the subsequent table(s) </li></ul>daCustomers = New SqlDataAdapter _ (&quot;select * from Customers&quot;, conn1) daCustomers.Fill(ds, &quot;Customers&quot;) Orders Customers daOrders = New SqlDataAdapter _ (&quot;select * from Orders&quot;, conn2) daOrders.Fill(ds, &quot;Orders&quot;) conn2 conn1 DataSet
  22. 22. Creating Relationships <ul><li>Identify parent column </li></ul><ul><li>Identify child column </li></ul><ul><li>Create DataRelation </li></ul>Dim dr As New DataRelation _ (&quot;name&quot;, parentCol, _ childCol) ds.DataRelations.Add(dr) Dim parentCol As DataColumn = _ ds.Tables(&quot;Customers&quot;).Columns(&quot;CustomerID&quot;) Dim childCol As DataColumn = _ ds.Tables(&quot;Orders&quot;).Columns(&quot;CustomerID&quot;) Orders table Customers table DataSet parentCol childCol DataRelation C# Code Example
  23. 23. Programmatically Navigating Between Tables Using Relationships ds.Tables( index ).Rows( index ).GetChildRows(&quot; relation &quot;) ds.Tables( index ).Rows( index ).GetParentRow(&quot; relation &quot;) Customers Orders GetChildRows GetParentRow DataSet ds.Tables[ index ].Rows[ index] .GetChildRows(&quot; relation &quot;); ds.Tables[ index] .Rows[ index] .GetParentRow(&quot; relation &quot;);
  24. 24. Visually Navigating Between Tables Using Relationships Dim tableView As DataView Dim currentRowView As DataRowView tableView = New DataView(ds.Tables(&quot;Customers&quot;)) currentRowView = tableView(dgCustomers.SelectedIndex) dgChild.DataSource = currentRowView.CreateChildView(&quot;CustOrders&quot;) Customers Orders CreateChildView DataRowView DataView DataSet DataView tableView; DataRowView currentRowView; tableView = new DataView(ds.Tables[&quot;Customers&quot;]); currentRowView = tableView[dgCustomers.SelectedIndex]; dgChild.DataSource = currentRowView.CreateChildView(&quot;CustOrders&quot;);
  25. 25. Instructor-Led Practice: Displaying Data from Multiple Tables <ul><li>Programmatically: </li></ul><ul><ul><li>Create a DataSet </li></ul></ul><ul><ul><li>Create a DataRelation </li></ul></ul><ul><ul><li>Display child records using the DataRelation </li></ul></ul><ul><li>Visually: </li></ul><ul><ul><li>Call CreateChildView </li></ul></ul>
  26. 26. Lesson: Accessing Data with DataReaders <ul><li>What is a DataReader? </li></ul><ul><li>Creating a DataReader </li></ul><ul><li>Reading Data from a DataReader </li></ul><ul><li>Binding a DataReader to a List-Bound Control </li></ul><ul><li>Practice: Organizing Code to Create a DataReader </li></ul><ul><li>Demonstration: Displaying Data Using DataReaders </li></ul>
  27. 27. What is a DataReader? <ul><li>Forward-only, read-only </li></ul><ul><li>Fast access to data </li></ul><ul><li>Connected to a data source </li></ul><ul><li>Manage the connection yourself </li></ul><ul><li>Manage the data yourself, or bind it to a list-bound control </li></ul><ul><li>Uses fewer server resources </li></ul>
  28. 28. Creating a DataReader <ul><li>To use a DataReader: </li></ul><ul><ul><li>Create and open the database connection </li></ul></ul><ul><ul><li>Create a Command object </li></ul></ul><ul><ul><li>Create a DataReader from the Command object </li></ul></ul><ul><ul><li>Call the ExecuteReader method </li></ul></ul><ul><ul><li>Use the DataReader object </li></ul></ul><ul><ul><li>Close the DataReader object </li></ul></ul><ul><ul><li>Close the Connection object </li></ul></ul><ul><li>Use Try…Catch…Finally error handling </li></ul>Code Example 1 2 3 4 5 6 7
  29. 29. Reading Data from a DataReader <ul><li>Call Read for each record </li></ul><ul><ul><li>Returns false when there are no more records </li></ul></ul><ul><li>Access fields </li></ul><ul><ul><li>Parameter is the ordinal position or name of the field </li></ul></ul><ul><ul><li>Get functions give best performance </li></ul></ul><ul><li>Close the DataReader </li></ul><ul><li>Close the connection </li></ul>Do While myReader.Read() str &= myReader(1) str &= myReader(&quot;field&quot;) str &= myReader.GetDateTime(2) Loop while (myReader.Read()) { str += myReader[1]; str += myReader[&quot;field&quot;]; str += myReader.GetDateTime(2); }
  30. 30. Binding a DataReader to a List-Bound Control <ul><li>Create the Control </li></ul><ul><li>Bind to a DataReader </li></ul>dgAuthors.DataSource = dr dgAuthors.DataBind() <asp:DataGrid id=&quot;dgAuthors&quot; runat=&quot;server&quot; /> dgAuthors.DataSource = dr; dgAuthors.DataBind();
  31. 31. Practice: Organizing Code to Create a DataReader <ul><li>Students will: </li></ul><ul><ul><li>Reorder lines of code to create a DataReader </li></ul></ul><ul><li>Time: 5 Minutes </li></ul>
  32. 32. Demonstration: Displaying Data Using DataReaders <ul><li>Create a SqlConnection </li></ul><ul><li>Create a DataReader </li></ul><ul><li>Bind the DataReader to a ListBox </li></ul><ul><li>Build ListBox items from data supplied by the DataReader </li></ul>
  33. 33. Review <ul><li>Introduction to Using ADO.NET </li></ul><ul><li>Connecting to a Database </li></ul><ul><li>Accessing Data with DataSets </li></ul><ul><li>Using Multiple Tables </li></ul><ul><li>Accessing Data with DataReaders </li></ul>
  34. 34. Lab 10: Accessing Data with Microsoft ADO.NET Medical Medical.aspx Benefits Home Page Default.aspx Life Insurance Life.aspx Retirement Retirement.aspx Dental Dental.aspx Dentists Doctors Doctors.aspx Doctors Logon Page Login.aspx Registration Register.aspx Coho Winery Prospectus Prospectus.aspx XML Web Service dentalService1.asmx Page Header Header.ascx ASPState tempdb Lab Web Application User Control namedate.ascx Menu Component Class1.vb or Class1.cs XML Files Web. config