Solved

Save csv File to Desktop

Posted on 2014-01-14
7
555 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
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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