Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now