简体   繁体   English

什么是“未提供参数化查询...。”错误?

[英]What is “The parameterized query … which was not supplied.” error?

I have experienced an error on 我在遇到错误

"The parametrized query '(@blue nvarchar(4000))SELECT blueBallImage FROM CorrespondingBal' expects the parameter '@blue',
which was not supplied."

I am doing a HttpHandler. 我正在做一个HttpHandler。

I want to retrieve the image from the database. 我想从数据库中检索图像。 My codes are as below. 我的代码如下。

public void ProcessRequest (HttpContext context) {
    string image = context.Request.QueryString["image"];

    SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=MyCloudGames;Integrated Security=True"); 
    SqlCommand cmd = new SqlCommand("SELECT blueBallImage FROM CorrespondingBall WHERE objective = blue", con); 
    cmd.CommandType = CommandType.Text; 
    cmd.Parameters.Add("blue", image); 

    con.Open(); 
    byte[] ballImage = (byte[])cmd.ExecuteScalar(); 
    con.Close(); 

    context.Response.ContentType = "image/png"; 
    context.Response.OutputStream.Write(ballImage, 78, ballImage.Length - 78); 
}

The error occurred at 错误发生在

byte[] ballImage = (byte[])cmd.ExecuteScalar();

Use AddWithValue instead of Add method. 使用AddWithValue而不是Add方法。

SqlCommand cmd = new SqlCommand("SELECT blueBallImage FROM CorrespondingBall WHERE objective = @blue", con); 
cmd.CommandType = CommandType.Text; 
cmd.Parameters.AddWithValue("@blue", image);

You should provide your parameter with @ in your command. 您应该在命令中使用@来提供参数。

Soner Gönül, image is null. SonerGönül,图片为空。 How can I correct it? 我该如何纠正?

So, you should return DBNull.Value if your image is null . 因此,如果imagenull ,则应返回DBNull.Value You can't pass a null on a required parameter. 您不能在必填参数上传递null You can use a method as an alternative like; 您可以将方法用作替代方法;例如:

SqlCommand cmd = new SqlCommand("SELECT blueBallImage FROM CorrespondingBall WHERE objective = @blue", con); 
cmd.CommandType = CommandType.Text; 
cmd.Parameters.AddWithValue("@blue", CheckForDbNull(image));

...

public static object CheckForDbNull(object value)
{
   if(value == null)
   {
       return DBNull.Value;
   }

   return value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未提供的参数化查询。 实体框架 - The parameterized query which was not supplied. Entity Framework {“参数化查询'(@Usr_Name nvarchar(4000)…nv'期望参数'@Usr_Name',未提供。”} - {“The parameterized query '(@Usr_Name nvarchar(4000)… nv' expects the parameter '@Usr_Name', which was not supplied.”} “参数化查询需要未提供的参数”错误 - “The parameterized query expects a parameter which was not supplied” error SQLException未提供的参数化查询### - SQLException The parameterized query ### which was not supplied 参数化查询需要未提供的参数 - The parameterized query expects the parameter , which was not supplied 参数化查询需要未提供的参数 xy - The parameterized query expects parameter xy which was not supplied 参数化查询需要未提供的参数###### - The parameterized query expects the parameter ###### which was not supplied 参数化查询...需要参数'@units',这是未提供的 - The parameterized query … expects the parameter '@units', which was not supplied 参数化查询需要未提供的参数'@Id' - The parameterized query expects the parameter '@Id', which was not supplied 参数化查询“需要参数”,但未提供 - The parameterized query '' expects the parameter '', which was not supplied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM