2

I have this SQL database "Student" and I want to export images(all or selected) from it's table "StudentPic" to a specific folder with ".jpg" extension.

This is my work and it didn't work. Any comments about this.?

Dim sql As String = " SELECT TOP 10 StudID,StudentPic FROM Student"
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
conn.Open
Dim dr As SqlDataReader = cmd.ExecuteReader

While dr.Read
    Dim bytes() As Byte = CType(dr("StudentPic"),Byte())
    Dim memStream As MemoryStream = New MemoryStream(bytes)
    Try 
        Dim MyImage As Bitmap = New Bitmap(memStream)
        MyImage = New Bitmap(MyImage, 200, 250)
        MyImage.Save((dr("StudID") + ".jpg"), ImageFormat.Jpeg)
    Catch ex As Exception
        Throw ex
    End Try

End While
Tonix
  • 177
  • 2
  • 9
  • You could try looking here: http://stackoverflow.com/questions/11819247/reading-attached-files-from-database-using-ole-db/11819599#11819599 –  Jan 16 '13 at 08:01

2 Answers2

1

You can select the images from the table with a query, then save the images to disk using .net image functions. It's not too difficult, but as has been mentioned, it's not a good idea to expect code to be written here. I would dig into it in .net and if you get stuck, then post your questions here (with details). Good luck!

xpda
  • 15,585
  • 8
  • 51
  • 82
0

You can retrive image to a byte array then write to file using FileStream. Here is Example.

Retrieving images from the database is the exact reverse process of saving images to the database.

SqlCommand cmd = new SqlCommand("select Pic from StudentPic where StudentID=@ID", 
                   new SqlConnection("conn stirng"));cmd.Parameters.AddWithValue("@ID",'id of a student');cmd.Connection.Open();

execute “ExecuteScalar” because we want only “IMAGE” column data back.

byte[] byteImg=(byte[])cmd.ExecuteScalar();cmd.Connection.Close();

Save this data to a Folder.

string strPath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\StudentPics\\"+"picName.jpg";
    FileStream fs=new FileStream(strFileTime,FileMode.CreateNew,FileAccess.Write);
    fs.Write(byteImg,0,byteImg.Length);fs.Flush();fs.Close();

Hopefully helps it.

Lütfullah Kus
  • 302
  • 1
  • 12