15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Triggers ❘ 971<br />

In this example, the associated table is Person.Contact, which is indicated by the Target property. The<br />

Event property defines when the trigger should occur; here, the event string is set to FOR INSERT, which<br />

means the trigger is started when a new row is inserted in the Users table.<br />

The property SqlContext.TriggerContext returns the trigger context in an object of type<br />

SqlTriggerContext. The SqlTriggerContext class offers three properties:<br />

➤<br />

➤<br />

➤<br />

ColumnsUpdated returns a Boolean array to flag every column that was changed<br />

EventData contains the new <strong>and</strong> the original data of an update in XML format<br />

TriggerAction returns an enumeration of type TriggerAction to mark the reason for the trigger<br />

The following code compares whether the TriggerAction of the trigger context is set to TriggerAction.<br />

Insert before continuing.<br />

Triggers can access temporary tables; for example, in the following code listing the INSERTED table<br />

is accessed. With INSERT, UPDATE, <strong>and</strong> DELETE SQL statements, temporary tables are created. The<br />

INSERT statement creates an INSERTED table; the DELETE statement creates a DELETED table. With<br />

the UPDATE statement both INSERTED <strong>and</strong> DELETED tables are used. The temporary tables have the same<br />

columns as the table that is associated with the trigger. The SQL statement SELECT Username, Email<br />

FROM INSERTED is used to access username <strong>and</strong> e-mail, <strong>and</strong> to check the e-mail address for correct syntax.<br />

SqlComm<strong>and</strong>.ExecuteRow() returns a row represented in a SqlDataRecord. Username <strong>and</strong> e-mail are read<br />

from the data record. Using the regular expression class, RegEx, the expression used with the IsMatch()<br />

method checks if the e-mail address conforms to valid e-mail syntax. If it does not conform, an exception is<br />

thrown <strong>and</strong> the record is not inserted, because a rollback occurs with the transaction:<br />

using System;<br />

using System.Data.SqlClient;<br />

using System.Text.RegularExpressions;<br />

using Microsoft.SqlServer.Server;<br />

public partial class Triggers<br />

{<br />

[SqlTrigger(Name ="InsertContact", Target="Person.Contact",<br />

Event="FOR INSERT")]<br />

public static void InsertContact()<br />

{<br />

SqlTriggerContext triggerContext = SqlContext.TriggerContext;<br />

if (triggerContext.TriggerAction == TriggerAction.Insert)<br />

{<br />

var connection = new SqlConnection("Context Connection=true");<br />

var comm<strong>and</strong> = new SqlComm<strong>and</strong>();<br />

comm<strong>and</strong>.Connection = connection;<br />

comm<strong>and</strong>.Comm<strong>and</strong>Text = "SELECT EmailAddress FROM INSERTED";<br />

connection.Open();<br />

string email = (string)comm<strong>and</strong>.ExecuteScalar();<br />

connection.Close();<br />

}<br />

}<br />

}<br />

if (!Regex.IsMatch(email,<br />

@"([\w-]+\.)*[\w-]+@[\w-]+\.([\w-]+\.)*[\w]+$"))<br />

{<br />

throw new FormatException("Invalid email");<br />

}<br />

code snippet SqlSamplesUsingAdventureWorks/InsertContact.cs<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!