Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Bro can you help me, please!

How to store and retrieve file excel in sql server database through VB.NET



thanks,
Posted
Updated 7-Jun-12 23:40pm
v2
Comments
codeBegin 8-Jun-12 5:33am    
Excel or images what exactly your requirement is??
Maciej Los 8-Jun-12 18:21pm    
What it mean: BRO?

Images:
Table
SQL
CREATE TABLE dbo.STORED_IMAGES
     (
     ID int NOT NULL  IDENTITY (1, 1),
     Img varbinary(MAX) NULL
     ) 

Storing Image:
VB
Dim CMD As New SqlClient.SqlCommand("insert into STORED_IMAGES(Img) values(@Img)",cn)
CMD.Parameters.Add("@Img", SqlDbType.VarBinary, Integer.MaxValue).Value = ReadFile(Path)

Function for reading file:
VB
Private Function ReadFile(sPath As String) As Byte()
        Dim data As Byte() = Nothing
        Dim fInfo As New IO.FileInfo(sPath)
        Dim numBytes As Long = fInfo.Length
        Using fStream As New IO.FileStream(sPath, IO.FileMode.Open, IO.FileAccess.Read)
            Dim br As New IO.BinaryReader(fStream)
            data = br.ReadBytes(CInt(numBytes))
        End Using
        Return data
    End Function

Getting from DB:
SQL
Dim fcmd As New SqlCommand("select Img from STORED_IMAGES where ID=@ID", cn)
fcmd.Parameters.Add("@ID", SqlDbType.Int).Value = 3 'e.g.
Dim bytes As Byte() = fcmd.ExecuteScalar


Ref: How to store and retrieve images in sql server database with vb.net[^]
Refer:
Save and retrieve images with SQL Server [^]
Storing images in SQL Server 2005 and retrieving them from VB.NET[^]

Excel Files:
Retrieve excel file stored in sql server using vb.net[^]
 
Share this answer
 
Comments
maddoxcom 8-Jun-12 5:42am    
sorry bro not image but file ex: excel or doc
and retrieve from database become file again

thanks
Dave Kreskowiak 8-Jun-12 9:41am    
The process is exactly the same. Whether it's an image or an Excel file, a file is just a stream of bytes! It makes no difference to SQL Server what that stream represents.
Maciej Los 8-Jun-12 18:22pm    
Good work! +5
maddoxcom 12-Jun-12 7:44am    
oke thanks bro
but
sorry bro I'm Junior
want to ask again
after this proses
'Dim fcmd As New SqlCommand("select Img from STORED_IMAGES where ID=@ID", cn)
fcmd.Parameters.Add("@ID", SqlDbType.Int).Value = 3 'e.g.
Dim bytes As Byte() = fcmd.ExecuteScalar'

how to make excel file again
Hi maddoxcom,
Go through this link-
Store file in database[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900