Saving an Image to SQL

This should help you save images to your DB.


 Dim dt As New DataTable
        Const ImageField As String = "NameOfTheImageField"

        'The code assumes you have a table with an imagefield in it
        'and that a form is used, where a connection to that table is added
        'through the server explorer. The designer should have created an sqlconnection
        'object and an sqldataadapter object. The latter is called SqlDataAdapter1
        'in my case and that object is the one that does the actual updating
        'Also the example simply reads and updates the first row, so the table should
        'already contain a row.

        'read value
        Me.SqlDataAdapter1.Fill(dt)
        Dim img As Object = dt.Rows(0)(ImageField)
        If Not img Is DBNull.Value Then
            Me.PictureBox1.Image = Image.FromStream(New System.IO.MemoryStream(CType(img, Byte())))
            'the above in steps:
            'the field contains a byte array, so img is converted with CType(img, Byte())
            'of the resulting array a memorystream is created New System.IO.MemoryStream(TheByteArray)
            'The resulting memorystream is then read with the shared function FromStream of the Image object
        End If

        'update
        'load a test image
        Dim TestImage As Image = Image.FromFile("c:	est.bmp")
        'Since this test loads from disk, the file could be read in as
        'binary data straight from disk, but for completeness and reusibility
        'we'll convert an already loaded .net image object to binary:
        Dim ms As New System.IO.MemoryStream
        TestImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) 'choose the format in which you want to store the picture here
        dt.Rows(0)(ImageField) = ms.ToArray()

        Me.SqlDataAdapter1.Update(dt)

This is in C#


DataTable dt = new DataTable();
            const string ImageField = "AnImage";

            /*The code assumes you have a table with an imagefield in it
            and that a form is used, where a connection to that table is added
            through the server explorer. The designer should have created an sqlconnection
            object and an sqldataadapter object. The latter is called SqlDataAdapter1
            in my case and that object is the one that does the actual updating
            Also the example simply reads and updates the first row, so the table should
            already contain a row.*/

            //read value
            this.sqlDataAdapter1.Fill(dt);
            object img = dt.Rows[0][ImageField];
            if( img != DBNull.Value )
                this.pictureBox1.Image = Image.FromStream(new System.IO.MemoryStream((Byte[])img));
            /*the above in steps:
            the field contains a byte array, so img is converted with (Byte[])img
            of the resulting array a memorystream is created: new System.IO.MemoryStream(TheByteArray)
            The resulting memorystream is then read with the shared function FromStream of the Image object
            */


            //update
            //load a test image
            Image TestImage = Image.FromFile(@"c:	est.bmp");
            /*Since this test loads from disk, the file could be read in as
            binary data straight from disk, but for completeness and reusibility
            we'll convert an already loaded .net image object to binary:*/
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            TestImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //choose the format in which you want to store the picture here
            dt.Rows[0][ImageField] = ms.ToArray();

            this.sqlDataAdapter1.Update(dt);