Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “什么是”呢;参数化查询。。。没有提供的;错误?_C#_Httphandler - Fatal编程技术网

C# “什么是”呢;参数化查询。。。没有提供的;错误?

C# “什么是”呢;参数化查询。。。没有提供的;错误?,c#,httphandler,C#,Httphandler,我在上遇到了一个错误 "The parametrized query '(@blue nvarchar(4000))SELECT blueBallImage FROM CorrespondingBal' expects the parameter '@blue', which was not supplied." 我正在做一个HttpHandler 我想从数据库中检索图像。我的代码如下 public void ProcessRequest (HttpContext context) {

我在上遇到了一个错误

"The parametrized query '(@blue nvarchar(4000))SELECT blueBallImage FROM CorrespondingBal' expects the parameter '@blue',
which was not supplied."
我正在做一个HttpHandler

我想从数据库中检索图像。我的代码如下

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); 
}
错误发生在

byte[] ballImage = (byte[])cmd.ExecuteScalar();
使用替代方法

您应该在命令中为参数提供
@

Soner Gönül,图像为空。我怎样才能纠正它

因此,如果
image
null
,则应返回
DBNull.Value
。不能对所需参数传递
null
。您可以使用一种方法作为替代方法,如

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;
}

嗨,谢谢你!然而,它不起作用。同样的错误仍然发生。@LiuJiaHui检查我更新的问题。记住使用
@blue
而不是
blue
@LiuJiaHui您确定您的
图像
不为空吗?请与调试器联系。@LiuJiaHui更新了我的问题。让我们
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;
}