C#:使用存储过程从数据库中读取图像

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

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:

Procedure or function ‘readImage’ expects parameter ‘@img’, which was not supplied.

即使我已经传递了@img变量.请帮我!谢谢!

最佳答案 更改retValue.Direction = ParameterDirection.ReturnValue; to retValue.Direction = ParameterDirection.Output;

点赞