【问题标题】:Procedure or function expects parameter '@ID', which was not supplied过程或函数需要未提供的参数“@ID”
【发布时间】: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


【解决方案1】:

确保您从Request.QueryString["Photo_ID"] 获得价值。尝试传递硬编码值进行测试。

加上传递参数一个有效的SqlDbType

        SqlParameter paramId = new SqlParameter()
        {
            ParameterName = "@Photo_ID",
            SqlDbType = SqlDbType.Int,
            Value = Request.QueryString["Photo_ID"]
        };

【讨论】:

  • 您是否尝试将硬编码值传递给@Photo_ID
  • 是的,我做到了,而且我添加了您建议的代码,但错误仍然存​​在。
  • 尝试用@KeyPhoto重命名@Photo_Id
  • 尝试使用语句EXEC spGetImageById @Photo_ID = 10在 SQL Server 上执行您的 SP
  • 如果您怀疑存储过程有问题,请独立于您的应用程序代码对其进行测试。事实上,在尝试使用应用程序代码调用它之前,您应该确保它正常工作。
猜你喜欢
  • 1970-01-01
  • 2011-02-05
  • 2013-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多