C #: reading image from database using stored procedure

I am trying to read an image file using a stored procedure. The following is the stored procedure for obtaining the image file.

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 

Below is the code to get the value returned from the stored procedure.

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

However, when I try to run the code, I get a SqlException :

The procedure or function 'readImage' expects the parameter '@img', which was not provided.

Although I already passed the @img variable. Please help me! Thanks!

+4
source share
1 answer

change retValue.Direction = ParameterDirection.ReturnValue; on the
retValue.Direction = ParameterDirection.Output;

+2
source

Source: https://habr.com/ru/post/1440441/


All Articles