| | |
How to use trigger in C#
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 11
Reputation:
Solved Threads: 0
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 -
Can anyone help me how can I use this trigger and query in C#.
Thanx in advance with lots of expectation from this forum
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 -
C# Syntax (Toggle Plain Text)
Create trigger tr1 on student after update as begin 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 end 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
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
1
#2 Oct 31st, 2009
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.
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.
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 1
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.
CREATE TRIGGER:
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
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.
CREATE TRIGGER:
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
1
#5 Nov 2nd, 2009
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.
![]() |
Other Threads in the C# Forum
- Previous Thread: import data from sql to excel
- Next Thread: Image in DataGridView
Views: 563 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client code color control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms ftp function gcd gdi+ http httpwebrequest image index input java list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving server sleep socket sql statistics stream string table textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





