Visualizzazione dei risultati da 1 a 5 su 5

Discussione: Transazione

  1. #1

    Transazione

    ho una domanda da farvi riguardo una transazione in asp.net (ado.net)

    in una mia sub ho

    Dim transazione As SqlTransaction

    Dim sql4 As String
    sql4 = "INSERT INTO assenze (id_per, data_in, motivo) VALUES (" & viewstate("osp") & ", '" & Left(viewstate("data_ps"), 10) & "', 'infortunio')"
    Dim cmd4 As New SqlCommand(sql4, cnx)

    Dim sql5 As String
    sql5 = "SELECT MAX(id_ass) FROM assenze"
    Dim cmd5 As New SqlCommand(sql5, cnx)
    Dim id_max_ass As Integer


    cnx.Open()
    transazione = cnx.BeginTransaction
    cmd4.Transaction = transazione
    cmd5.Transaction = transazione

    Try
    cmd4.ExecuteNonQuery()
    id_max_ass = cmd5.ExecuteScalar()
    transazione.Commit()

    Dim sql6 As String
    sql6 = "UPDATE mia_tabella SET attivo=0, id_uuu=" & reparto.SelectedItem.Value & ", id_asse=" & id_max_ass & " WHERE id_rico=" & viewstate("id_cippa") & ""
    Dim cmd6 As New SqlCommand(sql6, cnx)
    cmd6.ExecuteNonQuery()


    Response.Redirect("../moduli_comuni/auto_close.asp")
    cnx.Close()
    Catch ex As Exception
    transazione.Rollback()
    Response.Write("fallito")
    cnx.Close()
    End Try

    la transazione funziona e anche l'operazione seguente, solo che mi piacerebbe fare tutto in un'unica transazione, anche se effettivamente il passaggio delicato è fare i primi 2 in contemporanea.

    Non risco a fare tutto insiame perchè per il 3° comando ho bisogno un valore che ho solo dopo aver effettuato il 2° e non lo posso metterlo nella stringa sql. Se sposto prima del try il 3° comando che voglio integrare nella transazione, la stringa sql6 non è esatta in quanto non ho ancora id_max_ass.

    Come posso fare per mettere tutto insieme ? non voglio usare una SP
    Frate Priore: "È Lucifero in persona!"
    Trinità: "Lo conosci?"
    Bambino: "Mai sentito nominare, deve essere un professionista dell'est"

  2. #2
    Sinceramente non capisco il problema.
    Perche' non vuoi fare la stored?

  3. #3
    Perche' non vuoi fare la stored?
    la verità è perchè sono tanto pigro... stored che poi mi devo ricopiare nel server di produzione (si lo sò ci vogliono 3 sec.)

    In ogni caso, non sarebbe male sapere come fare in .net e basta usando una transazione
    Frate Priore: "È Lucifero in persona!"
    Trinità: "Lo conosci?"
    Bambino: "Mai sentito nominare, deve essere un professionista dell'est"

  4. #4
    id_ass è un campo identity autoincrementante ?
    Se la risposta è si puoi fare:

    "SELECT @@identity as 'Identity'"
    leggendo il valore nel rowset del campo identity puoi procedere a fare gli update sull'altra tabella tutto nella stessa transazione.
    Ti posto un pezzo di codice dove devo fare una cosa simile, tieni conto che ADDINSERTPARAMETERS è solo una mia funzione che inserisce i valori

    codice:
       
    
                Dim da As New OleDbDataAdapter
                Dim tr As OleDbTransaction
                Dim cn As New OleDbConnection
                Dim tbl As New DataTable
    
                Dim res As Integer = -1
    
                Try
                    cn.ConnectionString = GetFullDSN()
                    cn.Open()
                    tr = cn.BeginTransaction
                    da.InsertCommand = New OleDbCommand
                    da.InsertCommand.Connection = cn
                    da.InsertCommand.Transaction = tr
                    da.InsertCommand.CommandType = CommandType.Text
    
                    da.InsertCommand.CommandText = "INSERT INTO Prodotti (Codice,Prova) VALUES ('nuovoprodotto',1)"
                    da.InsertCommand.ExecuteNonQuery()
    
                    ''recupero l'ID
                    da.SelectCommand = New OleDbCommand
                    da.SelectCommand.CommandText = "SELECT @@identity as 'Identity'"
                    da.SelectCommand.CommandType = CommandType.Text
                    da.SelectCommand.Connection = cn
                    da.SelectCommand.Transaction = tr
                    da.Fill(tbl)
                    res = tbl.Rows(0).Item("identity")
                    m_ID = res
    
                    ' Inserisco i prezzi
                    Dim item As Integer
                    For item = 0 To m_Prezzo.Length - 1
                        If m_Prezzo(item) <> 0 Then
                            strCols = "(id_prodotto "
                            strValues = "(? "
                            da.InsertCommand.Parameters.Clear()
                            da.InsertCommand.Parameters.Add(New OleDbParameter)
                            da.InsertCommand.Parameters.Item(0).Direction = ParameterDirection.Input
                            da.InsertCommand.Parameters.Item(0).DbType = DbType.Int32
                            da.InsertCommand.Parameters.Item(0).Size = 4
                            da.InsertCommand.Parameters.Item(0).Value = m_ID
                            AddInsertParameters(-1, "Prezzo", 8, strCols, strValues, da, m_Prezzo(item), True)
                            AddInsertParameters(-1, "id_listino", 4, strCols, strValues, da, item)
                            da.InsertCommand.CommandText = "INSERT INTO ListinoPrezzi " & strCols & ") VALUES " & strValues & ")"
                            da.InsertCommand.ExecuteNonQuery()
                        End If
                    Next
                    tr.Commit()
                Catch err As Exception
                    tr.Rollback()
                    Throw err
                Finally
                    da = Nothing
    
                End Try

  5. #5
    grazie Michela-r, ma ormai ho risolto.
    Si trattava di spostare un pò la sequenza dei comandi e ho risolto
    Frate Priore: "È Lucifero in persona!"
    Trinità: "Lo conosci?"
    Bambino: "Mai sentito nominare, deve essere un professionista dell'est"

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.