GESTIONE DI MDB in WEB Il presente documento riporta

annuncio pubblicitario
Gestione database di Access con ASP in ambiente WEB
GESTIONE DI MDB in WEB
Il presente documento riporta un esempio generico di gestione di un database
di Access.
Il lettore può personalizzare in base alle proprie esigenze sia il contenuto del
database, sia l’aspetto grafico delle pagine web.
Operazioni preliminari
• Posizionarsi nella cartella <RootSito>/mdb-database/database
• La cartella creata deve essere condivisa con permessi di scrittura e/o
modifica da parte di altri utenti della rete
• Avviare ACCESS e creare qui un database vuoto di nome: Database
• Creare nel database una tabella di nome Tabella
• La Tabella deve avere la seguente struttura:
Codice
Testo, 5 caratteri
Descrizione Testo, 50 caratteri
Creazione pagine WEB
Creare in una cartella del sito Web i seguenti file, corrispondenti ad altrettante
pagine web:
Home.html (Home Page della sezione del ‘sito’ dedicata alla gestione del database)
<HTML>
<HEAD>
<TITLE>Gestione di un database di ACCESS</TITLE>
</HEAD>
<BODY>
<OL TYPE="1">
<LI><A HREF="Inserim.html">Inserimento</A></LI>
<LI><A HREF="modifica.html">Modifica</A></LI>
<LI><A HREF="elimina.html">Eliminazione</A></LI>
<LI><A HREF="visualizza.asp">Visualizzazione</A></LI>
</OL>
</BODY>
</HTML>
Config.asp(questo file serve solo per eseguire identiche operazioni in tutte le pagine asp)
<%
Option Explicit
'On Error Resume Next
Response.Buffer = True
Dim StrCn
StrCn = ""
StrCn = StrCn & "driver={Microsoft Access Driver (*.mdb)};"
StrCn = StrCn & "dbq=" & Server.MapPath("../mdb-database/database/database.mdb")
%>
Realizzato da C. Ferrara
Gestione database di Access con ASP in ambiente WEB
Pagine per ‘Operazioni di INSERIMENTO di record’
Inserim.html
<HTML>
<HEAD><TITLE>Gestione di un database</TITLE></HEAD>
<BODY>
<TABLE border="1">
<FORM method="post" action="RegistraIns.asp">
<TR>
<TD>
Codice<BR>
<INPUT type="text" name="codice"><BR>
Descrizione<BR>
<INPUT type="text" name="descrizione"><BR>
<INPUT type="submit" value="Inserisci"><BR>
<INPUT type="reset" value="Annulla"><BR>
</TD>
</TR>
</TABLE>
<a href='home.html'>Indietro</a></p>
</BODY>
</HTML>
RegistraIns.asp
<% @LANGUAGE = VBScript %>
<!--#include file="config.asp"-->
<%
Dim C, D, CN, RS
%>
<html>
<head>
<title>Registrazione dati</title>
</head>
<body>
<%
' Recupero i dati dal form
C = Request.Form("codice")
D = Request.Form("descrizione")
' Mi connetto al database
Set Cn = Server.CreateObject("ADODB.Connection")
Cn.Open StrCn
' Verifico che i dati da registrare non esistano già
Dim SQL
SQL = "SELECT * FROM tabella WHERE codice = '" & C & "' AND descrizione = '" & D & "';"
Set Rs = Server.CreateObject("ADODB.Recordset")
rs.open SQL, cn, 2, 3
If Not rs.EOF Then
Response.Write "<p align=""center"">I dati specificati sono già presenti</p>"
Else
' Se non esiste, effettuo la registrazione
rs.close
Rs.Open "tabella", Cn, 2, 3
Rs.AddNew
Rs("codice") = C
Rs("descrizione") = D
Rs.Update
Response.Write "<p align=""center"">Registrazione avvenuta con successo!</p>"
End If
Rs.Close
Set Rs = Nothing
Cn.Close
Set Cn = Nothing
With Response
.Write "<p align=""center"">"
.Write "<a href='home.html'>"
.Write "Indietro</a></p>"
End With
%>
</body>
</html>
Realizzato da C. Ferrara
Gestione database di Access con ASP in ambiente WEB
Pagine per ‘Operazioni di MODIFICA di record’
Modifica.html
<HTML>
<HEAD><TITLE>Gestione di un database</TITLE></HEAD>
<BODY>
<TABLE border="1">
<FORM method="post" action="modifica.asp">
<TR>
<TD>
Codice del record da modificare<BR>
<INPUT type="text" name="codice"><BR>
<INPUT type="submit" value="Cerca record">
<INPUT type="reset" value="Annulla">
</TD>
</TR>
</TABLE>
<a href='home.html'>Indietro</a></p>
</BODY>
</HTML>
Modifica.asp
<%@LANGUAGE = VBScript%>
<!--#include file="config.asp"-->
<%
Dim C, D, Cn, Rs
%>
<html>
<head>
<title>Modifica Record di una tabella</title>
</head>
<body>
<%
' Recupero il record con il codice inserito
C = Request.Form("codice")
' Mi connetto al database
Set Cn = Server.CreateObject("ADODB.Connection")
Cn.Open StrCn
' Verifico che il record esista già
Dim SQL
SQL = "SELECT * FROM tabella WHERE Codice = '" & C & "';"
Set Rs = Server.CreateObject("ADODB.Recordset")
rs.open SQL, cn
If rs.EOF=True and rs.BOF=True Then
Response.Write "<p align=""center"">Il codice specificato non esiste</p>"
else
%>
<table border="1">
<form method="post" action="RegistraMod.asp">
<tr>
<td>Codice<br>
<input type="text" name="Codice" value="<%=Rs("Codice")%>" maxlength="10" style="width: 100%;">
Descrizione<br>
<input type="text" name="Descrizione" value="<%=Rs("Descrizione")%>" maxlength="80" style="width:
100%;">
</td>
</tr>
<tr>
<td align="right">
<input type="submit" value="Salva"></td>
</tr>
</form>
</table>
<%
End If
rs.Close
set Rs = Nothing
Cn.Close
Set Cn = Nothing
%>
<a href='home.html'>Indietro</a></p>
</body>
</html>
Realizzato da C. Ferrara
Gestione database di Access con ASP in ambiente WEB
RegistraMod.asp
<%@LANGUAGE = VBScript%>
<!--#include file="config.asp"-->
<%
Dim C, D
%>
<html>
<head>
<title>Registrazione modifica dati</title>
</head>
<body>
<%
Dim SQL
dim Cn, Rs
' Recupero i dati
C = Request.Form("Codice")
D = Request.Form("Descrizione")
Response.Write "Dati ricevuti:"+"<BR>"
Response.Write "Codice = " + C+"<BR>"
Response.Write "Descrizione = " + D+"<BR>"
' Mi connetto al database
Set Cn = Server.CreateObject("ADODB.Connection")
Cn.Open StrCn
SQL = "SELECT * FROM tabella WHERE Codice = '" & C & "';"
Set Rs = Server.CreateObject("ADODB.Recordset")
rs.open SQL, cn, 2, 3
If Not rs.EOF Then
' Se esiste già effettuo la Modifica
Rs("codice") = C
Rs("descrizione") = D
Rs.Update
Response.Write "Dati salvati:"+"<BR>"
Response.Write "Codice = " + Rs("codice")+"<BR>"
Response.Write "Descrizione = " + Rs("descrizione")+"<BR>"
Response.Write "<p align=""center"">Modifica avvenuta con successo!</p>"
Else
Response.Write "<p align=""center"">Impossibile effettuare la modifica. Il record non è più presente</p>"
End If
Rs.Close
Set Rs = Nothing
Cn.Close
Set Cn = Nothing
With Response
.Write "<p align=""center"">"
.Write "<a href='home.html'>"
.Write "Indietro</a></p>"
End With
%>
</body>
</html>
Realizzato da C. Ferrara
Gestione database di Access con ASP in ambiente WEB
Pagine per ‘Operazioni di CANCELLAZIONE di record’
Elimina.html
<HTML>
<HEAD><TITLE>Gestione di un database</TITLE></HEAD>
<BODY>
<TABLE border="1">
<FORM method="post" action="elimina.asp">
<TR>
<TD>
Codice del record da eliminare<BR>
<INPUT type="text" name="codice"><BR>
<INPUT type="submit" value="Cerca record">
<INPUT type="reset" value="Annulla">
</TD>
</TR>
</TABLE>
<a href='home.html'>Indietro</a></p>
</BODY>
</HTML>
Elimina.asp
<%@LANGUAGE = VBScript%>
<!--#include file="config.asp"-->
<%
Dim C, D
%>
<html>
<head>
<title>Eliminazione Record di una tabella</title>
</head>
<body>
<%
Dim Cn, Rs
' Recupero il record con il codice inserito
C = Request.Form("codice")
' Mi connetto al database
Set Cn = Server.CreateObject("ADODB.Connection")
Cn.Open StrCn
' Verifico che il record esista
Dim SQL
SQL = "SELECT * FROM tabella WHERE Codice = '" & C & "';"
Set Rs = Server.CreateObject("ADODB.Recordset")
rs.open SQL, cn
If rs.EOF=True and rs.BOF=True Then
Response.Write "<p align=""center"">Il codice specificato non esiste</p>"
else
%>
<table border="1">
<form method="post" action="RegistraDel.asp">
<tr>
<td>Codice<br>
<input type="text" name="Codice" value="<%=Rs("Codice")%>" maxlength="10" style="width: 100%;">
Descrizione<br>
<input type="text" name="Descrizione" value="<%=Rs("Descrizione")%>" maxlength="80" style="width:
100%;">
</td>
</tr>
<tr>
<td align="right">
<input type="submit" value="Elimina"></td>
</tr>
</form>
</table>
<%
End If
rs.Close
set Rs = Nothing
Cn.Close
Set Cn = Nothing
%>
<a href='home.html'>Indietro</a></p>
</body>
</html>
Realizzato da C. Ferrara
Gestione database di Access con ASP in ambiente WEB
RegistraDel.asp
<%@LANGUAGE = VBScript%>
<!--#include file="config.asp"-->
<%
Dim C, D
%>
<html>
<head>
<title>Eliminazione Record di una tabella</title>
</head>
<body>
<%
Dim Cn, Rs
' Recupero il record con il codice inserito
C = Request.Form("codice")
' Mi connetto al database
Set Cn = Server.CreateObject("ADODB.Connection")
Cn.Open StrCn
' Verifico che il record esista
Dim SQL
SQL = "SELECT * FROM tabella WHERE Codice = '" & C & "';"
Set Rs = Server.CreateObject("ADODB.Recordset")
rs.open SQL, cn
If rs.EOF=True and rs.BOF=True Then
Response.Write "<p align=""center"">Il codice specificato non esiste</p>"
else
%>
<table border="1">
<form method="post" action="RegistraDel.asp">
<tr>
<td>Codice<br>
<input type="text" name="Codice" value="<%=Rs("Codice")%>" maxlength="10" style="width: 100%;">
Descrizione<br>
<input type="text" name="Descrizione" value="<%=Rs("Descrizione")%>" maxlength="80" style="width:
100%;">
</td>
</tr>
<tr>
<td align="right">
<input type="submit" value="Elimina"></td>
</tr>
</form>
</table>
<%
End If
rs.Close
set Rs = Nothing
Cn.Close
Set Cn = Nothing
%>
<a href='home.html'>Indietro</a></p>
</body>
</html>
Realizzato da C. Ferrara
Scarica