ti faccio un esempio:
database = Access
tabella = Tabella7
campo Nome di tipo testo, richiesto=sì, consenti lunghezza zero=no, indicizzato=sì(duplicati non ammessi)
in questo modo mi assicuro di non poter inserire nè nomi vuoti, nè nomi duplicati
pagina aspx
codice:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="a.aspx.vb" Inherits="prove_a" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Pagina senza titolo</title>
<link href="../stili/Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Width="252px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Label ID="Label1" runat="server"></asp:Label></div>
</form>
</body>
</html>
codice:
codice:
Option Strict On
Partial Class prove_a
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Connessione As OleDbConnection = Nothing
Dim Comando As OleDbCommand = Nothing
Dim Transazione As OleDbTransaction = Nothing
Dim Sql$ = ""
Try
Connessione = New OleDbConnection(StringaConnessioneTest)
Connessione.Open()
Transazione = Connessione.BeginTransaction()
Comando = New OleDbCommand
Comando.Connection = Connessione
Comando.Transaction = Transazione
Append(Comando)
'Update(Comando)
'Delete(Comando)
Transazione.Commit()
'Transazione.Rollback()
Me.Label1.Text = "Operazioni su database eseguite regolarmente"
Catch ex As Exception
If (Not (Transazione Is Nothing) AndAlso Not (Transazione.Connection Is Nothing)) Then Transazione.Rollback()
Me.Label1.Text = "Errore nell'aggiornamento:" & vbNewLine & vbNewLine & ex.Message
Finally
If (Not (Connessione Is Nothing)) Then Connessione.Close()
End Try
End Sub
Private Sub Append(ByVal Comando As OleDbCommand)
Dim prossimo_id As Integer = libreria.ProssimoIDAccess(Comando, "tabella7", "id")
Dim Sql$ = "INSERT INTO [tabella7] ( [nome], [ID]) VALUES (?,?) "
Comando.CommandText = Sql
Comando.Parameters.Clear()
Comando.Parameters.Add("nome", OleDbType.VarChar, 50).Value = Me.TextBox1.Text
Comando.Parameters.Add("id", OleDbType.Integer).Value = prossimo_id
Comando.ExecuteNonQuery()
End Sub
End Class