Solved

How to get a file from SqlServer DB

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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