473,763 Members | 6,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clr trigger for insert

Hi
I need to use Clr trigger for insert command
My code is as below
I am using SQL server 2005 and VS 2008.... but after running this
code
i didnt get the result as i expexted it shows the result as no row
is
effected ...Please help me guys

using System;
using System.Data;
using System.Data.Sql ;
using System.Data.Sql Types;
using System.Data.Sql Client;
using Microsoft.SqlSe rver.Server;
using System.Xml;
using System.IO;
//using System.Transact ions;
public partial class Triggers
{
// Enter existing table or view for the target and uncomment the
attribute line
[Microsoft.SqlSe rver.Server.Sql Trigger(Name = @"firstTrigger" ,
Target = "dbo.CrossSell" , Event = "FOR Insert")]
public static void firstTrigger()
{
Guid CrossSellId;
int int_id;
Guid ProductId;
Guid CrossSellingId;
SqlCommand command;
SqlTriggerConte xt triggContext = SqlContext.Trig gerContext;
//string st = triggContext.Ev entData.Value;
// XmlDocument xmlDoc = new XmlDocument();
//xmlDoc.LoadXml( st);
SqlPipe pipe = SqlContext.Pipe ;
SqlDataReader reader;
// DataTable dt = new DataTable();
//SqlDataAdapter da = new SqlDataAdapter( );
switch (triggContext.T riggerAction)
{
case TriggerAction.I nsert:
// Retrieve the connection that the trigger is using
using (SqlConnection connection
= new SqlConnection(@ "context connection=true "))
{
connection.Open ();
command = new SqlCommand(@"SE LECT * FROM
INSERTED;",
connection);
SqlDataAdapter da = new
SqlDataAdapter( "select*fro m inserted ",connectio n);
DataTable dt = new DataTable();
da.Fill(dt);
StringWriter writer = new StringWriter();
dt.WriteXml(wri ter,XmlWriteMod e.WriteSchema,f alse);
string xmlFromDataTabl e = writer.ToString ();
reader = command.Execute Reader();
reader.Read();
CrossSellId = (Guid)reader[0];
int_id = (int)reader[1];
ProductId = (Guid)reader[2];
CrossSellingId = (Guid)reader[3];
reader.Close();
////// command = new SqlCommand(
//////"INSERT into CrossSell (CrossSellId,
int_id,ProductI d,CrossSellingI d) " +
//////"VALUES (@CrossSellId,
@int_id,@Produc tId,@CrossSelli ngId)", connection);
command = new SqlCommand(
@"INSERT [dbo].[CrossSell] VALUES ("
+ CrossSellId + @", " + int_id + @"," + ProductId +
@"," + CrossSellingId + @");",
connection);
pipe.Send(comma nd.CommandText) ;
command.Execute NonQuery();
pipe.Send(xmlFr omDataTable);
//pipe.Send("Cros sSell inserted!");
//connection.Open ();
//da.Fill(dt);
//
connection.Clos e();
}
break;
}
After this i need to update my ProductBase table
DECLARE @ProductBase TABLE (ID int IDENTITY(1,1), CrossSell xml)
INSERT INTO @ProductBase
DEFAULT VALUES
SELECT * FROM @ProductBase
Update @ProductBase
set CrossSell = ' '
where CrossSell IS NULL
SELECT * FROM @ProductBase
then it shows the updated result...
This what i did ....
but after debugging the clr trigger it shows no rows are effected i
dont kknw what is the problem with it..i am new to this...thanks in
advance for your help
Nov 11 '08 #1
7 5793
While this isn't the answer to your question, I have to ask, why aren't
you doing this in T-SQL? You are most definitely going to get better
performance using T-SQL, and it would probably be MUCH easier to code.

Specifically, regarding your problem, I imagine the insert statement is
incorrect. You are creating an insert statement which is open to SQL
injection attacks, as you are trying to append the parameters yourself. You
should be using parameterized queries.

If you must create the insert command from scratch, the GUID values
should be in quotes.

But I would strongly recommend doing this in T-SQL. It just doesn't
make sense to do this kind of work in the CLR.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"anu b" <an*******@gmai l.comwrote in message
news:e3******** *************** ***********@v22 g2000pro.google groups.com...
Hi
I need to use Clr trigger for insert command
My code is as below
I am using SQL server 2005 and VS 2008.... but after running this
code
i didnt get the result as i expexted it shows the result as no row
is
effected ...Please help me guys

using System;
using System.Data;
using System.Data.Sql ;
using System.Data.Sql Types;
using System.Data.Sql Client;
using Microsoft.SqlSe rver.Server;
using System.Xml;
using System.IO;
//using System.Transact ions;
public partial class Triggers
{
// Enter existing table or view for the target and uncomment the
attribute line
[Microsoft.SqlSe rver.Server.Sql Trigger(Name = @"firstTrigger" ,
Target = "dbo.CrossSell" , Event = "FOR Insert")]
public static void firstTrigger()
{
Guid CrossSellId;
int int_id;
Guid ProductId;
Guid CrossSellingId;
SqlCommand command;
SqlTriggerConte xt triggContext = SqlContext.Trig gerContext;
//string st = triggContext.Ev entData.Value;
// XmlDocument xmlDoc = new XmlDocument();
//xmlDoc.LoadXml( st);
SqlPipe pipe = SqlContext.Pipe ;
SqlDataReader reader;
// DataTable dt = new DataTable();
//SqlDataAdapter da = new SqlDataAdapter( );
switch (triggContext.T riggerAction)
{
case TriggerAction.I nsert:
// Retrieve the connection that the trigger is using
using (SqlConnection connection
= new SqlConnection(@ "context connection=true "))
{
connection.Open ();
command = new SqlCommand(@"SE LECT * FROM
INSERTED;",
connection);
SqlDataAdapter da = new
SqlDataAdapter( "select*fro m inserted ",connectio n);
DataTable dt = new DataTable();
da.Fill(dt);
StringWriter writer = new StringWriter();
dt.WriteXml(wri ter,XmlWriteMod e.WriteSchema,f alse);
string xmlFromDataTabl e = writer.ToString ();
reader = command.Execute Reader();
reader.Read();
CrossSellId = (Guid)reader[0];
int_id = (int)reader[1];
ProductId = (Guid)reader[2];
CrossSellingId = (Guid)reader[3];
reader.Close();
////// command = new SqlCommand(
//////"INSERT into CrossSell (CrossSellId,
int_id,ProductI d,CrossSellingI d) " +
//////"VALUES (@CrossSellId,
@int_id,@Produc tId,@CrossSelli ngId)", connection);
command = new SqlCommand(
@"INSERT [dbo].[CrossSell] VALUES ("
+ CrossSellId + @", " + int_id + @"," + ProductId +
@"," + CrossSellingId + @");",
connection);
pipe.Send(comma nd.CommandText) ;
command.Execute NonQuery();
pipe.Send(xmlFr omDataTable);
//pipe.Send("Cros sSell inserted!");
//connection.Open ();
//da.Fill(dt);
//
connection.Clos e();
}
break;
}
After this i need to update my ProductBase table
DECLARE @ProductBase TABLE (ID int IDENTITY(1,1), CrossSell xml)
INSERT INTO @ProductBase
DEFAULT VALUES
SELECT * FROM @ProductBase
Update @ProductBase
set CrossSell = ' '
where CrossSell IS NULL
SELECT * FROM @ProductBase
then it shows the updated result...
This what i did ....
but after debugging the clr trigger it shows no rows are effected i
dont kknw what is the problem with it..i am new to this...thanks in
advance for your help


Nov 11 '08 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:eE******** ******@TK2MSFTN GP05.phx.gbl...
While this isn't the answer to your question, I have to ask, why aren't
you doing this in T-SQL? You are most definitely going to get better
performance using T-SQL, and it would probably be MUCH easier to code.
Amen x infinity.

I have YET to see a compelling reason for CLR intergration in SQL Server.
Nov 11 '08 #3
Jeff,

I wouldn't say that. I should be more specific and say that for the
operations that the OP is performing (set operations, moving data from one
table to another), T-SQL is MUCH better at doing this than CLR code.

If the trigger had to do something of a computational nature, then I
could understand the CLR code. In general, when working with data sets,
T-SQL is better, when doing computational operations, the CLR code is
better.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jeff Johnson" <i.***@enough.s pamwrote in message
news:u6******** ******@TK2MSFTN GP03.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:eE******** ******@TK2MSFTN GP05.phx.gbl...
> While this isn't the answer to your question, I have to ask, why
aren't you doing this in T-SQL? You are most definitely going to get
better performance using T-SQL, and it would probably be MUCH easier to
code.

Amen x infinity.

I have YET to see a compelling reason for CLR intergration in SQL Server.

Nov 11 '08 #4
Jeff Johnson wrote:
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:eE******** ******@TK2MSFTN GP05.phx.gbl...
> While this isn't the answer to your question, I have to ask, why aren't
you doing this in T-SQL? You are most definitely going to get better
performance using T-SQL, and it would probably be MUCH easier to code.

Amen x infinity.

I have YET to see a compelling reason for CLR intergration in SQL Server.
It is very relevant every time there is a need to code something
within SQLServer that is "general programming oriented" instead
of "SQL oriented". TSQL is not a very good language for
traditional coding logic. And even though TSQL has some functions,
then .NET has a lot more.

It is also relevant to look at the competition. Oracle, DB2 and
Sybase all has had the ability in many years to write stored
procedures/functions in a traditional programming language
(Java for all 3 - except that Oracle also support .NET on
Windows platform).

Arne
Nov 17 '08 #5
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:49******** *************** @news.sunsite.d k...
>I have YET to see a compelling reason for CLR intergration in SQL Server.

It is very relevant every time there is a need to code something
within SQLServer
And that's where it breaks down for me. Maybe if you're doing some really
complex math and you want it processed on the server while set processing is
also taking place, then fine. But in my line of work (and I guess that's the
key phrase!) I simply cannot justify putting any non-SQL functionality into
my SQL Server. I believe SQL Server should handle RDBMS stuff and external
software should handle computational stuff.
Nov 18 '08 #6
Jeff Johnson wrote:
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:49******** *************** @news.sunsite.d k...
>>I have YET to see a compelling reason for CLR intergration in SQL Server.
It is very relevant every time there is a need to code something
within SQLServer

And that's where it breaks down for me. Maybe if you're doing some really
complex math and you want it processed on the server while set processing is
also taking place, then fine. But in my line of work (and I guess that's the
key phrase!) I simply cannot justify putting any non-SQL functionality into
my SQL Server. I believe SQL Server should handle RDBMS stuff and external
software should handle computational stuff.
There are two reasons for putting computational stuff in
the database:
* developers that believe in putting the business logic
in stored procedures
* unusual requirements where you need computations as part
of the queries

Arne
Nov 18 '08 #7
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:49******** *************** @news.sunsite.d k...
There are two reasons for putting computational stuff in
the database:
* developers that believe in putting the business logic
in stored procedures
99% of these developers are wrong and should be shot (in their mouse hand).
I bet most of them are of the "I only have a hammer so everything looks like
a nail" variety.
* unusual requirements where you need computations as part
of the queries
This one I can get behind, as long as the requirements are SO unusual that a
TSQL user-defined function doesn't fit the bill.

Basically, I see CLR integration as marketing hype. There's not much more
you can add to SQL itself, so Microsoft had to find something to crow about
when they introduced SQL Server 2005. "CLR Integration! CLR Integration! CLR
Integration!" My biggest fear was that developers would see this feature and
use it because they COULD, not because they SHOULD.

Not that I'm opinionated or anything....
Nov 19 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2737
by: Anton.Nikiforov | last post by:
Dear all, i have a problem with insertion data and running post insert trigger on it. Preambula: there is a table named raw: ipsrc | cidr ipdst | cidr bytes | bigint time | timestamp Triggers:
2
6434
by: 73blazer | last post by:
Perhaps my thinking is wrong but this is what I have: 1 table (Tab1) with 1 attribute (Attr1) Attr1 char(16) for bit data ----------------------------------------------- create trigger check no cascade before insert on Tab1 referencing new as N
0
2477
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as I've found it educational. Note: - I build this for use in a JDEdwards OneWorld environment. I'm not sure how generic others find it but it should be fairly generic. - I use a C stored procedure GETJOBNAME to get some extra audit data,
8
2636
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or something entirely different? The docs only mention regular triggers being executed alfabetically.
2
3341
by: Net Virtual Mailing Lists | last post by:
Hello, If I have a rule like this: CREATE OR REPLACE RULE sometable_update AS ON UPDATE TO table2 DO UPDATE cache SET updated_dt=NULL WHERE tablename='sometable'; CREATE OR REPLACE RULE sometable_insert AS ON INSERT TO table2 DO UPDATE cache SET updated_dt=NULL WHERE tablename='sometable'; CREATE OR REPLACE RULE sometable_delete AS ON DELETE TO table2 DO UPDATE
2
3791
by: mghale | last post by:
Hello, I have to create a trigger to accomplish the following: Before the insert into table A occurs, the trigger must check to see if the combination of two columns (from the insert statement) exist in another domain table. If the combination does not exist the trigger must insert a record into the domain table so that the original insert statement will succeed. I know what you're thinking, why not use RI. Well not my choice so I'm...
1
6210
by: abhi81 | last post by:
Hello All, I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp. Insert and Update trigger work fine when i have only one of them defined. However when I have all the 3 triggers in place and when i try to fire a insert query on the statement. It triggers both insert and update trigger at the same time and...
2
3369
by: wugon.net | last post by:
Problem: after inser trigger encounter error sql0348 Env:db2 v8 + fp 13 + win xp Description: we build two after insert triggers DB2.TRG1, DB2.TRG2 on base table DB2.TEST1, insert data into DB2.TEST1 encounter error sql0348 but we re-create trigger DB2.TRG2 before DB2.TRG1 and re-insert data
1
2280
by: veasnamuch | last post by:
I have a problem while I create a trigger to my table. My objective is getting any change made to my table and record it in to another table . My have thousands records before I add new trigger to this table, everything work fine means, when I manual insert data into table the last row of inserting is what I am doing but after I add trigger on inserting to this table and I do next manual insert data into the last row in Enterprise Manager, I...
2
3753
by: lenygold via DBMonster.com | last post by:
Hi Everebody: I have a table: CREATE TABLE CROSS_REFERENCE (ROW# INTEGER NOT NULL ,KEY_WORD CHAR(16) NOT NULL ,QUERY_DESCR VARCHAR(330) NOT NULL ,PRIMARY KEY (ROW#,KEY_WORD)); It is a cross reference table to my CATALOG Table based on key words.
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.