FileToBlob – Loading a file into a SQL Server’s BLOB

' Reusable routine that loads a file into a SQL Server's BLOB.' Requires Imports for System.Data and System.Data.SqlClient' (It supports only tables with one key field.)'' Example:'    FileToBlob(cn, "pub_info", "logo", "pub_id", "0877", "c:Example.bmp")Sub FileToBlob(ByVal cn As SqlConnection, ByVal tableName As String, _    ByVal blobField As String, ByVal keyField As String, _    ByVal keyValue As Object, ByVal filename As String)    ' Get the value of the pointer to the BLOB field.    Dim sql As String = String.Format _        ("SELECT TEXTPTR({0}) FROM {1} WHERE {2}[email protected]", blobField, tableName, _        keyField)    Dim cmd As New SqlCommand(sql, cn)    cmd.Parameters.Add("@id", keyValue)    ' The pointer is actually a byte array, but we don't really care here.    Dim pointer As Object = cmd.ExecuteScalar()    cmd.Dispose()    ' Open the file, throw if not found.    Dim stream As New System.IO.FileStream(filename, FileMode.Open, _        FileAccess.Read)    ' Prepare the receiving buffer.    Const CHUNK_SIZE As Integer = 200    Dim buffer(CHUNK_SIZE - 1) As Byte    ' Prepare the UPDATETEXT command.    sql = String.Format("UPDATETEXT {0}.{1} @pointer @offset 0 @bytes", _        tableName, blobField)    cmd = New SqlCommand(sql, cn)    cmd.Parameters.Add("@pointer", pointer)    Dim offsetPar As SqlParameter = cmd.Parameters.Add("@offset", SqlDbType.Int)    Dim bufferPar As SqlParameter = cmd.Parameters.Add("@bytes", _        SqlDbType.Image, CHUNK_SIZE)    Dim offset As Integer = 0    Try        Do            ' Read a bunch of bytes from the stream.            Dim count As Integer = stream.Read(buffer, 0, buffer.Length)            If count = 0 Then Exit Do            ' Execute the UPDATETEXT command with this offset and these bytes.            offsetPar.Value = offset            bufferPar.Value = buffer            cmd.ExecuteNonQuery()            offset += count        Loop    Finally        stream.Close()    End TryEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS