Blog di Andrea Soldano

"I have a dream ..."
posts - 53, comments - 263, trackbacks - 18

My Links

News








Tag Cloud

Archives

Post Categories

I miei siti

DB Object: Examples for a WebApplication

L'oggetto che potete scaricare nella sezione downloads non è aggiornatissimo, l'ho scritto nel 2001.

Prima o poi troverò il tempo per aggiornarlo. Per le applicazioni scritte con asp.net 1.1 può essere molto utile.

Di seguito riporto alcuni esempi di utilizzo:

Aggiungete nel file di configurazione il seguente codice:

<appSettings>
  <add key="ConnectionString" value="Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=."/>
 </appSettings>

Ecco la pagina:

<%@ Page Language="VB" %>
<%@ Import Namespace="Esamatic.Database" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Protected Sub btnOpenConnection_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpenConnection.Click
        'Connection open using ConnetionString retrived from web.config AppSetting
        DB.OpenDB()
        Response.Write("Connection Open using ConnetionString retrived from web.config AppSetting .")

        DB.CloseDB()
        Response.Write("Connection Close")
    End Sub

    Protected Sub btnOpenConnection1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpenConnection1.Click
        'Connection open setting a ConnectionString
        DB.ConnectionString = "Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=."
        DB.OpenDB()
        Response.Write("Connection Open setting a ConnectionString.")

        DB.CloseDB()
        Response.Write("Connection Close")
    End Sub


    Protected Sub btnOpenConnection2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpenConnection2.Click
        'Connection open setting a SQLConnection object
        Dim con As New System.Data.SqlClient.SqlConnection("Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=.")
        DB.Connessione = con
        DB.OpenDB()
        Response.Write("Connection open setting a SQLConnection object.")

        DB.CloseDB()
        Response.Write("Connection Close")
    End Sub

    Protected Sub btnRetriveDataset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRetriveDataset.Click
        'Retrive data into a DataSet and bind them to a GridView
        gw.DataSource = DB.GetDataset("Select * from Customers", "Customers").Tables(0)
        gw.DataBind()
    End Sub

    Protected Sub btnExecuteScalar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExecuteScalar.Click
        'execute a command using ExecuteScalar
        DB.OpenDB()
        Response.Write("Customers count: " & DB.CreateCommand("SELECT count(*) From Customers").ExecuteScalar())
        DB.CloseDB()
    End Sub

    Protected Sub btnExecuteDataReader_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExecuteDataReader.Click
        'retrive a datareader
        DB.OpenDB()
        Dim dr As System.Data.IDataReader = DB.CreateCommand("SELECT top 2 * From Customers").ExecuteReader()
        While dr.Read
            Response.Write(dr(0) & "<br/>")
        End While
        DB.CloseDB()
    End Sub

    Protected Sub btnExecuteCommandWithParameters_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExecuteCommandWithParameters.Click
        DB.OpenDB()
        Dim cmd As System.Data.IDbCommand = DB.CreateCommand("SELECT * From Customers where CustomerID = " & DB.CreateQueryParameterName("@CustomerID"))
        cmd.Parameters.Add(DB.CreateParameter(cmd, "CustomerID", System.Data.DbType.String, "ALFKI"))
        Dim dr As System.Data.IDataReader = cmd.ExecuteReader()
        While dr.Read
            Response.Write(dr(0) & "<br/>")
        End While
        DB.CloseDB()
    End Sub
   
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnOpenConnection" runat="server" Text="Open Connection using web.config information." />
        <asp:Button ID="btnOpenConnection1" runat="server" Text="Open Connection using setting a connection string." />
        <asp:Button ID="btnOpenConnection2" runat="server" Text="Open Connection setting a connection object." />
        <hr />
        <asp:Button ID="btnRetriveDataset" runat="server" Text="Retrive a dataset." />
        <asp:GridView ID="gw" runat="server" EnableViewState="false">
        </asp:GridView>
        <asp:Button ID="btnExecuteScalar" runat="server" Text="Execute a scalar command" />
        <asp:Button ID="btnExecuteDataReader" runat="server" Text="Execute a DataReader" />
        <asp:Button ID="btnExecuteCommandWithParameters" runat="server" Text="Execute a Command With Parameters" />   
    </div>
    </form>
</body>
</html>

 

Print | posted on lunedì 23 luglio 2007 15:23 | Filed Under [ ADO.NET DBObject ]

Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET