4

我一直在尝试使用存储过程读取图像文件。下面给出的是获取图像文件的存储过程。

CREATE PROCEDURE readImage 
    @sID int,
    @img image output
AS
BEGIN
    SET NOCOUNT ON;
    SET @img=(SELECT s_Image FROM Student WHERE s_ID=@sID);
END
GO

下面给出的是获取从存储过程返回的值的代码。

   SqlConnection con = new SqlConnection();
    Connect conn = new Connect();
    con = conn.getConnected();
    con.Open();
    SqlCommand cmd = new SqlCommand("readImage", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@sID", SqlDbType.Int).Value = 17;
    SqlParameter retValue = cmd.Parameters.Add("@img", SqlDbType.Image);
    retValue.Direction = ParameterDirection.ReturnValue;
    try
    {
        cmd.ExecuteNonQuery();
        MemoryStream ms = new MemoryStream((byte[])retValue.Value);
        pictureBox1.Image = Image.FromStream(ms);
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox1.Refresh();
    }
    catch (InvalidOperationException ex)
    {
        MessageBox.Show(ex.Message);
    }

    finally
    {
        if (con.State == ConnectionState.Open)
            con.Close();
    }

但是,当我尝试运行代码时,我得到SqlException

过程或函数“readImage”需要参数“@img”,但未提供该参数。

即使我已经通过了@img变量。请帮我!谢谢!

4

1 回答 1

2

更改 retValue.Direction = ParameterDirection.ReturnValue;
retValue.Direction = ParameterDirection.Output;

于 2012-10-18T03:42:27.313 回答