电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

如何:創建和運行 CLR SQL Server 觸發器

瀏覽:8日期:2023-11-07 09:58:55

通過向 SQL Server 項目添加“觸發器”創建 SQL 觸發器。成功部署后,就可以像其他任何 T-SQL 觸發器一樣調用和執行在托管代碼中創建的觸發器。使用托管語言編寫的觸發器可以使用 SqlTriggerContext 類獲得對相關信息的訪問,這些信息與 T-SQL 觸發器的可用信息相同。

注意; 在默認情況下,Microsoft SQL Server 中關閉了公共語言運行庫 (CLR) 集成功能。必須啟用該功能才能使用 SQL Server 項目項。若要啟用 CLR 集成,請使用 sp_configure 存儲過程的“啟用 clr”選項。有關更多信息,請參見 啟用 CLR 集成。注意; 顯示的對話框和菜單命令可能會與幫助中的描述不同,具體取決于您現用的設置或版本。若要更改設置,請在“工具”菜單上選擇“導入和導出設置”。有關更多信息,請參見 Visual Studio 設置。

創建 SQL Server 觸發器創建 SQL Server 觸發器打開一個現有的“SQL Server 項目”,或者創建一個新項目。有關更多信息,請參見 如何:創建 SQL Server 項目。

從“項目”菜單中選擇“添加新項”。

在 “添加新項”對話框 中選擇“觸發器”。

鍵入新觸發器的“名稱”。

添加觸發器執行時要運行的代碼。請參見下面的第一個示例。

注意; C++ 示例在編譯時必須使用 /clr:safe 編譯器選項。

對于 Visual Basic 和 Visual C#,在“解決方案資源管理器”中,打開“TestScripts”文件夾,再雙擊“Test.sql”文件。

對于 Visual C++,在“解決方案資源管理器”中,雙擊“debug.sql”文件。

將代碼添加到“Test.sql”(Visual C++ 中為“debug.sql”)文件中以執行觸發器。請參見下面的第二個示例。

按 F5 生成、部署并調試此觸發器。有關不進行調試直接部署的信息,請參見 如何:將 SQL Server 項目項部署到 SQL Server 中。

在 “輸出”窗口 中查看結果,然后選擇“從此處顯示輸出:數據庫輸出”。

示例此示例演示以下這種情況:用戶選擇他們需要的任何用戶名,但是您希望知道哪些用戶輸入了電子郵件地址作為用戶名。此觸發器檢測該信息并將它記錄到審核表。

Visual Basic 復制代碼Imports System.Data.SqlClientImports System.Text.RegularExpressionsImports Microsoft.SqlServer.Server

Partial Public Class Triggers

<SqlTrigger(Name:='UserNameAudit', Target:='Users', Event:='FOR INSERT')> _ Public Shared Sub UserNameAudit()

Dim triggContext As SqlTriggerContext = SqlContext.TriggerContext() Dim userName As New SqlParameter('@username', SqlDbType.NVarChar)

If triggContext.TriggerAction = TriggerAction.Insert Then

Using conn As New SqlConnection('context connection=true')

conn.Open() Dim sqlComm As New SqlCommand Dim sqlP As SqlPipe = SqlContext.Pipe()

sqlComm.Connection = conn sqlComm.CommandText = 'SELECT UserName from INSERTED'

userName.Value = sqlComm.ExecuteScalar.ToString()

If IsEMailAddress(userName.ToString) Then sqlComm.CommandText = 'INSERT UsersAudit(UserName) VALUES(username)' sqlP.Send(sqlComm.CommandText) sqlP.ExecuteAndSend(sqlComm) End If End Using End If End Sub

Public Shared Function IsEMailAddress(ByVal s As String) As Boolean

Return Regex.IsMatch(s, '^([w-]+.)*?[w-]+@[w-]+.([w-]+.)*?[w]+$') End FunctionEnd ClassC# 復制代碼using System.Data.SqlClient;using System.Text.RegularExpressions;using Microsoft.SqlServer.Server;

public partial class Triggers{ [SqlTrigger(Name='UserNameAudit', Target='Users', Event='FOR INSERT')] public static void UserNameAudit() { SqlTriggerContext triggContext = SqlContext.TriggerContext; SqlParameter userName = new SqlParameter('@username', System.Data.SqlDbType.NVarChar);

if (triggContext.TriggerAction == TriggerAction.Insert) { using (SqlConnection conn = new SqlConnection('context connection=true')) { conn.Open(); SqlCommand sqlComm = new SqlCommand(); SqlPipe sqlP = SqlContext.Pipe;

sqlComm.Connection = conn; sqlComm.CommandText = 'SELECT UserName from INSERTED';

userName.Value = sqlComm.ExecuteScalar().ToString();

if (IsEMailAddress(userName.ToString())) { sqlComm.CommandText = 'INSERT UsersAudit(UserName) VALUES(userName)'; sqlP.Send(sqlComm.CommandText); sqlP.ExecuteAndSend(sqlComm); } } } }

public static bool IsEMailAddress(string s) { return Regex.IsMatch(s, '^([w-]+.)*?[w-]+@[w-]+.([w-]+.)*?[w]+$'); }}C++ 復制代碼#include 'stdafx.h'

#using <System.dll>#using <System.Data.dll>#using <System.Xml.dll>

using namespace System;using namespace System::Data;using namespace System::Data::Sql;using namespace System::Data::SqlClient;using namespace System::Data::SqlTypes;using namespace System::Text::RegularExpressions;using namespace Microsoft::SqlServer::Server;

// In order to debug your Trigger, add the following to your debug.sql file://// -- Insert one user name that is not an e-mail address and one that is// INSERT INTO Users(UserName, Pass) VALUES(N'someone', N'cnffjbeq')// INSERT INTO Users(UserName, Pass) VALUES(N'someone@example.com', N'cnffjbeq')//// -- check the Users and UsersAudit tables to see the results of the trigger// SELECT * FROM Users// SELECT * FROM UsersAudit//

public ref class AddNewTrigger{public: [SqlTrigger(Name='UserNameAudit', Target='Users', Event='FOR INSERT')] static void UserNameAudit() { SqlTriggerContext ^triggContext = SqlContext::TriggerContext; SqlParameter ^userName = gcnew SqlParameter('@username', System::Data::SqlDbType::NVarChar);

if (triggContext->TriggerAction == TriggerAction::Insert) { SqlConnection ^conn = gcnew SqlConnection('context connection=true'); conn->Open(); SqlCommand ^sqlComm = gcnew SqlCommand(); SqlPipe ^sqlP = SqlContext::Pipe;

sqlComm->Connection = conn; sqlComm->CommandText = 'SELECT UserName from INSERTED';

userName->Value = sqlComm->ExecuteScalar()->ToString();

if (IsEMailAddress(userName->ToString())) { sqlComm->CommandText = 'INSERT UsersAudit(UserName) VALUES(userName)'; sqlP->Send(sqlComm->CommandText); sqlP->ExecuteAndSend(sqlComm); }

conn->Close(); } }

static bool IsEMailAddress(String ^s) { return Regex::IsMatch(s, '^([w-]+.)*?[w-]+@[w-]+.([w-]+.)*?[w]+$'); }};

向位于項目的 TestScripts 文件夾中的 Test.sql 文件(Visual C++ 中為 debug.sql)添加代碼以執行和測試您的觸發器。例如,如果已部署了觸發器,您可以通過運行腳本對其進行測試,該腳本向設置了此觸發器的表中插入新行,從而可激發此觸發器。以下調試代碼假定存在具有以下定義的兩個表:

CREATE TABLE Users

(

UserName NVARCHAR(200) NOT NULL,

Pass NVARCHAR(200) NOT NULL

)

CREATE TABLE UsersAudit

(

UserName NVARCHAR(200) NOT NULL

)

復制代碼-- Insert one user name that is not an e-mail address and one that isINSERT INTO Users(UserName, Pass) VALUES(N'someone', N'cnffjbeq')INSERT INTO Users(UserName, Pass) VALUES(N'someone@example.com', N'cnffjbeq')

-- check the Users and UsersAudit tables to see the results of the triggerselect * from Usersselect * from UsersAudit

標簽: Sql Server 數據庫
主站蜘蛛池模板: 济南律师,济南法律咨询,山东法律顾问-山东沃德律师事务所 | 磨煤机配件-高铬辊套-高铬衬板-立磨辊套-盐山县宏润电力设备有限公司 | 电子元器件呆滞料_元器件临期库存清仓尾料_尾料优选现货采购处理交易商城 | 彩超机-黑白B超机-便携兽用B超机-多普勒彩超机价格「大为彩超」厂家 | 水冷散热器_水冷电子散热器_大功率散热器_水冷板散热器厂家-河源市恒光辉散热器有限公司 | 气动调节阀,电动调节阀,自力式压力调节阀,切断阀「厂家」-浙江利沃夫自控阀门 | 呼末二氧化碳|ETCO2模块采样管_气体干燥管_气体过滤器-湖南纳雄医疗器械有限公司 | 定制液氮罐_小型气相液氮罐_自增压液氮罐_班德液氮罐厂家 | 高压绝缘垫-红色配电房绝缘垫-绿色高压绝缘地毯-上海苏海电气 | 专注氟塑料泵_衬氟泵_磁力泵_卧龙泵阀_化工泵专业品牌 - 梭川泵阀 | 并离网逆变器_高频UPS电源定制_户用储能光伏逆变器厂家-深圳市索克新能源 | 壹作文_中小学生优秀满分作文大全| 达利园物流科技集团-| 济南网站建设|济南建网站|济南网站建设公司【济南腾飞网络】【荐】 | pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 | 对夹式止回阀_对夹式蝶形止回阀_对夹式软密封止回阀_超薄型止回阀_不锈钢底阀-温州上炬阀门科技有限公司 | 头条搜索极速版下载安装免费新版,头条搜索极速版邀请码怎么填写? - 欧远全 | 山东聚盛新型材料有限公司-纳米防腐隔热彩铝板和纳米防腐隔热板以及钛锡板、PVDF氟膜板供应商 | 交通信号灯生产厂家_红绿灯厂家_电子警察监控杆_标志杆厂家-沃霖电子科技 | 氨水-液氨-工业氨水-氨水生产厂家-辽宁顺程化工 | 定做大型恒温循环水浴槽-工业用不锈钢恒温水箱-大容量低温恒温水槽-常州精达仪器 | 礼至家居-全屋定制家具_一站式全屋整装_免费量房设计报价 | 上海平衡机-单面卧式动平衡机-万向节动平衡机-圈带动平衡机厂家-上海申岢动平衡机制造有限公司 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 小型数控车床-数控车床厂家-双头数控车床 | 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | 聚天冬氨酸,亚氨基二琥珀酸四钠,PASP,IDS - 远联化工 | 恒温振荡混匀器-微孔板振荡器厂家-多管涡旋混匀器厂家-合肥艾本森(www.17world.net) | 浙江清风侠环保设备有限公司| 北京租车牌|京牌指标租赁|小客车指标出租 | 天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司 | 喷漆房_废气处理设备-湖北天地鑫环保设备有限公司 | 必胜高考网_全国高考备考和志愿填报信息平台 | 充气膜专家-气膜馆-PTFE膜结构-ETFE膜结构-商业街膜结构-奥克金鼎 | 砍排机-锯骨机-冻肉切丁机-熟肉切片机-预制菜生产线一站式服务厂商 - 广州市祥九瑞盈机械设备有限公司 | 防火窗_耐火窗_防火门厂家_防火卷帘门-重庆三乐门业有限公司 | 在线浊度仪_悬浮物污泥浓度计_超声波泥位计_污泥界面仪_泥水界面仪-无锡蓝拓仪表科技有限公司 | 天然气分析仪-液化气二甲醚分析仪|传昊仪器 | 锌合金压铸-铝合金压铸厂-压铸模具-冷挤压-誉格精密压铸 | 超声波清洗机_超声波清洗机设备_超声波清洗机厂家_鼎泰恒胜 | EDLC超级法拉电容器_LIC锂离子超级电容_超级电容模组_软包单体电容电池_轴向薄膜电力电容器_深圳佳名兴电容有限公司_JMX专注中高端品牌电容生产厂家 |