logvendormodification.cs

来自「sqlserver scripts from ch 14 in zip file」· CS 代码 · 共 40 行

CS
40
字号
using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using System.Transactions;

public partial class Triggers
{
    [Microsoft.SqlServer.Server.SqlTrigger(Name = "LogVendorModification", Target = "[Purchasing].[Vendor]", Event = "FOR UPDATE")]
    public static void LogVendorModification()
    {
        using (SqlConnection connection = new SqlConnection(@"context connection=true"))
        {
            SqlCommand command;
            SqlDataReader reader;
            string emailAddress;

            // Open the connection.
            connection.Open();

            // Get the inserted value.
            command = new SqlCommand(@"SELECT PurchasingWebServiceURL FROM INSERTED", connection);
            reader = command.ExecuteReader();
            reader.Read();
            emailAddress = (string)reader[0];
            reader.Close();

            // Rollback the transaction if an invalid email address was inserted.
            if (emailAddress.Length < 4)
            {
                Transaction.Current.Rollback();
            }

            // Close the connection.
            connection.Close();
        }

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?