Solved

Creating SQL Trigger via Code

Posted on 2004-09-14
5
221 Views
Last Modified: 2010-04-23
Does anyone know how to manage trigger via code, I'm more interested in being able to delete and create triggers from within my application (VB.Net).

VBSMerlin
0
Comment
Question by:VBSMerlin
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:RacinRan
ID: 12057206
If it's SQL Server ... send the database drop trigger and create trigger statements.

Racin


0
 
LVL 1

Author Comment

by:VBSMerlin
ID: 12057967
but how would i send that from code just as a regular sql command??? or is there something in the framework that can take care of that?

VBS
0
 
LVL 3

Expert Comment

by:RacinRan
ID: 12058016
Should be able to just send as a regular sql command.
0
 
LVL 1

Author Comment

by:VBSMerlin
ID: 12058076
RacinRan,  I will leave this open for about 2 hours to see if i get any other comment otherwise the points are yours

VBSMerlin
0
 
LVL 7

Accepted Solution

by:
natloz earned 250 total points
ID: 12058312
Here is a code snippet...

Dim cDB As clsDBConn = New clsDBConn 'Connection Class
        Dim strConn As String = cDB.getStrConn 'Get connection string for database
        Dim oConn As New SqlConnection 'SQL connection object
        Dim intReturnID As Integer 'Return ID from Stored Procedures
        Dim oComm As SqlCommand 'SQL Command object

        Try
            'Pass connection string to SqlConnection
            oConn.ConnectionString = strConn

            'SQL Database statements
            oComm = New SqlCommand
            oComm.CommandType = CommandType.Text
            oComm.CommandText = "drop trigger trg_test"
            oComm.Connection = oConn

            oConn.Open() 'Open connection
            oComm.ExecuteNonQuery() 'Execute SQL Statement to drop trigger
            oConn.Close() 'Close connection

        Catch ex As Exception
            MsgBox(ex.ToString)
            MsgBox("Error connecting to SQL Server")
        End Try

        'Return the ID added or ERROR Code
        Return intReturnID
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question