Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.

Similar presentations


Presentation on theme: "Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET."— Presentation transcript:

1 Session 8: ADO.NET

2 Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET Creating a Connection to a Database Displaying a DataSet in a List-Bound Control Accessing Data with DataSets Accessing Data with DataReaders Using Multiple Tables

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

4 Using Namespaces Using the Imports statement to import namespaces Namespaces used with ADO.NET include: System.Data System.Data.SqlClient System.Data.OleDb Imports System.Data Imports System.Data.SqlClient Imports System.Data Imports System.Data.SqlClient

5 DataSet SQL Server.NET Data Provider OLE DB.NET Data Provider SQL Server 7.0 (and later) OLEDB sources (SQL Server 6.5) OleDbConnection OleDbDataAdapter SqlDataAdapter SqlConnection DataTable The ADO.NET Object Model

6 Using the ADO.NET Object Model

7 SQL Server 2000 DataSet DataTable Physical storage OleDb Database SqlDataAdapter SqlConnection DataTable Web server memory OleDbDataAdapter OleDbConnection What is a Dataset?

8 Accessing Data with ADO.NET Database 4.Return the DataSet to the Client 5.Client manipulates the data 2.Create the SqlConnection and SqlDataAdapter objects 3.Fill the DataSet from the DataAdapter and close the connection SqlDataAdapter SqlConnection List-Bound Control 1.Client makes request11 22 33 44 55 6.Update the DataSet 7.Use the SqlDataAdapter to open the SqlConnection, update the database, and close the connection 66 77 Client Web server DataSet

9 The DataAdapter Object Model sp_SELECT Command SelectCommandUpdateCommandInsertCommandDeleteCommand DataAdapter Command Connection sp_UPDATEsp_INSERTsp_DELETE Database DataSet DataReader

10 Generating a DataSet You can generate a DataSet… …through the UI… Creates a DataSet that allows you to access data as an object …or through code… and then fill… Dim ds As New DataSet() DataAdapter1.Fill(ds) DataAdapter2.Fill(ds) DataAdapter1.Fill(ds) DataAdapter2.Fill(ds)

11 What are List-Bound Controls? Controls that connect to a data source and display the data List-bound controls include the following: DropDownList ListBox CheckBoxList RadioButtonList DataGrid DataList Repeater

12 Displaying DataSet Data in List-Bound Controls Set the properties Fill the DataSet, then call the DataBind method DataAdapter1.Fill(ds) lstEmployees.DataBind() DataAdapter1.Fill(ds) lstEmployees.DataBind()PropertyPropertyDescriptionDescription DataSource  The DataSet containing the data DataMember  The DataTable in the DataSet DataTextField  The field in the DataTable that is displayed DataValueField  The field in the DataTable that becomes the value of the selected item in the list

13 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 DataReaderDataSet DisconnectedConnected

14 What is a DataReader? Forward-only, read-only Fast access to data Connected to a data source Manage the connection yourself Manage the data yourself, or bind it to a list-bound control Uses fewer server resources

15 Creating a DataReader To use a DataReader: 1.Create and open the database connection 2.Create a Command object 3.Create a DataReader from the Command object 4.Call the ExecuteReader method 5.Use the DataReader object 6.Close the DataReader object 7.Close the Connection object Use Try…Catch…Finally error handling 11 22 33 44 55 66 77

16 Reading Data from a DataReader Call Read for each record Returns false when there are no more records Access fields Parameter is the ordinal position or name of the field Get functions give best performance Close the DataReader Close the connection Do While myReader.Read() str &= myReader(1) str &= myReader("field") str &= myReader.GetDateTime(2) Loop Do While myReader.Read() str &= myReader(1) str &= myReader("field") str &= myReader.GetDateTime(2) Loop

17 Binding a DataReader to a List-Bound Control Create the Control Bind to a DataReader dgAuthors.DataSource = dr dgAuthors.DataBind() dgAuthors.DataSource = dr dgAuthors.DataBind()

18 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

19 Creating the Connection Using SqlConnection Setting connection string parameters Connection timeout Data source Initial catalog Integrated security Dim strConn As String = "data source=localhost; " & _ "initial catalog=northwind; integrated security=true" Dim conn As New SqlConnection(strConn) Dim strConn As String = "data source=localhost; " & _ "initial catalog=northwind; integrated security=true" Dim conn As New SqlConnection(strConn) Password Persist security info Provider User ID

20 Store the query in a DataAdapter The DataAdapter constructor sets the SelectCommand property Set the InsertCommand, UpdateCommand, and DeleteCommand properties if needed Creating a DataAdapter Dim da As New SqlDataAdapter _ ("select * from Authors", conn) Dim da As New SqlDataAdapter _ ("select * from Authors", conn) da.SelectCommand.CommandText da.SelectCommand.Connection da.SelectCommand.CommandText da.SelectCommand.Connection

21 Creating a DataSet Create and populate a DataSet with DataTables Fill method executes the SelectCommand Access a DataTable Dim ds As New DataSet() da.Fill(ds, "Authors") Dim ds As New DataSet() da.Fill(ds, "Authors") Dim r As DataRow Dim str As String For Each r in _ ds.Tables("Authors").Rows str &= r(2) str &= r("au_lname") Next Dim r As DataRow Dim str As String For Each r in _ ds.Tables("Authors").Rows str &= r(2) str &= r("au_lname") Next ds.Tables("Authors").Rows.Count

22 Using a DataView A DataView can be customized to present a subset of data from a DataTable The DefaultView property returns the default DataView of the table Setting up a different view of a DataSet Dim dv As DataView = ds.Tables("Authors").DefaultView Dim dv As New DataView (ds.Tables("Authors")) dv.RowFilter = "state = 'CA'" Dim dv As New DataView (ds.Tables("Authors")) dv.RowFilter = "state = 'CA'"

23 Binding a DataSet to a List-Bound Control Create the control Bind to a DataSet or a DataView dg.DataSource = ds dg.DataMember = "Authors" dg.DataBind() dg.DataSource = ds dg.DataMember = "Authors" dg.DataBind()

24 Storing Multiple Tables Add the first table Add the subsequent table(s) daCustomers = New SqlDataAdapter _ ("select * from Customers", conn1) daCustomers.Fill(ds, "Customers") daCustomers = New SqlDataAdapter _ ("select * from Customers", conn1) daCustomers.Fill(ds, "Customers") Orders Customers daOrders = New SqlDataAdapter _ ("select * from Orders", conn2) daOrders.Fill(ds, "Orders") daOrders = New SqlDataAdapter _ ("select * from Orders", conn2) daOrders.Fill(ds, "Orders") conn2conn1 DataSet

25 Creating Relationships Identify parent column Identify child column Create DataRelation Dim dr As New DataRelation _ ("name", parentCol, _ childCol) ds.DataRelations.Add(dr) Dim dr As New DataRelation _ ("name", parentCol, _ childCol) ds.DataRelations.Add(dr) Dim parentCol As DataColumn = _ ds.Tables("Customers").Columns("CustomerID") Dim parentCol As DataColumn = _ ds.Tables("Customers").Columns("CustomerID") Dim childCol As DataColumn = _ ds.Tables("Orders").Columns("CustomerID") Dim childCol As DataColumn = _ ds.Tables("Orders").Columns("CustomerID") Orders table Customers table DataSet parentCol childCol DataRelation

26 Programmatically Navigating Between Tables Using Relationships ds.Tables(index).Rows(index).GetChildRows("relation") ds.Tables(index).Rows(index).GetParentRow("relation") ds.Tables(index).Rows(index).GetChildRows("relation") ds.Tables(index).Rows(index).GetParentRow("relation") Customers Orders GetChildRows GetParentRow DataSet

27 Visually Navigating Between Tables Using Relationships Dim tableView As DataView Dim currentRowView As DataRowView tableView = New DataView(ds.Tables("Customers")) currentRowView = tableView(dgCustomers.SelectedIndex) dgChild.DataSource = currentRowView.CreateChildView("CustOrders") Dim tableView As DataView Dim currentRowView As DataRowView tableView = New DataView(ds.Tables("Customers")) currentRowView = tableView(dgCustomers.SelectedIndex) dgChild.DataSource = currentRowView.CreateChildView("CustOrders") Customers Orders CreateChildView DataRowView DataView DataSet


Download ppt "Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET."

Similar presentations


Ads by Google