?
Solved

Save csv File to Desktop

Posted on 2014-01-14
7
Medium Priority
?
605 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 34

Assisted Solution

by:Big Monty
Big Monty earned 700 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 34

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 34

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 700 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

840 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