如何接受参数(包括图片)通过JSON Web服务,并将其存储到SQL Server数据库? [英] How to accept parameters (including images) through a json web service and store it into a SQL Server database?

查看:158
本文介绍了如何接受参数(包括图片)通过JSON Web服务,并将其存储到SQL Server数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误:

操作在合同'IService1''ADDUSER'具有UriTemplate希望获得命名为'PHONE_NO,DP,REG_ID'参数,但没有与对操作该名称没有输入参数

Operation 'addUser' in contract 'IService1' has a UriTemplate that expects a parameter named 'PHONE_NO,DP,REG_ID', but there is no input parameter with that name on the operation.

我的code:

public interface IService1
{
    [OperationContract]
    void DoWork();

    [OperationContract]
    [WebInvoke(Method = "POST", 
               ResponseFormat = WebMessageFormat.Json, 
               BodyStyle = WebMessageBodyStyle.Wrapped, 
               UriTemplate = "addUser/{phone_no,dp,reg_id}")]
    void addUser(String phone_no, String dp, String reg_id);
}

ADDUSER 功能如下:

 public void addUser(String phone_no, String dp, String reg_id)
 {
        string conn = "";
        conn = ConfigurationManager.ConnectionStrings["Conn"].ToString();

        SqlConnection objsqlconn = new SqlConnection(conn);
        objsqlconn.Open();

        SqlDataAdapter da = new SqlDataAdapter();

        SqlCommand cmd = new SqlCommand("select * from dbo.User where phone_no in (" + phone_no + ")", objsqlconn);
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();

        da.Fill(ds);
        objsqlconn.Close();

        if (ds.Tables[0].Rows.Count == 0)
        {
            objsqlconn.Open();

            SqlCommand cmd2 = new SqlCommand("insert into dbo.User(phone_no,dp,reg_id) values(@phone_no,@dp,@reg_id)", objsqlconn);
            da.SelectCommand = cmd2;
            DataSet dt = new DataSet();
            da.Fill(ds);
            objsqlconn.Close();
        }
   }

下面 DP 是图像可有人告诉我,如果这是正确的方式来接受图像?

Here dp is the image can someone tell me if this is the right way to accept images?

推荐答案

您服务可以是这样的

[ServiceContract]
public class TestService
{
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/AddUser/{phoneNo}/{regId}")]
    public string AddUser(string phoneNo, String regId, Stream image)
    {
       //Read your stream here
       return phoneNo + " " + regId;
    }
}

您可以调用它为:

using (var client = new HttpClient())
{
    var stream = new StreamContent(File.OpenRead(filename));
    var resp = await client.PostAsync("http://ip:port/TestService/AddUser/555111222/12345", stream);
    Console.WriteLine(resp.StatusCode.ToString());
}

这篇关于如何接受参数(包括图片)通过JSON Web服务,并将其存储到SQL Server数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆