不知何故,我無法找到在新記錄插入時如何正確傳遞數據,並將第一列值發送回WCF。一切正常,但我無法傳遞數據。我錯過了什麼?將CLR觸發器的值傳遞給WCF
CLR觸發調用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;
}
}
}
}
}
我的服務合同:
namespace SampleService
{
class MyService : IServiceContract
{
public void UpdateOccured()
{
Console.WriteLine("Update Occured");
}
public void InsertOccured(string Name)
{
Console.WriteLine("Insert Occured",Name);
}
}
}
接口:
namespace SampleService
{
[ServiceContract]
interface IServiceContract
{
[OperationContract]
void UpdateOccured();
[OperationContract]
void InsertOccured(string Name);
}
}
契約接口顯式指定'UpdateOccured'不帶任何參數。 – arootbeer
Ooops對不起,這是假設爲InsertOccured的arootbeet。 – Usher
@ arootbeer,改爲myclient.InsertOccured(curdType);但它引發了一種說法,即在當前上下文中不存在「curdType」。 – Usher