如何通过 json 网络服务接受参数(包括图像)并将其存储到 SQL 服务器数据库中?
How to accept parameters (including images) through a json web service and store it into a SQL Server database?
我遇到一个错误:
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.
我的代码:
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
是图片 谁能告诉我这是接受图片的正确方式吗?
你的服务可以是这样的
[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());
}
我遇到一个错误:
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.
我的代码:
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
是图片 谁能告诉我这是接受图片的正确方式吗?
你的服务可以是这样的
[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());
}