将值从SQL传递到WCF

时间:2012-01-23 04:46:31

标签: c#

使用CLR Trigger将值传递给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()]

    [Microsoft.SqlServer.Server.SqlTrigger(Name = "WCFTrigger",
       Target = "tbCR", Event = "FOR UPDATE, 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 = reader[1].ToString();
                reader.Dispose();
                myclient.InsertOccured(Name);
                }
            }
        }

    }
}

接口

namespace SampleService
{
    [ServiceContract]
    interface IServiceContract
    {
        [OperationContract]
        void UpdateOccured();
        [OperationContract]
        void InsertOccured(String Name);
    }
}

合同

namespace SampleService
{
    class MyService : IServiceContract
    {
         public void InsertOccured(string Name)
        {
            Console.WriteLine("Insert Occured",Name);
        }

    }
}

每次我插入记录时,WCF只会显示“Insert Occured”,但我期待“插入Occured,Test”。

你能否指导一下我想从SLQ Tirgger获得恒定价值的事情。

1 个答案:

答案 0 :(得分:1)

在您的服务操作中,实现看起来需要将字符串参数添加到Console.WriteLine()字符串模板中。换句话说:

Console.WriteLine("Insert Occured, {0}",Name);

您可以在此处找到有关Console.WriteLine和复合格式的更多信息: http://msdn.microsoft.com/en-us/library/828t9b9h.aspx