Saving Binary values to the Database

Saving native types like a string or an integer to the database is relatively simple. But for saving files or images, you need to convert them first to an array of byte. Here’s a snippet of how to get the blob. 

Private Function StreamFile(ByVal filename As String) As Byte()

  Dim ImageData As Byte() = Nothing

  Try 

    Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read)

    ImageData = New Byte(fs.Length – 1) {}

    fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length))

    fs.Close()

  Catch ex As Exception

    ‘ Do nothing

  End Try

  Return ImageData

End Function

Advertisements

One thought on “Saving Binary values to the Database

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s