ASP.NET: DataReader e DataSet – Codice sorgente dell’esempio

Sorgenti: DataReader.aspxDataSet.aspxConnessione.ascx

Scarica i sorgenti: aspnet10.zip

Articolo a cui si riferisce il codice: ASP.NET: DataReader e DataSet

DataReader.aspx

  1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3 
  4 <%@ Page Language="VB" Debug="True" %>
  5 
  6 <%@ Import Namespace="System.Data" %>
  7 <%@ Import Namespace="System.Data.OleDb" %>
  8 
  9 <%@ Register TagPrefix="cnn" TagName="connection" Src="connessione.ascx" %>
 10 <%@ Register TagPrefix="ANet" TagName="Footer" Src="includes/footer.ascx"%>
 11 
 12 <!
 13 # nome: DataReader.aspx
 14 # scritto da: Antonio Volpon
 15 # data creazione: 14 Novembre 2001
 16 # ultima modifica: 14 Novembre 2001
 17 # copyright: Antonio Volpon
 18 >
 19 
 20 <script language="vb" runat="server">
 21 
 22 Sub Page_Load()
 23 
 24   Dim strSelect As String
 25   Dim objDataSet As New DataSet()
 26   Dim objDataReader As OleDbDataReader
 27   
 28   Dim strTabella As String
 29   
 30   strSelect = "SELECT Titolo,NumeroPagine,Collana FROM Libro WHERE EdizioneAnno = ‘1987’ ORDER BY Titolo"
 31 
 32    Try
 33 
 34     Dim objConnection As New OleDbConnection(ctlConnessione.Access)
 35     
 36     objConnection.Open()
 37 
 38     Dim objCommand As New OleDbCommand(strSelect,objConnection)    
 39 
 40     
 41     objDataReader = objCommand.ExecuteReader()
 42     
 43     While objDataReader.Read
 44       strTabella += "<tr><td>" & objDataReader("Titolo") & "</td><td>" & objDataReader("NumeroPagine") & "</td><td>" & objDataReader("Collana") & "</td></tr>"
 45     End While
 46     
 47     objDataReader.Close()
 48     
 49     objConnection.Close()
 50     
 51    Catch objError As Exception
 52 
 53    risposta.innerHTML = "Errore: " & objError.Message & " " & objError.Source
 54    Exit Sub
 55     
 56    End Try
 57   
 58   risultati.Text = strTabella
 59    
 60 
 61 End Sub
 62 
 63 </script>
 64 
 65 <cnn:connection id="ctlConnessione" runat="server"/>
 66 
 67 <html>
 68 
 69   <head>
 70     <link rel="stylesheet" href="css/aspnet.css" type="text/css">
 71     </link>
 72     <title>DataReader</title>
 73   </head>
 74   
 75   <body>
 76 
 77     <div class="titolo">
 78       DataReader
 79     </div>
 80     
 81     <hr noshade="noshade" size="1" width="100%" />  
 82     
 83     <table border="1" cellspacing="0" cellpadding="3" bordercolor="#C0C0C0">
 84       <tr><td class="txtb">Titolo</td><td class="txtb">Pagine</td><td class="txtb">Collana</td></tr>
 85       <asp:literal id="risultati" runat="server" />
 86     </table>
 87     
 88     <div class="txtb" id="risposta" runat="server"></div>
 89     
 90  <ANet:Footer id="Menu" runat="server" />
 91 
 92   </body>
 93   
 94 </html>

DataSet.aspx

  1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3 
  4 <%@ Page Language="VB" Debug="True" %>
  5 
  6 <%@ Import Namespace="System.Data" %>
  7 <%@ Import Namespace="System.Data.OleDb" %>
  8 
  9 <%@ Register TagPrefix="cnn" TagName="connection" Src="connessione.ascx" %>
 10 <%@ Register TagPrefix="ANet" TagName="Footer" Src="includes/footer.ascx"%>
 11 
 12 <!
 13 # nome: DataSet.aspx
 14 # scritto da: Antonio Volpon
 15 # data creazione: 12 Novembre 2001
 16 # ultima modifica: 12 Novembre 2001
 17 # copyright: Antonio Volpon
 18 >
 19 
 20 <script language="vb" runat="server">
 21 
 22 Sub Page_Load()
 23 
 24   Dim strSelect As String
 25   Dim objDataSet As New DataSet()
 26 
 27   strSelect = "SELECT Titolo,NumeroPagine,Collana FROM Libro WHERE EdizioneAnno = ‘1997’ ORDER BY Titolo"
 28 
 29   Dim objConnection As New OleDbConnection(ctlConnessione.Access)
 30     
 31   Dim objDataAdapter As New OleDbDataAdapter(strSelect, objConnection)
 32   
 33   objDataAdapter.Fill(objDataSet,"Libro")
 34 
 35   Dim objDataView As New DataView(objDataSet.Tables("Libro"))
 36   
 37   libri.DataSource = objDataView
 38   libri.DataBind()
 39 
 40 End Sub
 41 
 42 </script>
 43 
 44 <cnn:connection id="ctlConnessione" runat="server"/>
 45 
 46 <html>
 47 
 48   <head>
 49     <link rel="stylesheet" href="css/aspnet.css" type="text/css">
 50     </link>
 51     <title>Data Set</title>
 52   </head>
 53   
 54   <body>
 55     <div class="titolo">
 56       Data Set
 57     </div>
 58     
 59     <hr noshade="true" size="1" width="100%">  
 60     
 61     <asp:datagrid id="libri" runat="server"/>
 62     
 63  <ANet:Footer id="Menu" runat="server" />
 64 
 65   </body>
 66   
 67 </html>

Connessione.ascx

  1 <%@ Control Language="VB" %>
  2 
  3 <script language="VB" runat="server">
  4 
  5 Public ReadOnly Property SQLServer() As String
  6  Get
  7  Return "Provider=SQLOLEDB.1;data source=nomedatasource;initial catalog=Biblioteca;uid=zzz;pwd=xxx;"
  8  End Get
  9 End Property
 10 
 11 Public ReadOnly Property Access() As String
 12  Get
 13  Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= D:\percorso\biblioteca.mdb"
 14  End Get
 15 End Property
 16 
 17 </script>