Solved

How to get a file from SqlServer DB

Posted on 2016-09-14
4
28 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
4 Comments
 
LVL 32

Accepted Solution

by:
Stefan Hoffmann earned 500 total points
Comment Utility
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
Comment Utility
thanks
0
 

Author Comment

by:rflorencio
Comment Utility
Sorry, my mistaque.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

11 Experts available now in Live!

Get 1:1 Help Now