473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 10 '08 #1
1 2641
On Nov 10, 12:22*pm, anu b <anupam...@gmail.comwrote:
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
As you're asking about SQL and C#, I'd suggest you go to one of those
newsgroups, and not the VB.NET group.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Nov 11 '08 #2

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 ipdst | cidr bytes | bigint time | timestamp...
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 ----------------------------------------------- create trigger...
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 automagically. Hope someone finds this as useful as...
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 executed in order of firing or alfabetically or...
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 tablename='sometable'; CREATE OR REPLACE RULE...
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 combination of two columns (from the insert...
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 the unique key for each table and the timestamp....
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, DB2.TRG2 on base table DB2.TEST1, insert data into...
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 have thousands records before I add new trigger to...
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 NULL ,PRIMARY KEY (ROW#,KEY_WORD)); It is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.