views:

138

answers:

7

I want to save user image into a database in C#. How do I do that?

A: 

You'll need to serialize the image to a binary format that can be stored in a SQL BLOB column. Assuming you're using SQL Server, here is a good article on the subject:

http://www.eggheadcafe.com/articles/20020929.asp

Dave Swersky
A: 

You'll want to convert the image to a byte[] in C#, and then you'll have the database column as varbinary(MAX)

After that, it's just like saving any other data type.

Mike M.
+1  A: 

Hi,you can save the path of the image in the database or save the image itself as a BLOB ( binary - array of bytes)..it depends on the case you got,if your application is a web application,then saving the path of the image is much better.but if you got a client based application that connects to a centralized database,then you must save it as binary.

Khaled
A: 

This is a method that uses a FileUpload control in asp.net:

byte[] buffer = new byte[fu.FileContent.Length];
Stream s = fu.FileContent;
s.Read(buffer, 0, buffer.Length);
//Then save 'buffer' to the varbinary column in your db where you want to store the image.
Abe Miessler
A: 

Since you are using SQL, would recommend against using adhoc ('writing statements in strings'), especially given that you are loading an image.

ADO.NET can do all of the hard work of mapping, escaping etc for you.

Either create a Stored Procedure, or use SqlParameter to do the binding.

As the other posters say, use VARBINARY(MAX) as your storage type - IMAGE is being depracated.

nonnb
+2  A: 

Try this method. It should work when field when you want to store image is of type bytea. First it creates byte[] for image. Then it saves it DB using IDataParameter of type binary.

    public static void PerisitImage(string path, IDbConnection connection)
    {
        using (var command = connection.CreateCommand ())
        {
            Image img = Image.FromFile (path);
            MemoryStream tmpStream = new MemoryStream();
            img.Save (tmpStream, ImageFormat.Png); // change to other format
            tmpStream.Seek (0, SeekOrigin.Begin);
            byte[] imgBytes = new byte[MAX_IMG_SIZE];
            tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE);

            command.CommandText = "INSERT INTO images(payload) VALUES (:payload)";
            IDataParameter par = command.CreateParameter();
            par.ParameterName = "payload";
            par.DbType = DbType.Binary;
            par.Value = imgBytes;
            command.Parameters.Add(par);
            command.ExecuteNonQuery ();
        }
    }
jethro
+1  A: 

My personal preference is not to save the images to a database as such. Save the image somewhere in the file system and save a reference in the database.

Jaydee
Only useful in a few cases, as you have to make the file system in question open to all the .NET code. It's also inconsistent. Can be useful enough in web apps, as long as you never need to expand onto a webfarm, at which point sharing the files can be a nuisance.
Jon Hanna
you can use the SQL http://msdn.microsoft.com/en-us/library/cc949109.aspx - also available in nHIbernate from 2.1 http://zvolkov.com/blog/post/2009/07/20/Whats-new-in-NHibernate-21.aspx serilaizing in the DB is not a good design strategy - having to set permissions on a folder is not a good reason to not do it properly :)
cvista
@cvista I save images to the DB frequently. Drop a memcached instance in the mix and it becomes even more attractive.
Ryan