Re: CLR Trigger Rollback
- From: "Jesús López" <sqlranger.mpv@xxxxxxxx>
- Date: Thu, 12 Oct 2006 22:49:28 +0200
I think you should use a ROLLBACK command instead of
Transaction.Current.Rollback():
using (SqlCommand cmd = new SqlCommand("ROLLBACK", cn))
{
cmd.ExecuteNonQuery();
}
Regards:
Jesús López
"honzas" <jansotola@xxxxxxxxx> escribió en el mensaje
news:1160575233.565903.88760@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Cor Ligthert [MVP] wrote:
Jan,
What am I doing wrong?
There is probably something in your code that does not go right.
OK, my question was quite stupid.
I should have asked "What would I be doing wrong?"
Or, better, I should have attached a source code that is throwing the
exception - so here it is:
1) I've got such a simple class with one method:
---------------------------------------------------
using System;
using System.Data;
using System.Data.Sql;
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace SqlClr
{
public class ClrTriggers
{
[SqlTrigger(Name = @"ti_ble", Target = "[dbo].[ble]", Event =
"FOR INSERT")]
public static void TiBle()
{
using (SqlConnection connection
= new SqlConnection(@"context connection=true"))
{
connection.Open();
SqlCommand command = new SqlCommand(@"SELECT val FROM
INSERTED;", connection);
SqlDataReader reader = command.ExecuteReader();
reader.Read();
string val = (string)reader[0];
reader.Close();
if (val == "wrong")
System.Transactions.Transaction.Current.Rollback();
}
}
}
}
---------------------------------------------------
2) I have compilled it and registered the assembly in my SQL Server
2005 developer edition (running on my localhost).
Than I've created a trigger on a very simple table:
CREATE TABLE ble (id int, val varchar(255))
CREATE TRIGGER ti_ble ON ble FOR INSERT
AS EXTERNAL NAME SqlClr.[SqlClr.ClrTriggers].TiBle
3) At least I've tried to insert a value "wrong" into the "ble" table
(this value should fire rollback inside the clr trigger:
INSERT INTO ble(val) VALUES ('wrong')
And I'm receiving following error message:
Msg 6549, Level 16, State 1, Procedure ti_ble, Line 1
A .NET Framework error occurred during execution of user defined
routine or aggregate 'ti_ble':
System.Data.SqlClient.SqlException: Transaction is not allowed to roll
back inside a user defined routine, trigger or aggregate because the
transaction is not started in that CLR level. Change application logic
to enforce strict transaction nesting.
System.Data.SqlClient.SqlException:
at System.Data.SqlServer.Internal.StandardEventSink.HandleErrors()
at
System.Data.SqlServer.Internal.ClrLevelContext.SuperiorTransaction.Rollback()
at
System.Transactions.TransactionStateSubordinateActive.Rollback(InternalTransaction
tx, Exception e)
at System.Transactions.Transaction.Rollback()
at SqlClr.ClrTriggers.TiBle()
. User transaction, if any, will be rolled back.
The statement has been terminated.
Cor
"honzas" <jansotola@xxxxxxxxx> schreef in bericht
news:1160384994.575251.48140@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
A disccussion on this topic (how to perform rollback from CLR trigger)
went here last year.
(http://groups.google.com/group/microsoft.public.dotnet.framework.adonet/browse_thread/thread/10ff4535253f9241/698222981b1d15b6?lnk=st&q=CLR+trigger+rollback&rnum=1#698222981b1d15b6)
Pablo Castro (and also some MSDN articles) was answering:
Try:
System.Transactions.Transaction.Current.Rollback();
That should do it for your scenario.
--
Pablo Castro
Program Manager - ADO.NET Team
Microsoft Corp.
I tryed it but I'm still recieving following error message:
System.Data.SqlClient.SqlException: Transaction is not allowed to roll
back inside a user defined routine, trigger or aggregate because the
transaction is not started in that CLR level. Change application logic
to enforce strict transaction nesting.
What am I doing wrong?
Jan Sotola.
.
- Follow-Ups:
- Re: CLR Trigger Rollback
- From: honzas
- Re: CLR Trigger Rollback
- References:
- CLR Trigger Rollback
- From: honzas
- Re: CLR Trigger Rollback
- From: Cor Ligthert [MVP]
- Re: CLR Trigger Rollback
- From: honzas
- CLR Trigger Rollback
- Prev by Date: Re: Connection String Problem
- Next by Date: Re: Connection String Problem
- Previous by thread: Re: CLR Trigger Rollback
- Next by thread: Re: CLR Trigger Rollback
- Index(es):
Relevant Pages
|