?
Solved

Export SQL Query to TXT

Posted on 2012-04-05
5
Medium Priority
?
238 Views
Last Modified: 2012-08-13
Hello, is there a way to allow to download a SQL Query to TXT (csv) without creating the physical TXT file on the hard drive and provide a link? I use VB not C# (asp.net)

Thanks.
0
Comment
Question by:pvg1975
[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
  • 3
  • 2
5 Comments
 

Author Comment

by:pvg1975
ID: 37812691
I will explain better. I have a SQL query that returns about 2,000 records. These records should be exported to a TXT file, but I dont want to create the file on the server's HDD, I was wondering if there's a way to allow the user to download the SQL results on a TXT file without creating such file on the hard drive. Thanks.
0
 
LVL 27

Accepted Solution

by:
Chinmay Patel earned 2000 total points
ID: 37815408
Hi pvg1975,

Check this code :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write("Downloading....")

        Dim sch As New clsReport
        Dim fileData As New clsReport.Letter
        fileData = sch.DownloadLetter(Session("LetterID"))
        Dim ms As System.IO.MemoryStream = fileData.fil

        Dim fil As Byte() = ms.ToArray
        Dim nam As String = fileData.name
        Dim ext As String = fileData.ext
        If Not fil Is Nothing Then
            Try
                ' Response.Clear()
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + nam & "." & ext)
                ' Response.AddHeader("Content-Disposition", nam & "." & ext)
                Response.AddHeader("Content-Length", fil.Length.ToString)
                Response.ContentType = "Application/octet-stream"
                Response.Buffer = True
                Response.BinaryWrite(fil)
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try

        End If


    End Sub

Open in new window


Basically all you need to do is to build fileData string and then use Response.Write not Response.BinaryWrite.

Regards,
Chinmay.
0
 

Author Comment

by:pvg1975
ID: 37819246
Thanks, I found that code too at dotnetspark, but I couldnt make it work, I get an error at

Dim sch As New clsReport
0
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 37819676
Hi pvg1975,

We won't need that. You can remove it. I have given the example code as reference only based on which you can dervie your own code.

Regards,
Chinmay.
0
 

Author Comment

by:pvg1975
ID: 37819935
Yes I figured that out! Thanks :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

765 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