Solved

How to get a file from SqlServer DB

Posted on 2016-09-14
4
73 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
[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
4 Comments
 
LVL 34

Accepted Solution

by:
ste5an earned 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

615 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