Exemplo n.º 1
0
        public static void NewMember()
        {
            SqlCommand     command     = null;
            DataSet        ds          = null;
            SqlDataAdapter dataAdapter = null;

            try
            {
                SqlTriggerContext triggContext = SqlContext.TriggerContext;
                switch (triggContext.TriggerAction)
                {
                case TriggerAction.Insert:
                    // Retrieve the connection that the trigger is using
                    using (SqlConnection connection
                               = new SqlConnection(@"context connection=true"))
                    {
                        connection.Open();
                        command = new SqlCommand(@"SELECT * FROM INSERTED;",
                                                 connection);
                        dataAdapter = new SqlDataAdapter(command);
                        ds          = new DataSet();
                        dataAdapter.Fill(ds);

                        DataTable            table   = ds.Tables[0];
                        DataRow              row     = table.Rows[0];
                        DataColumnCollection columns = table.Columns;

                        string TargetEmailAddress = (string)row["Email"];

                        string subject = Templates.NewMemberSubject;
                        string body    = Templates.NewMemberBody;
                        subject = MergeHelper.GenericMerge(subject, columns, row);
                        body    = MergeHelper.GenericMerge(body, columns, row);

                        MailHelper.SendEmail(TargetEmailAddress, subject, body);
                    }

                    break;
                }
            }
            catch { }
            finally
            {
                try
                {
                    if (command != null)
                    {
                        command.Dispose();
                    }

                    if (ds != null)
                    {
                        ds.Dispose();
                    }

                    if (dataAdapter != null)
                    {
                        dataAdapter.Dispose();
                    }
                }
                catch { }
            }
        }