codice:
Option Strict On
Partial Class CorsoApogeo_wrox_gestione_dati_comandi_parametrici_con_transazione
Inherits System.Web.UI.Page
Protected messaggioJS$ = "" 'serve nel client
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()
messaggioJS = toStringaJS("Operazioni su database eseguite regolarmente")
Catch ex As Exception
If (Not (Transazione Is Nothing) AndAlso Not (Transazione.Connection Is Nothing)) Then Transazione.Rollback()
Me.messaggioJS = toStringaJS("Errore nell'aggiornamento:" & vbNewLine & vbNewLine & exMessage(ex))
Finally
If (Not (Connessione Is Nothing)) Then Connessione.Close()
End Try
End Sub
Private m_prossimo_id% = 0
Private Sub Append(ByVal Comando As OleDbCommand)
Me.m_prossimo_id = libreria.ProssimoIDAccess(Comando, "campi", "id")
Dim Sql$ = "INSERT INTO [CAMPI] ( [PASSWORD], [TESTO], [MEMO], [DATA_ORA], [VALUTA], [SI_NO], [BYTE], [INTERO], [LONG], [SINGLE], [DOUBLE], [DECIMALE], [ID]) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) "
Comando.CommandText = Sql
Comando.Parameters.Clear()
Comando.Parameters.Add("password", OleDbType.VarChar, 50).Value = DBNull.Value
Comando.Parameters.Add("testo", OleDbType.VarChar, 50).Value = DBNull.Value
Comando.Parameters.Add("memo", OleDbType.VarChar, 60000).Value = DBNull.Value
Comando.Parameters.Add("data_ora", OleDbType.Date).Value = DBNull.Value
Comando.Parameters.Add("valuta", OleDbType.Currency).Value = DBNull.Value
Comando.Parameters.Add("si_no", OleDbType.Boolean).Value = DBNull.Value
Comando.Parameters.Add("byte", OleDbType.TinyInt).Value = DBNull.Value
Comando.Parameters.Add("intero", OleDbType.Integer).Value = DBNull.Value
Comando.Parameters.Add("long", OleDbType.Integer).Value = DBNull.Value
Comando.Parameters.Add("single", OleDbType.Single).Value = DBNull.Value
Comando.Parameters.Add("double", OleDbType.Double).Value = DBNull.Value
Comando.Parameters.Add("decimal", OleDbType.Decimal).Value = DBNull.Value
Comando.Parameters.Add("id", OleDbType.Integer).Value = Me.m_prossimo_id
Comando.ExecuteNonQuery()
End Sub
Private Sub Update(ByVal Comando As OleDbCommand)
Dim Sql$ = "UPDATE [CAMPI] SET [PASSWORD] = ?, [TESTO] = ?, [MEMO] = ?, [DATA_ORA] = ?, [VALUTA] = ?, [SI_NO] = ?, [BYTE] = ?, [INTERO] = ?, [LONG] = ?, [SINGLE] = ?, [DOUBLE] = ?, [DECIMALE] = ? WHERE [ID] = ? "
Comando.CommandText = Sql
Comando.Parameters.Clear()
Comando.Parameters.Add("password", OleDbType.VarChar, 50).Value = DBNull.Value
Comando.Parameters.Add("testo", OleDbType.VarChar, 50).Value = DBNull.Value
Comando.Parameters.Add("memo", OleDbType.VarChar, 60000).Value = DBNull.Value
Comando.Parameters.Add("data_ora", OleDbType.Date).Value = DBNull.Value
Comando.Parameters.Add("valuta", OleDbType.Currency).Value = DBNull.Value
Comando.Parameters.Add("si_no", OleDbType.Boolean).Value = DBNull.Value
Comando.Parameters.Add("byte", OleDbType.TinyInt).Value = DBNull.Value
Comando.Parameters.Add("intero", OleDbType.Integer).Value = DBNull.Value
Comando.Parameters.Add("long", OleDbType.Integer).Value = DBNull.Value
Comando.Parameters.Add("single", OleDbType.Single).Value = DBNull.Value
Comando.Parameters.Add("double", OleDbType.Double).Value = DBNull.Value
Comando.Parameters.Add("decimal", OleDbType.Decimal).Value = DBNull.Value
Comando.Parameters.Add("id", OleDbType.Integer).Value = Me.m_prossimo_id
Comando.ExecuteNonQuery()
End Sub
Private Sub Delete(ByVal Comando As OleDbCommand)
Dim Sql$ = "DELETE FROM [CAMPI] WHERE [ID] = ? "
Comando.CommandText = Sql
Comando.Parameters.Clear()
Comando.Parameters.Add("id", OleDbType.Integer).Value = Me.m_prossimo_id
Comando.ExecuteNonQuery()
End Sub
End Class
qulche nota: