Dear All

I am new in ADO.Net and i am using C#. My problem is I want to execute one trigger in my program I am having name of student in my student table now from my C# program I will pass any name which is in table from textbox and that name should go to the trigger through update query. Here is the trigger and Update query -

Create trigger tr1 on student
after update as
update m set m.due_amount=m.due_amount-d.st_pay 
from master as m inner join inserted as d on d.st_name = m.st_name 
update student set st_pay=500
where st_name='ram'

Can anyone help me how can I use this trigger and query in C#.

Thanx in advance with lots of expectation from this forum

Recommended Answers

All 5 Replies

You cannot directly call a trigger from C#. What you might be able to do is:

1) Separate your trigger code into a stored procedure that can be called by the trigger, and also by the application (c#).

2) Do something from the application that would cause the DB to fire the trigger. This way is kind of kludgee way to do it.

commented: thats how I would do it +6

A trigger is procedure that initiates on INSERT, DELETE or UPDATE actions. Before SQL Server 2000 Triggers are also used to maintain the referential integrity. We can not execute triggers explicitly; the DBMS automatically fires the trigger when data modification events (INSERT, DELETE or UPDATE) happened in the associated table.
Triggers are same as stored procedures in term of procedural logic that is stored at the database level. Stored procedures are executed explicitly and triggers are event-drive.
Triggers can also execute stored procedures.

Creates a DML, DDL, or logon trigger. A trigger is a special kind of stored procedure that automatically executes when an event occurs in the database server. DML triggers execute when a user tries to modify data through a data manipulation language (DML) event. DML events are INSERT, UPDATE, or DELETE statements on a table or view.
DDL triggers execute in response to a variety of data definition language (DDL) events. These events primarily correspond to Transact-SQL CREATE, ALTER, and DROP statements, and certain system stored procedures that perform DDL-like operations. Logon triggers fire in response to the LOGON event that is raised when a user sessions is being established. Triggers can be created directly from Transact-SQL statements or from methods of assemblies that are created in the Microsoft .NET Framework common language runtime (CLR) and uploaded to an instance of SQL Server. SQL Server allows for creating multiple triggers for any specific statement

sir,I want to use a trigger in C# ado .net code not in the sql statement
how to use trigger in code. sir give a best solution...

You don't directly use triggers in a C# app. If you have a trigger on update for the Student table it will be triggered when you send an update statement from your code. If you want the db to do jobs before an update put this code in a stored procedure as this is not what triggers are designed to do.

thanks sir it is done

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.