1

でエラーが発生しました

"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();
4

1 に答える 1

2

メソッドAddWithValueの代わりに使用します。Add

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

コマンドでパラメーターを指定する必要があります@

Soner Gönül、画像は null です。どうすれば修正できますか?

したがって、あなたがのDBNull.Value場合は戻る必要があります。必須パラメーターに を渡すことはできません。次のような代替手段としてメソッドを使用できます。imagenullnull

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;
}
于 2013-09-30T06:38:52.390 に答える