Solved

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

Posted on 2007-03-20
4
190 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …

734 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