C# 过程或函数需要参数'@ID';,这是没有提供的
我的源代码给了我一个错误: 过程或函数“spGetImageById”需要参数“@Photo_ID”, 没有提供 我的存储过程如下所示:C# 过程或函数需要参数'@ID';,这是没有提供的,c#,sql,.net,stored-procedures,parameters,C#,Sql,.net,Stored Procedures,Parameters,我的源代码给了我一个错误: 过程或函数“spGetImageById”需要参数“@Photo_ID”, 没有提供 我的存储过程如下所示: GO /****** Object: StoredProcedure [dbo].[spGetImageById] Script Date: 2018-01-13 18:14:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER procedure [dbo].[spGetI
GO
/****** Object: StoredProcedure [dbo].[spGetImageById] Script Date: 2018-01-13 18:14:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[spGetImageById]
@Photo_ID int
as
Begin
Select Photo_Data
from tblImages where Photo_ID=@Photo_ID
End
后端代码:
protected void Page_Load(object sender, EventArgs e) {
string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs)) {
SqlCommand cmd = new SqlCommand("spGetImageById", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramId = new SqlParameter()
{
ParameterName = "@Photo_ID",
Value = Request.QueryString["Photo_ID"]
};
cmd.Parameters.Add(paramId);
con.Open();
byte[] bytes = (byte[])cmd.ExecuteScalar();
string strBase64 = Convert.ToBase64String(bytes);
Image1.ImageUrl = "data:Image/png;base64," + strBase64;
con.Close();
}
}
错误发生在以下行中:
“byte[]bytes=(byte[])cmd.ExecuteScalar();”
你知道是什么导致了这个问题吗?我失去了所有希望…确保您从
请求中获得了值。查询字符串[“Photo\u ID”]
。尝试传递硬编码值以进行测试
加上传递参数a有效SqlDbType
SqlParameter paramId = new SqlParameter()
{
ParameterName = "@Photo_ID",
SqlDbType = SqlDbType.Int,
Value = Request.QueryString["Photo_ID"]
};
在
cmd.Parameters上放置一个断点。添加(paramId)
并检查paramId
我会给参数指定一个类型。我想你需要在cmd变量中使用exec
这个词。你是否尝试过将硬编码值传递给@Photo\u ID
是的,我尝试过,另外,我添加了您建议的代码,但错误仍然存在。请尝试使用@KeyPhoto
重命名@Photo\u Id
,尝试使用语句EXEC spGetImageById@Photo\u Id=10在SQL Server上执行SP。如果怀疑存储过程有问题,请独立于应用程序代码进行测试。事实上,在尝试使用应用程序代码调用它之前,应该确保它正常工作。