Solved

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

Posted on 2007-03-20
4
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

626 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