Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Creating SQL Trigger via Code

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
VBSMerlin
Asked:
VBSMerlin
  • 2
  • 2
1 Solution
 
RacinRanCommented:
If it's SQL Server ... send the database drop trigger and create trigger statements.

Racin


0
 
VBSMerlinAuthor Commented:
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
 
RacinRanCommented:
Should be able to just send as a regular sql command.
0
 
VBSMerlinAuthor Commented:
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
 
natlozCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now