不知何故,我无法在插入新记录时找到如何正确传递数据,我将第一列值发送回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);
}
}
答案 0 :(得分:2)
您应该从实际表的INSERTED insertead中选择,即
cmd = new SqlCommand(@"SELECT * FROM INSERTED", conn);
此外,您应该明确命名所需的列,而不是使用*。
INSERTED和DELETED以及SQL Server创建的内存驻留表表。
删除的表存储DELETE期间受影响的行的副本 UPDATE语句。在执行DELETE或UPDATE期间 语句,行从触发器表中删除并传输到 删除的表。删除的表和触发表通常 没有共同的行。
insert表在INSERT期间存储受影响行的副本 和UPDATE语句。在插入或更新事务期间,新的 行被添加到插入的表和触发器表中。该 inserted表中的行是触发器中新行的副本 表
更新事务类似于删除操作后跟一个 插入操作;先将旧行复制到已删除的表中, 然后将新行复制到触发器表和 插表。