Description
This article will show you how to buffer data into a MemoryStream from a query and output the buffered data back to the browser as a text file.
Introduction
There are many occasions which require web developers to provide downloadable content to end-users in the form of text files, Excel files, etc. This is pretty straight forward if the files already exist and are physically located somewhere the user can access via the browser. However, this requirement becomes a little more complicated if the data that comprises the file is dynamically generated based on user input. An example of this is an application that allows a user to select the number of widgets their company produced between DateA and DateB and then have that data immediately downloaded as a text file. In this situation the text file has to be created on the fly and sent to the browser.
Meeting the requirement is pretty straightforward. The following example shows you how to query a SQL Server database, buffer the output to a MemoryStream, and then have that data transferred to the browser as a Tab-Delimited text file. here.
The Code:
The first step in this example is to query the data out of the database and put it into a MemoryStream. The following function give shows how this can be done.
private MemoryStream GetData()
{
//Create the return memorystream object that will hold
//the buffered data.
MemoryStream ReturnStream = new MemoryStream();
//Put together the connection string
string cn = "Persist Security Info=False;Integrated Security=false;User ID=sa;Pwd=foo;database=northwind;server=foo;Connect Timeout=30";
try
{
//Get the sql string
string SqlString = "select * from dbo.Orders";
//Create a new command object
SqlCommand sc = new SqlCommand();
//Set the connection
sc.Connection = new SqlConnection(cn);
//Open the connection
sc.Connection.Open();
//Set the command type
sc.CommandType = CommandType.Text;
//Set the SQL String
sc.CommandText = SqlString;
//Execute the SQL Statement/Procedure
SqlDataReader RS = sc.ExecuteReader();
//Create a streamwriter to write to the memory stream
StreamWriter sw = new StreamWriter(ReturnStream);
if (RS.Read())
{
//Create a header for the file from the names of the
//columns that we are retrieving
string tempstring = "";
for (int counter = 0; counter < RS.FieldCount;counter++)
{
tempstring += RS.IsDBNull(counter)?"":RS.GetName(counter) + "\t";
}
//Write the row of data to the Memory Stream.
sw.WriteLine(tempstring);
//Loop through the data row and write the contents to the memory
//stream.
while (RS.Read())
{
//Put each columns' data value into a string separated by a tab.
tempstring = "";
for (int counter = 0; counter < RS.FieldCount;counter++)
{
tempstring += RS.IsDBNull(counter)?"":RS.GetValue(counter).ToString() + "\t";
}
//Write the row of data to the Memory Stream.
sw.WriteLine(tempstring);
}
}
//Clean up the stream writer
sw.Flush();
sw.Close();
//Clean up the data reader
RS.Close();
//Clean up the command object
sc.Connection.Close();
sc.Dispose();
}
catch (Exception Ex)
{
throw Ex;
}
//Return the memory Stream
return ReturnStream;
}
David PantojaPosted Apr 18, 2013, 7:22 PM
Thanks! your code was very helpful, I modified it to code a method wich receives a MemoryStream and returns another MemoryStream with a XML serialization from Entity Framework and the result of the operation, works like a charm, thanks!
Shukla ViratPosted May 18, 2010, 4:28 AM
ttrtr