Solved

How to get a file from SqlServer DB

Posted on 2016-09-14
4
56 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 33

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 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