Storing WCF rest request data with SQL Server stored procedure

点点圈 提交于 2019-12-24 03:05:02

问题


I have a simple interface in my WCF service with one method which gets a myRequest parameter

public interface IService
{
    [OperationContract]
    string MyOperation(myRequest request);
}

When I'm posting the data from the client, the content type is application/json, so the request body auto deserialise into myRequest object.

myRequest is a WCF DataContract:

[DataContract]
public class myRequest
{
    string id;
    string name;
    List<Phone> phones

    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string ID { get; set; }
    [DataMember]
    public List<Phone> Phones { get; set; }
}

and the Phone class

public class Phone
{
    public string Number { get; set; }
    public string Type { get; set; }

    public override string ToString()
    {
        return "[" + Number + "," + Type + "]";
    }
}

Ok, so now I would like to store the request data into my local SQL Server database and for that I need to use stored procedures. I want to send the myRequest object to SQL Server and let it deal with insert/update (I think what I will have to keep 2 tables: people and phones).

I don't have to much background about stored procedures but I will great to get from you guys the right approach to solve this.

Thanks.


回答1:


There's no way you can pass a complex object to the db, and it does not matter if you use a CLR stored proc, an old plain stored proc or something else.

You'll need to transform your request into something that can be passed to the db. An XML serialisation is a good candidate for the job, if you know how to manipulate XML in T-Sql (which could be a little tricky).

I think abandoning the idea of using stored procedures and embracing something else (like EF) would be better for you.



来源:https://stackoverflow.com/questions/30128039/storing-wcf-rest-request-data-with-sql-server-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!