SlideShare una empresa de Scribd logo
1 de 34
Module 10:  Accessing Data with Microsoft ADO.NET
Overview ,[object Object],[object Object],[object Object],[object Object],[object Object]
Lesson: Introduction to Using ADO.NET ,[object Object],[object Object],[object Object]
Multimedia: The ADO.NET Object Model
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
Practice: When to Use DataSets or DataReaders ,[object Object],[object Object],[object Object]
Lesson: Connecting to a Database ,[object Object],[object Object],[object Object]
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
Creating the Connection ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Dim strConn As String = "data source=localhost; " & _ "initial catalog=northwind; integrated security=true" Dim conn As New SqlConnection(strConn) ,[object Object],[object Object],[object Object],[object Object],string strConn = "data source=localhost; " + "initial catalog=northwind; integrated security=true"; SqlConnection conn = new SqlConnection(strConn);
Demonstration: Setting SQL Server Security ,[object Object],[object Object],[object Object],[object Object]
Lesson: Accessing Data with DataSets ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a DataAdapter ,[object Object],[object Object],[object Object],Dim da As New SqlDataAdapter _ ("select * from Authors", conn) da.SelectCommand.CommandText  da.SelectCommand.Connection SqlDataAdapter da = new SqlDataAdapter ("select * from Authors",conn); da.SelectCommand.CommandText; da.SelectCommand.Connection;
Creating a DataSet ,[object Object],[object Object],[object Object],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 ds.Tables("Authors").Rows.Count DataSet ds = new DataSet(); da.Fill(ds, "Authors"); ds.Tables["Authors"].Rows.Count; string str=""; foreach(DataRow r in  ds.Tables["Authors"].Rows) { str += r[2]; str += r["au_lname"]; }
Demonstration: Programmatically Using a DataSet ,[object Object],[object Object],[object Object],[object Object]
Using a DataView ,[object Object],[object Object],[object Object],DataView dv = new DataView(ds.Tables["Authors"]); dv.RowFilter = "state = 'CA'"; Dim dv As DataView = ds.Tables("Authors").DefaultView   Dim dv As New DataView (ds.Tables("Authors")) dv.RowFilter = "state = 'CA'"  DataView dv = ds.Tables["Authors"].DefaultView;
Practice: Organizing Code to Create a DataSet ,[object Object],[object Object],[object Object]
Binding a DataSet to a List-Bound Control ,[object Object],[object Object],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() ;
Instructor-Led Practice: Displaying a DataSet ,[object Object],[object Object],[object Object],[object Object],[object Object]
Handling Errors ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Code Example
Lesson: Using Multiple Tables ,[object Object],[object Object],[object Object],[object Object],[object Object]
Storing Multiple Tables  ,[object Object],[object Object],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
Creating Relationships ,[object Object],[object Object],[object Object],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
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;);
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;);
Instructor-Led Practice: Displaying Data from Multiple Tables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lesson: Accessing Data with DataReaders ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What is a DataReader? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a DataReader ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Code Example 1 2 3 4 5 6 7
Reading Data from a DataReader ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],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); }
Binding a DataReader to a List-Bound Control ,[object Object],[object Object],dgAuthors.DataSource = dr dgAuthors.DataBind()  <asp:DataGrid id=&quot;dgAuthors&quot; runat=&quot;server&quot; /> dgAuthors.DataSource = dr; dgAuthors.DataBind();
Practice: Organizing Code to Create a DataReader ,[object Object],[object Object],[object Object]
Demonstration: Displaying Data Using DataReaders ,[object Object],[object Object],[object Object],[object Object]
Review ,[object Object],[object Object],[object Object],[object Object],[object Object]
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

Más contenido relacionado

La actualidad más candente

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
CS101- Introduction to Computing- Lecture 26
CS101- Introduction to Computing- Lecture 26CS101- Introduction to Computing- Lecture 26
CS101- Introduction to Computing- Lecture 26Bilal Ahmed
 
SFDC Advanced Apex
SFDC Advanced Apex SFDC Advanced Apex
SFDC Advanced Apex Sujit Kumar
 
More Stored Procedures and MUMPS for DivConq
More Stored Procedures and  MUMPS for DivConqMore Stored Procedures and  MUMPS for DivConq
More Stored Procedures and MUMPS for DivConqeTimeline, LLC
 
Building DSLs with Groovy
Building DSLs with GroovyBuilding DSLs with Groovy
Building DSLs with GroovySten Anderson
 
Creating a Custom Tanimoto or Cosine Similarity Vector Operator for Lucene / ...
Creating a Custom Tanimoto or Cosine Similarity Vector Operator for Lucene / ...Creating a Custom Tanimoto or Cosine Similarity Vector Operator for Lucene / ...
Creating a Custom Tanimoto or Cosine Similarity Vector Operator for Lucene / ...Lucidworks
 
Design Summit - Navigating the ManageIQ Object Model - Brad Ascar
Design Summit - Navigating the ManageIQ Object Model - Brad AscarDesign Summit - Navigating the ManageIQ Object Model - Brad Ascar
Design Summit - Navigating the ManageIQ Object Model - Brad AscarManageIQ
 
Spark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSpark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSigmoid
 
Appengine Java Night #2b
Appengine Java Night #2bAppengine Java Night #2b
Appengine Java Night #2bShinichi Ogawa
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracleyazidds2
 
Wcf data services
Wcf data servicesWcf data services
Wcf data servicesEyal Vardi
 
Appengine Java Night #2a
Appengine Java Night #2aAppengine Java Night #2a
Appengine Java Night #2aShinichi Ogawa
 
QA Fest 2014. Анна Гаврилюк. Cool as сucumber
QA Fest 2014. Анна Гаврилюк. Cool as сucumberQA Fest 2014. Анна Гаврилюк. Cool as сucumber
QA Fest 2014. Анна Гаврилюк. Cool as сucumberQAFest
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and ArraysWebStackAcademy
 

La actualidad más candente (20)

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
CS101- Introduction to Computing- Lecture 26
CS101- Introduction to Computing- Lecture 26CS101- Introduction to Computing- Lecture 26
CS101- Introduction to Computing- Lecture 26
 
SFDC Advanced Apex
SFDC Advanced Apex SFDC Advanced Apex
SFDC Advanced Apex
 
Android - Saving data
Android - Saving dataAndroid - Saving data
Android - Saving data
 
More Stored Procedures and MUMPS for DivConq
More Stored Procedures and  MUMPS for DivConqMore Stored Procedures and  MUMPS for DivConq
More Stored Procedures and MUMPS for DivConq
 
Building DSLs with Groovy
Building DSLs with GroovyBuilding DSLs with Groovy
Building DSLs with Groovy
 
Ajax chap 3
Ajax chap 3Ajax chap 3
Ajax chap 3
 
Ajax chap 2.-part 1
Ajax chap 2.-part 1Ajax chap 2.-part 1
Ajax chap 2.-part 1
 
Backendless apps
Backendless appsBackendless apps
Backendless apps
 
Creating a Custom Tanimoto or Cosine Similarity Vector Operator for Lucene / ...
Creating a Custom Tanimoto or Cosine Similarity Vector Operator for Lucene / ...Creating a Custom Tanimoto or Cosine Similarity Vector Operator for Lucene / ...
Creating a Custom Tanimoto or Cosine Similarity Vector Operator for Lucene / ...
 
Design Summit - Navigating the ManageIQ Object Model - Brad Ascar
Design Summit - Navigating the ManageIQ Object Model - Brad AscarDesign Summit - Navigating the ManageIQ Object Model - Brad Ascar
Design Summit - Navigating the ManageIQ Object Model - Brad Ascar
 
Spark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSpark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. Jyotiska
 
Appengine Java Night #2b
Appengine Java Night #2bAppengine Java Night #2b
Appengine Java Night #2b
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracle
 
Wcf data services
Wcf data servicesWcf data services
Wcf data services
 
Appengine Java Night #2a
Appengine Java Night #2aAppengine Java Night #2a
Appengine Java Night #2a
 
Python dictionaries
Python dictionariesPython dictionaries
Python dictionaries
 
QA Fest 2014. Анна Гаврилюк. Cool as сucumber
QA Fest 2014. Анна Гаврилюк. Cool as сucumberQA Fest 2014. Анна Гаврилюк. Cool as сucumber
QA Fest 2014. Анна Гаврилюк. Cool as сucumber
 
Graphql, REST and Apollo
Graphql, REST and ApolloGraphql, REST and Apollo
Graphql, REST and Apollo
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 

Destacado (8)

Stored procedure
Stored procedureStored procedure
Stored procedure
 
Module04
Module04Module04
Module04
 
Module03
Module03Module03
Module03
 
Module02
Module02Module02
Module02
 
Module08
Module08Module08
Module08
 
Module06
Module06Module06
Module06
 
2310 b xd
2310 b xd2310 b xd
2310 b xd
 
Module05
Module05Module05
Module05
 

Similar a 2310 b 10 (20)

2310 b 09
2310 b 092310 b 09
2310 b 09
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
ASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NETASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NET
 
Ado.net
Ado.netAdo.net
Ado.net
 
ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And Representation
 
Ch 7 data binding
Ch 7 data bindingCh 7 data binding
Ch 7 data binding
 
Lecture13
Lecture13Lecture13
Lecture13
 
Ado.Net
Ado.NetAdo.Net
Ado.Net
 
Dev308
Dev308Dev308
Dev308
 
Csharp_dotnet_ADO_Net_database_query.pptx
Csharp_dotnet_ADO_Net_database_query.pptxCsharp_dotnet_ADO_Net_database_query.pptx
Csharp_dotnet_ADO_Net_database_query.pptx
 
Dat402
Dat402Dat402
Dat402
 
Advanced dot net
Advanced dot netAdvanced dot net
Advanced dot net
 
Ado.Net Architecture
Ado.Net ArchitectureAdo.Net Architecture
Ado.Net Architecture
 
Ado .net
Ado .netAdo .net
Ado .net
 
Asp db
Asp dbAsp db
Asp db
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
ADO.NET by ASP.NET Development Company in india
ADO.NET by ASP.NET  Development Company in indiaADO.NET by ASP.NET  Development Company in india
ADO.NET by ASP.NET Development Company in india
 
ADO.net control
ADO.net controlADO.net control
ADO.net control
 
Microsoft Ado
Microsoft AdoMicrosoft Ado
Microsoft Ado
 
B_110500002
B_110500002B_110500002
B_110500002
 

Más de Krazy Koder (20)

2310 b xd
2310 b xd2310 b xd
2310 b xd
 
2310 b xd
2310 b xd2310 b xd
2310 b xd
 
2310 b xc
2310 b xc2310 b xc
2310 b xc
 
2310 b xb
2310 b xb2310 b xb
2310 b xb
 
2310 b 17
2310 b 172310 b 17
2310 b 17
 
2310 b 16
2310 b 162310 b 16
2310 b 16
 
2310 b 16
2310 b 162310 b 16
2310 b 16
 
2310 b 15
2310 b 152310 b 15
2310 b 15
 
2310 b 15
2310 b 152310 b 15
2310 b 15
 
2310 b 14
2310 b 142310 b 14
2310 b 14
 
2310 b 13
2310 b 132310 b 13
2310 b 13
 
2310 b 12
2310 b 122310 b 12
2310 b 12
 
2310 b 11
2310 b 112310 b 11
2310 b 11
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 07
2310 b 072310 b 07
2310 b 07
 
2310 b 06
2310 b 062310 b 06
2310 b 06
 
2310 b 05
2310 b 052310 b 05
2310 b 05
 
2310 b 04
2310 b 042310 b 04
2310 b 04
 

2310 b 10

  • 1. Module 10: Accessing Data with Microsoft ADO.NET
  • 2.
  • 3.
  • 4. Multimedia: The ADO.NET Object Model
  • 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.
  • 7.
  • 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.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 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. 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.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 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