Save Excel to a SQL image field

Today's task was to take an Excel file and save it to a SQL Image field.

All the approaches that I saw while researching did an ODBC type query and sent the DATA to SQL. This approach takes the actual Excel file and saves it. 

Specifically, we are using a Telerik ASP.NET RadAsyncUpload control to grab a file from the user... but the approach can be modified for almost any use. 

Code below. 

 

Related Articles

... and you 'll find more on the Telerik ASPNET Menu

.NET Code:

Private Sub RadAsyncUpload1_FileUploaded(sender As Object, e As FileUploadedEventArgs) Handles RadAsyncUpload1.FileUploaded
       Dim strError As String = ""
       Try
 
           'the user is uploading an Excel spreadsheet.
           'bring the file into a Binary Reader
           Dim br As New BinaryReader(e.File.InputStream)
 
           'convert to a byte array
           Dim oByte As Byte() = br.ReadBytes(Convert.ToInt32(e.File.InputStream.Length))
 
           'the user can add comments about the file
           Dim strComments As String = ToString2(txtComment.Text)
 
           'create a name for the document
           Dim strDocName As String = String.Format("{0} {1:MMdd}", "Quote", Now)
 
           'sql insert
           SPs.fp_FPDocument_MergeWSeq2(mintQuoteID, 23, "TK", 0, oByte, strComments, "Quote", App.UserName, strDocName, "xlsx", App.ConnectionString).execute()
 
           'this is not needed, but it is interesting. this saves the byte array to disk
           System.IO.File.WriteAllBytes("c:/temp/restored.xlsx", oByte)
 
 
       Catch ex As ThreadAbortException
       Catch ex As Exception
           ErrorHandler.globalErrorHandler(ex, strError, App.AppName, App.UserName, False)
           Me.lblError.Text = ex.Message
       End Try
   End Sub

SQL Stored Proc

IF EXISTS (SELECT name
    FROM   sys.objects
    WHERE  name = N'fp_FPDocument_MergeWSeq2'
    AND    type = 'P')
    DROP PROCEDURE fp_FPDocument_MergeWSeq2
GO
 
 
CREATE PROCEDURE fp_FPDocument_MergeWSeq2
/*
declare @i varbinary(max)
set @i = 1
exec fp_FPDocument_MergeWSeq2 'STD10004195',2,'Delivery',0, @i , 'test note', 'DEL','sg','Delivery 0215','jpg'
 
 
select * from FPDocument
 
*/
 
@Docnumber varchar(21),     -- masternumber
@Doctype int,               -- 1
@FileType varchar(20),      -- Signature, Delivery
@Sequence INT,              -- can be 0
@ImageBLOB image,
@Notes Varchar(MAX),
@Category VARCHAR(15),      -- MasterOrder
@CreatedBy VARCHAR(15),
@DocName VARCHAR(15),       -- SignatureMMDD
@Extension VARCHAR(3)       -- jpg
 
 
AS
 
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
 
IF @Sequence = 0 BEGIN
    select @Sequence = max(sequence) + 1 from docs..FPDocument where Docnumber = @Docnumber and Doctype = @Doctype
    if @Sequence is null begin
        set @Sequence = 1
    end
END
 
--don't want more than one signature for a SOP document. Just leave the Seq = 1
--we'll just keep the most recent one.
IF @filetype = 'sig' BEGIN
    SET @Sequence = 1
end
 
--Default the docname
DECLARE @FileTypeDesc VARCHAR(20)
SELECT @FileTypeDesc = filetypedesc
    FROM dbo.FPDocCategory dc
    WHERE dc.FileType = @FileType
SET @FileTypeDesc = ISNULL(@FileTypeDesc,'XX')
 
 
if not exists (select 1 from docs..FPDocument where Docnumber = @Docnumber and Doctype = @Doctype and FileType = @FileType AND Sequence = @Sequence ) BEGIN
    INSERT INTO  docs..FPDocument (Docnumber  , Doctype  , ImageBLOB  , Created   , FileType  , Sequence  , Category , Notes , CreatedBy  , Extension , DocName)
                     VALUES       (@Docnumber , @Doctype , @ImageBLOB , getdate() , @FileType , @Sequence , @Category, @Notes, @CreatedBy , @Extension, CASE WHEN @DocName = '' THEN @FileTypeDesc + ' ' + FORMAT(GETDATE(),'MMdd') ELSE @DocName end )
END ELSE BEGIN
    UPDATE docs..FPDocument SET
            ImageBLOB = @ImageBLOB,
            Extension = @Extension,
            Category = @Category,
            Docname = CASE WHEN @DocName = '' THEN @FileTypeDesc + ' ' + FORMAT(Created,'MMdd') ELSE @DocName end,
            Notes = @Notes
        WHERE Docnumber = @Docnumber and Doctype = @Doctype and FileType = @FileType AND Sequence = @Sequence
end
 
 
 
GO
 
 
grant exec on fp_FPDocument_MergeWSeq2 to PUBLIC
 
--  sp_sps 'f'

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences