472,353 Members | 1,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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.SqlTypes;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using System.Xml;
using System.IO;
//using System.Transactions;
public partial class Triggers
{
// Enter existing table or view for the target and uncomment the
attribute line
[Microsoft.SqlServer.Server.SqlTrigger(Name = @"firstTrigger",
Target = "dbo.CrossSell", Event = "FOR Insert")]
public static void firstTrigger()
{
Guid CrossSellId;
int int_id;
Guid ProductId;
Guid CrossSellingId;
SqlCommand command;
SqlTriggerContext triggContext = SqlContext.TriggerContext;
//string st = triggContext.EventData.Value;
// XmlDocument xmlDoc = new XmlDocument();
//xmlDoc.LoadXml(st);
SqlPipe pipe = SqlContext.Pipe;
SqlDataReader reader;
// DataTable dt = new DataTable();
//SqlDataAdapter da = new SqlDataAdapter();
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);
SqlDataAdapter da = new
SqlDataAdapter("select*from inserted ",connection);
DataTable dt = new DataTable();
da.Fill(dt);
StringWriter writer = new StringWriter();
dt.WriteXml(writer,XmlWriteMode.WriteSchema,false) ;
string xmlFromDataTable = writer.ToString();
reader = command.ExecuteReader();
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,ProductId,CrossSellingId) " +
//////"VALUES (@CrossSellId,
@int_id,@ProductId,@CrossSellingId)", connection);
command = new SqlCommand(
@"INSERT [dbo].[CrossSell] VALUES ("
+ CrossSellId + @", " + int_id + @"," + ProductId +
@"," + CrossSellingId + @");",
connection);
pipe.Send(command.CommandText);
command.ExecuteNonQuery();
pipe.Send(xmlFromDataTable);
//pipe.Send("CrossSell inserted!");
//connection.Open();
//da.Fill(dt);
//
connection.Close();
}
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 5631
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.com

"anu b" <an*******@gmail.comwrote in message
news:e3**********************************@v22g2000 pro.googlegroups.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.SqlTypes;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using System.Xml;
using System.IO;
//using System.Transactions;
public partial class Triggers
{
// Enter existing table or view for the target and uncomment the
attribute line
[Microsoft.SqlServer.Server.SqlTrigger(Name = @"firstTrigger",
Target = "dbo.CrossSell", Event = "FOR Insert")]
public static void firstTrigger()
{
Guid CrossSellId;
int int_id;
Guid ProductId;
Guid CrossSellingId;
SqlCommand command;
SqlTriggerContext triggContext = SqlContext.TriggerContext;
//string st = triggContext.EventData.Value;
// XmlDocument xmlDoc = new XmlDocument();
//xmlDoc.LoadXml(st);
SqlPipe pipe = SqlContext.Pipe;
SqlDataReader reader;
// DataTable dt = new DataTable();
//SqlDataAdapter da = new SqlDataAdapter();
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);
SqlDataAdapter da = new
SqlDataAdapter("select*from inserted ",connection);
DataTable dt = new DataTable();
da.Fill(dt);
StringWriter writer = new StringWriter();
dt.WriteXml(writer,XmlWriteMode.WriteSchema,false) ;
string xmlFromDataTable = writer.ToString();
reader = command.ExecuteReader();
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,ProductId,CrossSellingId) " +
//////"VALUES (@CrossSellId,
@int_id,@ProductId,@CrossSellingId)", connection);
command = new SqlCommand(
@"INSERT [dbo].[CrossSell] VALUES ("
+ CrossSellId + @", " + int_id + @"," + ProductId +
@"," + CrossSellingId + @");",
connection);
pipe.Send(command.CommandText);
command.ExecuteNonQuery();
pipe.Send(xmlFromDataTable);
//pipe.Send("CrossSell inserted!");
//connection.Open();
//da.Fill(dt);
//
connection.Close();
}
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.comwrote in
message news:eE**************@TK2MSFTNGP05.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.com

"Jeff Johnson" <i.***@enough.spamwrote in message
news:u6**************@TK2MSFTNGP03.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:eE**************@TK2MSFTNGP05.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.comwrote in
message news:eE**************@TK2MSFTNGP05.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.dkwrote in message
news:49***********************@news.sunsite.dk...
>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.dkwrote in message
news:49***********************@news.sunsite.dk...
>>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.dkwrote in message
news:49***********************@news.sunsite.dk...
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
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...
2
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 ...
0
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...
8
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...
2
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...
2
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...
1
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...
2
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,...
1
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 ...
2
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.