Solved

Importing and Exporting files to an SQL Server Database from VB.Net

Posted on 2007-03-20
4
173 Views
Last Modified: 2010-04-23
Hi there

I am storing in a database files (*.pdf, *.doc, *.bmp etc) as below

                Dim fs As New System.IO.FileStream(s(i), IO.FileMode.Open, IO.FileAccess.Read)
                Dim bytes() As Byte
                ReDim bytes(fs.Length)
                fs.Read(bytes, 0, fs.Length)
...................................
                cmd.Parameters.Add("file", SqlDbType.Image).Value = bytes

I now want to get the file back.  For this I am doing:

            file = System.IO.Path.GetFileName(SaveFileDialog1.FileName)
            file_path = System.IO.Path.GetDirectoryName(SaveFileDialog1.FileName)
.................
            Dim fs As New System.IO.FileStream(SaveFileDialog1.FileName, IO.FileMode.CreateNew, IO.FileAccess.Write)
            fs.Write(dt.Rows(0).Item("file"), 0, Len(dt.Rows(0).Item("filename")))
            fs.Close()

It is creating a file of the right name etc, but the file cannot be loaded correctly.

What am I doing wrong? Or is there a better way (and if so how).

Many thanks!!

0
Comment
Question by:DavidGreenfield
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
ullfindsmit earned 500 total points
ID: 18756452
change
 fs.Write(dt.Rows(0).Item("file"), 0, Len(dt.Rows(0).Item("filename")))
to
 fs.Write(dt.Rows(0).Item("file"), 0, Len(dt.Rows(0).Item("file")))

hope that helps
-Smit
0
 

Author Comment

by:DavidGreenfield
ID: 18756525
Thanks Smit, I didn't notice using the wrong variable!

However on changing it, I am still getting the same problem - i.e. the file being created won't load, i.e. it appears corrupted to the application opening it.
0
 
LVL 12

Assisted Solution

by:ullfindsmit
ullfindsmit earned 500 total points
ID: 18756607

                ReDim bytes(fs.Length)

needs to be


                ReDim bytes(fs.Length - 1)
0
 

Author Comment

by:DavidGreenfield
ID: 18756932
Got it finally the magic code is:

fs.Write(CType(dt.Rows(0).Item("file"), Byte()), 0, CType(dt.Rows(0).Item("file"), Byte()).GetLength(0))

Thanks to you both for pointing out my errors!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question