C# 将值从CLR触发器传递到WCF

C# 将值从CLR触发器传递到WCF,c#,sql-server,C#,Sql Server,不知何故,我无法找到在插入新记录时如何正确传递数据,因为我正在将第一列值发送回WCF。一切正常,但我无法传递数据。我错过了什么 调用WCF的CLR触发器: public partial class Triggers { public static EndpointAddress endpoint = new EndpointAddress(new Uri("http://localhost:8000/services/myservice")); public static W

不知何故,我无法找到在插入新记录时如何正确传递数据,因为我正在将第一列值发送回WCF。一切正常,但我无法传递数据。我错过了什么

调用WCF的CLR触发器:

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);
    }
}

您应该从实际表格的插入项中进行选择,即

cmd = new SqlCommand(@"SELECT * FROM INSERTED", conn);
此外,还应显式命名所需的列,而不是使用*

插入和删除以及SQL Server创建的内存驻留表

删除的表存储删除和删除期间受影响行的副本 更新语句。在执行删除或更新期间 语句,则从触发器表中删除行并将其传输到 删除的表。删除的表和触发器表 没有共同的行

插入的表在插入期间存储受影响行的副本 和更新语句。在插入或更新事务期间,新建 行同时添加到插入表和触发器表中。这个 插入表中的行是触发器中新行的副本 桌子

更新事务类似于删除操作,然后执行 插入操作;旧行首先复制到已删除的表中, 然后新行被复制到触发器表和 插入表


谢谢amit,我看到有人建议使用“select*from Inserted”选项,但对此不太确定,所以我使用了查询我的实际表。如果使用Inserted,请更正我,读者仍然可以找到合适的列?是的,就像DML触发器一样,CLR触发器还可以访问这些表,读取我在更新注释中发布的链接。除此之外,我的代码中缺少的内容,它不允许我传递值@myclient.UpdateOccured();我正在尝试执行我的client.UpdateOccured(crudType)@amig_g,现在一切正常,将我的查询更改为您建议的查询。现在我有一个问题,如果在不同的表中插入多条记录,会发生什么?插入的触发器如何知道正在监视哪个表?与DML触发器一样,您必须为每个表写入一个CLR触发器。您发布的仅适用于表tbCR。这被指定为方法-Target=“tbCR”上的属性。合同接口明确指定,
UpdateOccured
不接受任何参数。Ooops对不起,arootbeet,假设它是InsertOccured。@arootbeer,更改为myclient.InsertOccured(curdType);但是它抛出了一个表达式,表示“curdType”在当前上下文中不存在。您知道它为什么说“curdType”不存在吗?您应该用更改来更新您的代码示例。@arootbeet,不,我还没有,它仍然会在myclient.Insertocured(crudType)上引发异常;