473,472 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

streaming text (csv) files


Hi all,

I want to export the contents of a datatable out to a web client and I
am wondering what the best performance and maintanence concious method
of doing this is.

Would it be possible to have the text file streamed out to the client
directly? Meaning, I don't even want to write the text file to disk -
just create it in memory and then have the server stream the file
directly to the browser as a download.

Is this too much coding? Is this extremely advanced and time consuming?

Regards,

Troy
Nov 18 '05 #1
2 3725
Hi Troy,

Here's some code that should get you going. It grabs data from the SQL
database as a datareader and pushes it out as a CSV. No storage of the file
required. You should see Excel open with the data.

Does this help?

Ken
Microsoft MVP [ASP.NET]
Imports System.Data.SqlClient
Imports System.IO
Public Class csv
Inherits System.Web.UI.Page
Protected WithEvents Button1 As _
System.Web.UI.WebControls.Button
Protected WithEvents SqlConnection1 As _
System.Data.SqlClient.SqlConnection
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.SqlConnection1 = _
New System.Data.SqlClient.SqlConnection
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = _
"data source=P4320;initial catalog=" & _
"Northwind;password="""";persist security info=Tru" & _
"e;user id=sa;workstation id=P4320;packet size=4096"
End Sub
Private Sub Page_Init _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Init
'CODEGEN: This method call is
'required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
'Set the appropriate ContentType.
Dim filename As String = "orderdetails.csv"
Dim myCommand As New SqlCommand _
("select * from [order details] ", SqlConnection1)
myCommand.Connection.Open()
Dim myReader As SqlDataReader = _
myCommand.ExecuteReader _
(CommandBehavior.CloseConnection)
Dim i As Integer
Dim sb As New System.Text.StringBuilder
For i = 0 To myReader.FieldCount - 1
If i < (myReader.FieldCount - 1) Then
sb.Append(Chr(34) & myReader.GetName(i) & _
Chr(34) & ",")
Else
sb.Append(Chr(34) & myReader.GetName(i) & _
Chr(34) & vbCrLf)
End If
Next
While myReader.Read()
For i = 0 To myReader.FieldCount - 1
If i < (myReader.FieldCount - 1) Then
sb.Append(Chr(34) & _
myReader.GetValue(i).ToString & Chr(34) & ",")
Else
sb.Append(Chr(34) & _
myReader.GetValue(i).ToString & Chr(34) & vbCrLf)
End If
Next
End While
myReader.Close()
SqlConnection1.Close()
Response.ContentType = "Application/x-msexcel"
Response.AddHeader _
("content-disposition", "attachment; filename=""" & _
filename & """")
'Write the file directly to the HTTP output stream.
Response.Write(sb.ToString)
Response.End()
End Sub
End Class

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="csv.aspx.vb"
Inherits="p4320work.csv"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>csv</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Launch"></asp:Button>
</form>
</body>
</HTML>

"Troy" <po****@designguide.com> wrote in message
news:MP************************@msnews.microsoft.c om...

Hi all,

I want to export the contents of a datatable out to a web client and I
am wondering what the best performance and maintanence concious method
of doing this is.

Would it be possible to have the text file streamed out to the client
directly? Meaning, I don't even want to write the text file to disk -
just create it in memory and then have the server stream the file
directly to the browser as a download.

Is this too much coding? Is this extremely advanced and time consuming?

Regards,

Troy


Nov 18 '05 #2
Ken Cox [Microsoft MVP],
Hi Troy,

Here's some code that should get you going. It grabs data from the SQL
database as a datareader and pushes it out as a CSV. No storage of the file
required. You should see Excel open with the data.

Does this help?

That did the trick! Thanks!
--
--

Regards,

Troy
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John | last post by:
I am using Excel to save a spreadsheet in .csv format. This creates a text file with extension .csv But when I try to upload the file to the server by checking for its MIME type using $_FILE I get...
7
by: Markus Ernst | last post by:
Hi I know this question is rather HTTP related than PHP, but I did not find an HTTP group on my news server. I deliver some files with the following PHP syntax: header('Content-Description:...
10
by: Lorn Davies | last post by:
Hi there, I'm a Python newbie hoping for some direction in working with text files that range from 100MB to 1G in size. Basically certain rows, sorted by the first (primary) field maybe second...
2
by: David Pratt | last post by:
Hi. I have files that I will be importing in at least four different plain text formats, one of them being tab delimited format, a couple being token based uses pipes (but not delimited with...
3
by: David Reynolds | last post by:
Does anyone know how I could stream Excel to the browser for download without saving the file somewhere first. I wasn't sure how I would go about it.
3
by: A.M-SG | last post by:
Hi, I have a ASP.NET aspx file that needs to pass large images from a network storage to client browser. The requirement is that users cannot have access to the network share. The aspx file...
13
by: sonald | last post by:
Hi, Can anybody tell me how to change the text delimiter in FastCSV Parser ? By default the text delimiter is double quotes(") I want to change it to anything else... say a pipe (|).. can anyone...
5
by: praveenholal | last post by:
Hi Freinds , I want to convert the files that are in text format (.txt) to CSV file. I am working on Linux. So can anyone guide me. Here is my problem Description I have some result files...
5
by: felciano | last post by:
Hello -- Is there a convention, library or Pythonic idiom for performing lightweight relational operations on flatfiles? I frequently find myself writing code to do simple SQL-like operations...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.