Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Image update problem SQL Server

Options
  • 04-03-2006 5:46pm
    #1
    Closed Accounts Posts: 1,152 ✭✭✭


    Hey all,
    Im having a little difficulty updating an image stored in a database. What i want a user to be able to do is upload a picture to SQL Server and overwrite an already existing one.

    My Sql statement that i am using is
    SqlCommand command = new SqlCommand("UPDATE images SET image = @image_data,@img_name WHERE bbName = '"+bbName+"' ",connection);
    
    Parameters are:
    SqlParameter param0 = new SqlParameter( "@img_name", SqlDbType.VarChar,50 );
    				param0.Value = imgName;
    				command.Parameters.Add( param0 );
    
    				SqlParameter param1 = new SqlParameter( "@img_data", SqlDbType.Image );
    				param1.Value = imgbin;
    				command.Parameters.Add( param1 );
    
    
    I am using C# and the problem seems to be displaying the pics I think. Can anyone spot anything wrong with the above SQL statement?

    Thanks in advance


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    And, what appears to be the problem?


  • Closed Accounts Posts: 1,152 ✭✭✭sound_wave


    oh yeah, when I try and display the image using
    string name = Request.Params["name"];
    			
    			
    			MemoryStream stream = new MemoryStream ();
    			SqlConnection connection = new SqlConnection ("Data Source = INDUS; Initial Catalog=102708477;Integrated Security=SSPI;");
    			//SqlCommand myCommand = new SqlCommand("SELECT * from images "); //WHERE userName='"+name+"'");
    			try
    			{
    				connection.Open ();
    				SqlCommand command = new 
    					SqlCommand ("select image from images where bbName = '"+name+"'", connection);
    				byte[] image = (byte[]) command.ExecuteScalar ();   
    				stream.Write (image, 0, image.Length);
    				Bitmap bitmap = new Bitmap (stream);
    				Response.ContentType = "image/gif";
    				bitmap.Save (Response.OutputStream, ImageFormat.Jpeg);
    			} 
    			finally
    			{
    				connection.Close ();
    				stream.Close ();
    			}
    		}
    

    the image doesn't appear. This was working before I tried to update the image with the UPDATE statement in my previous post


  • Registered Users Posts: 2,781 ✭✭✭amen


    I'm surprised that
    UPDATE images SET image = @image_data,@img_name
    
    isn't blowing up ?
    what exactly is happening to the second variable is being assigned to image
    don't have to sql server to hand to try
    I'll try later


  • Closed Accounts Posts: 1,152 ✭✭✭sound_wave


    the string is being inserted into the database alright, its just the image that doesnt seem to be updating


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Probably a binary encoding issue. Is there a special reason you're putting images in the database in the first place, though? It's an odd thing to do.


  • Advertisement
Advertisement