A trigger is a special type of stored procedure that automatically runs when a language event executes. SQL Server includes two general types of triggers: data manipulation language (DML) and data definition language (DDL) triggers. We can create trigger in VS 2005. If we want to keep a table unchanging in the database, we can use Transaction. Before we use it, we should add System.Transactions to the reference.
try
{
// Get the current transaction and roll it back.
Transaction trans = Transaction.Current;
trans.Rollback();
}
catch (SqlException ex)
{
// Catch the expected exception.
}
It is easy to create the Rollback Transaction trigger. When we test the trigger, there are an error throwed. If we want to get the error, we should use the TRY...CATCH new feature in SQL server 2005. The code is below.
BEGIN TRY
SQL Statement
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNum, ERROR_MESSAGE() AS ErrorMessage
END CATCH