【发布时间】:2018-06-22 21:50:23
【问题描述】:
我的源代码给了我一个错误:
过程或函数“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();".
您知道导致问题的原因吗?我已经失去了所有的希望......
【问题讨论】:
-
在
cmd.Parameters.Add(paramId)上设置断点并检查paramId -
我会给参数一个类型。
-
我认为你需要在你的 cmd 变量中使用
exec这个词。
标签: c# sql .net stored-procedures parameters