我正在用C#创建一个Web服务:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public void upd(string id, string upddate,string pm)
{
SqlCommand cmd = new SqlCommand("updcsuv", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
SqlParameter[] param =
{
new SqlParameter("@id",id),
new SqlParameter("@upddate",upddate),
new SqlParameter("@username",pm)
};
cmd.Parameters.AddRange(param);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
并且Web服务的存储过程是
CREATE proc updcsuv
(@id int,
@upddate datetime,
@username varchar(30))
as
begin
update csuv
set date = @upddate,
UserName = @username,
where
id = @id
end
我在网络服务中收到错误,我不明白为什么错误发生,因为我认为我的网络服务是正确的,我正确地传递了所有参数
这是错误
System.InvalidOperationException:缺少参数:upddate。
在System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection集合)
在System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
在System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
在System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
答案 0 :(得分:1)
就像你说的那样,你的问题行是Host *
ForwardX11Trusted yes
ForwardAgend yes
ForwardX11 yes
所以你的查询参数中指定new SqlParameter("@upddate",upddate).
是upddate
类型。但你正在给它字符串。所以这可能会产生问题,我想你的字符串是空的,你忘记添加你之前创建的datetime
。
DateTime
修改强>
如果你想要更新日期,你应该写这样:
if (upddate.ToString() == "")
{
dt = Convert.ToDateTime("01-01-1900", enGB);
}
else
{
dt = Convert.ToDateTime(date, enGB);
}
//when you go to the params, set dt for upddate instead of your string.
SqlParameter[] param =
{
new SqlParameter("@id",id),
new SqlParameter("@upddate",dt),
....
}