Somehow, I couldn’t find out how to properly pass the data when a new record gets inserted, am sending the first column value back to WCF. Everything works, but I couldn’t pass the data. What am I missing?
CLR Trigger to call WCF:
public partial class Triggers
{
public static EndpointAddress endpoint = new EndpointAddress(new Uri("http://localhost:8000/services/myservice"));
public static WSHttpBinding httpBinding = new WSHttpBinding();
public static ServiceClient.ServiceReference1.ServiceContractClient myclient = new ServiceClient.ServiceReference1.ServiceContractClient(httpBinding, endpoint);
public delegate void MyDelagate(String crudType);
[SqlProcedure()]
public static void SendData(String crudType)
{
myclient.UpdateOccured();//i was trying to pass the crudType value here
}
[Microsoft.SqlServer.Server.SqlTrigger(Name = "WCFTrigger", Target = "tbCR", Event = "FOR INSERT")]
public static void Trigger1()
{
SqlCommand cmd;
SqlTriggerContext myContext = SqlContext.TriggerContext;
SqlPipe pipe = SqlContext.Pipe;
SqlDataReader reader;
if(myContext.TriggerAction== TriggerAction.Insert)
{
using (SqlConnection conn = new SqlConnection(@"context connection=true"))
{
conn.Open();
cmd = new SqlCommand(@"SELECT * FROM tbCR", conn);
reader = cmd.ExecuteReader();
reader.Read();
//get the insert value's here
string Name;
Name = (string)reader[1];
reader.Dispose();
switch (myContext.TriggerAction)
{
case TriggerAction.Update:
SendData(Name);
break;
case TriggerAction.Insert:
SendData(Name);
break;
}
}
}
}
}
My Service Contract:
namespace SampleService
{
class MyService : IServiceContract
{
public void UpdateOccured()
{
Console.WriteLine("Update Occured");
}
public void InsertOccured(string Name)
{
Console.WriteLine("Insert Occured",Name);
}
}
}
Interface:
namespace SampleService
{
[ServiceContract]
interface IServiceContract
{
[OperationContract]
void UpdateOccured();
[OperationContract]
void InsertOccured(string Name);
}
}
You should be selecting from INSERTED insertead of the actual table i.e.
Also, you should explicitly name the columns that you need instead of using *.
INSERTED and DELETED and SQL Server created, memory-resident tables tables.
More details