June 3, 2009 at 2:16 am
Hi,
I have to crate triggers within my c# program with my SqlConnection and SqlCommand object.
But when I use the cmd.ExecuteNonQuery with:
[font="Courier New"]CREATE TRIGGER [dbo].[trg_insert_mytable}]
ON [dbo].[myTable]
AFTER INSERT
AS
BEGIN
...
END[/font]
I get the message, that the object 'myTable' is not available. It looks like the statement is not executed within the right context so that the table is not visible.
The table exists and was just created within my program. Why is the tyble not reachable when I was able to create it in the "same" (?) context of my database???
Any ideas?
cew3
June 3, 2009 at 8:04 am
You have to use one command object to create the table before you can run the trigger code that will use it because ADO.NET is not connected to SQL Server all the time.
Kind regards,
Gift Peddie
June 4, 2009 at 1:37 am
I resolved the prolem:
I had to send a 'USE [myDatabase]' befor the 'Create Trigger...'
Solution:
[font="Courier New"]cmd.CommatText = "USE [myDatabase]";
cmd.ExecuteNonQuery();
cmd.CommatText = "CREATE TRIGGER ....";
cmd.ExecuteNonQuery();[/font]
---
Thanks for your reply!
cew3
June 5, 2009 at 8:30 am
So your problem was point of connection instead of delayed execution, glad you have solved your problem.
Kind regards,
Gift Peddie
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy