Classi SqlClient e autocomit

giovedì 12 aprile 2007 - 09.44

andreapavia Profilo | Senior Member

Ciao a tutti, sto creando un webservice al quale verranno passate n stringhe di insert....
io vorrei che queste che i record inseriti nelle diverse tabelle vengano committati alla fine, solo se tutte le insert non catturano eccezioni....

questo è un metodo che ho trovato su msdn ma non funziona.... o meglio sui db northwind e pubs... non so come farlo funzionare....
quando arriva all'istruzione su pubs
cmdDB2.ExecuteNonQuery()
cattura un'eccezione e mi trovo alla fine del metodo con i dati di northwind inseriti e quelli di pubs no.....
io verrei che quando cattura l'eccezione su pubs facesse la rollback su northwind....

come faccio?????


imports

Imports System.Web.Services
Imports System.Data.SqlClient
Imports System.Data
Imports System.EnterpriseServices


<WebMethod()> Function AddPippo(ByVal strFirst As String, ByVal strLast As String, ByVal strPubsId As String) As String
Dim fff As String
Try

'Insert new employee into Northwind database
Dim strDB1 As String = "Server=localhost;Database=Northwind;Trusted_Connection=Yes"
Dim conDB1 As SqlConnection
Dim cmdDB1 As SqlCommand
Dim strSQL1 As String = "insert into employees " & _
"(firstname, lastname) " & _
"values " & _
"('" & strFirst & "', '" & strLast & "');"

conDB1 = New SqlConnection(strDB1)
conDB1.Open()
cmdDB1 = New SqlCommand(strSQL1, conDB1)
cmdDB1.ExecuteNonQuery()

'Insert new employee into Pubs database
Dim strDB2 As String = "Server=localhost;Database=Pubs;Trusted_Connection=Yes"
Dim conDB2 As SqlConnection
Dim cmdDB2 As SqlCommand
Dim strSQL2 As String = "insert into employee " & _
"(emp_id, fname, lname) " & _
"values " & _
"('" & strPubsId & "', '" & strFirst & "', '" & strLast & "');"

conDB2 = New SqlConnection(strDB2)
conDB2.Open()
cmdDB2 = New SqlCommand(strSQL2, conDB2)
cmdDB2.ExecuteNonQuery()
ContextUtil.SetComplete()
fff = "Commit"
Catch
ContextUtil.SetAbort()
fff = "Rollback: " & Err().Description
End Try
Return fff
End Function

alx_81 Profilo | Guru

>Ciao a tutti
Ciao!
>
>questo è un metodo che ho trovato su msdn ma non funziona....
>o meglio sui db northwind e pubs... non so come farlo funzionare....
>quando arriva all'istruzione su pubs
>cmdDB2.ExecuteNonQuery()
>cattura un'eccezione e mi trovo alla fine del metodo con i dati
>di northwind inseriti e quelli di pubs no.....
perchè manca la transazione.. Infatti devi includere tutte le istruzioni che vuoi gestire in una sola "unità di lavoro" (transazione) per poterle gestire tramite eccezione..

>io verrei che quando cattura l'eccezione su pubs facesse la rollback
>su northwind....
>
>come faccio?????
leggiti questo link
http://msdn2.microsoft.com/en-us/library/2k2hy99x.aspx

Alx81 =)

http://blogs.dotnethell.it/suxstellino

andreapavia Profilo | Senior Member

.... azz.......

è vero..... mi mancava un comando..... io l'ho copiato senza leggere......
grazie 1000.....

alx_81 Profilo | Guru

de nada..
Alx81 =)

http://blogs.dotnethell.it/suxstellino
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5