我的源代码给了我一个错误:
程序或功能' 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].[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();"。
你知道造成这个问题的原因是什么吗?我已经失去了所有希望......
答案 0 :(得分:0)
确保从Request.QueryString["Photo_ID"]
获得价值。尝试传递硬编码值进行测试。
加上传递参数有效SqlDbType
SqlParameter paramId = new SqlParameter()
{
ParameterName = "@Photo_ID",
SqlDbType = SqlDbType.Int,
Value = Request.QueryString["Photo_ID"]
};