Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get a file from SqlServer DB

Posted on 2016-09-14
4
Medium Priority
?
86 Views
Last Modified: 2016-10-27
Hi,

I have a table in SqlServer with a field that stores a xm file.
How can get xml file from database to a memory stream for use like the following example, 'ms' is memory stream:

Dim d1 As New DashControl()
d1.LoadFromXml(ms)

best regards
0
Comment
Question by:rflorencio
  • 2
3 Comments
 
LVL 36

Accepted Solution

by:
ste5an earned 2000 total points
ID: 41798068
hmm, why not using the XML data type instead of a blob?

Retrieving Binary Data:
SqlCommand command = new SqlCommand("SELECT pub_id, logo FROM pub_info", connection);
FileStream stream;                          
BinaryWriter writer;                        
int bufferSize = 100;                   
byte[] outByte = new byte[bufferSize];  
long retval;                            
long startIndex = 0;                    
string pubID = "";                     
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
while (reader.Read())
{
  pubID = reader.GetString(0);  
  stream = new FileStream("logo" + pubID + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
  writer = new BinaryWriter(stream);
  startIndex = 0;
  retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);  
  while (retval == bufferSize)
  {
    writer.Write(outByte);
    writer.Flush();   
    startIndex += bufferSize;
    retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
  }
  
  writer.Write(outByte, 0, (int)retval - 1);
  writer.Flush();
  writer.Close();
  stream.Close();
}

reader.Close();
connection.Close();

Open in new window

0
 

Author Comment

by:rflorencio
ID: 41801453
thanks
0
 

Author Comment

by:rflorencio
ID: 41862042
Sorry, my mistaque.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

783 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