Solved

Save csv File to Desktop

Posted on 2014-01-14
7
567 Views
Last Modified: 2014-06-30
I have a .NET web app that queries a mySQL DB. I want to send the results to an Excel file/datatable on the users desktop.

Everything seems to work fine, but the file isn't there. But when I run the app a 2nd time, I get an error that the file exists, but there is no file.

Here's my file path & SQL query.

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                                     "dataTable.csv"); 
                                     
string sql3 = "SELECT * FROM table_173 " +     
              " INTO OUTFILE '" + fileName + "' " +
              " FIELDS TERMINATED BY ',' " +
              " ENCLOSED BY '\"' " +
              " LINES TERMINATED BY '\n'; ";

Open in new window

             
             
The following SQL query works fine & the file is there.
             
string sql2 = "SELECT * FROM table_173 " + 
              " INTO OUTFILE 'C:/LH/dataTable.csv' " +
              " FIELDS TERMINATED BY ',' " +
              " ENCLOSED BY '\"' "  + 
              " LINES TERMINATED BY '\n'; ";

Open in new window

0
Comment
Question by:LaserdogLezly
[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
  • 3
7 Comments
 
LVL 33

Assisted Solution

by:Big Monty
Big Monty earned 175 total points
ID: 39780292
you're saving the file to the servers desktop, not the users desktop.

you can't save a file automatically to a users desktop, that's a huge security issue.
0
 

Author Comment

by:LaserdogLezly
ID: 39780543
I saved an HTML file to the desktop successfully. What's the difference?


string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                                     "feeschedule.html");

         System.IO.StreamWriter file = new System.IO.StreamWriter(fileName);
         file.WriteLine(myHtmlFile);     
         return myHtmlFile;

Open in new window



on edit: I just checked the remote server & there is no file there.
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39780558
which desktop?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:LaserdogLezly
ID: 39780741
I saved an HTML file to my desktop.
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39780953
is this the same machine that hosts the web site?
0
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 175 total points
ID: 39782900
Hi,

It is worthy to read article at link MySQL Reference -13.2.8.1. SELECT ... INTO Syntax

As per this article,

The SELECT ... INTO OUTFILE 'file_name' form of SELECT writes the selected rows to a file. The file is created on the server host, so you must have the FILE privilege to use this syntax. file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed.

My suggestion as solution would be to grant full permission to website code for saving file at the location you specified in your code -

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                                     "feeschedule.html");

Open in new window

0
 

Author Closing Comment

by:LaserdogLezly
ID: 40168517
Both responses were correct in that I was saving to a remote server with permissions.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

732 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