Search code examples
winformsentity-framework-4vb.net-2010

Saving Files From Folder Into SQL Database In Windows Forms VB EF


I'm really struggling with this and any help would be greatly appreciated.

I need a bit of code that goes through each file in a folder and saves it into a database so that it can later be pulled out and displayed. I haven't got to the displaying part yet lol, still trying to get the files in the database.

What I have so far is giving me an error

Value of type '1-dimensional array of Byte' cannot be converted to 'String'.

All the fields in the database are nvarchar(MAX)

    Dim dir As New System.IO.DirectoryInfo("C:\Users\Will\Desktop\SUBARU")
    For Each f As System.IO.FileInfo In dir.GetFiles("*.*")
        Using db As New FileStoreAppDBEntities
            Dim NewFile As New StoredFile
            NewFile.FileName = f.Name
            NewFile.FileSize = f.Length
            'got trouble here
            Dim ImageData As Byte() = System.IO.File.ReadAllBytes(f.FullName)
            NewFile.FileContent = ImageData
            '
            NewFile.FileType = f.Extension
            db.StoredFiles.AddObject(NewFile)
            db.SaveChanges()
        End Using
    Next

Perhaps i'm doing this all wrong?

Many thanks for your help.

--EDIT

This seems to do it!

     For Each f As System.IO.FileInfo In dir.GetFiles("*.*")
        Using db As New FileStoreAppDBEntities
            Dim NewFile As New StoredFile
            NewFile.FileName = f.Name
            NewFile.FileSize = f.Length
            Dim filename As String = f.FullName
            NewFile.FileContent = System.IO.File.ReadAllBytes(filename)
            NewFile.FileType = f.Extension.ToLower
            db.StoredFiles.AddObject(NewFile)
            db.SaveChanges()
        End Using
     Next

Not sure on the performance though, i think this might put the file in memory then send it instead of streaming?

And to retrieve the file:

    Using db As New FileStoreAppDBEntities
        Dim IDofFile As Integer = 31
        Dim getFile = (From files In db.StoredFiles Where files.FileID = IDofFile Select files).SingleOrDefault
        'getFile.FileName eg. mydocument.txt
        System.IO.File.WriteAllBytes("C:\LocationToSaveTo\" & getFile.FileName, getFile.FileContent)
    End Using

Getting an error for very large files System.OutOfMemoryException Working perfectly for the smaller files though...

Perhaps chunking the file up would be possible?


Solution

  • This seems to do it!

     For Each f As System.IO.FileInfo In dir.GetFiles("*.*")
        Using db As New FileStoreAppDBEntities
            Dim NewFile As New StoredFile
            NewFile.FileName = f.Name
            NewFile.FileSize = f.Length
            Dim filename As String = f.FullName
            NewFile.FileContent = System.IO.File.ReadAllBytes(filename)
            NewFile.FileType = f.Extension.ToLower
            db.StoredFiles.AddObject(NewFile)
            db.SaveChanges()
        End Using
     Next
    

    Not sure on the performance though, i think this might put the file in memory then send it instead of streaming?

    And to retrieve the file:

    Using db As New FileStoreAppDBEntities
        Dim IDofFile As Integer = 31
        Dim getFile = (From files In db.StoredFiles Where files.FileID = IDofFile Select files).SingleOrDefault
        'getFile.FileName eg. mydocument.txt
        System.IO.File.WriteAllBytes("C:\LocationToSaveTo\" & getFile.FileName, getFile.FileContent)
    End Using
    

    Getting an error for very large files System.OutOfMemoryException Working perfectly for the smaller files though...