Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Image update problem SQL Server

  • 04-03-2006 05: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, Registered Users 2 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, Registered Users 2 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