0

I want to change my folder path on my MySQL query on my code. But I don't know how to.

My visual studio exception : {"Can't create/write to file 'C:UsersAndriDesktoploled.csv' (Errcode: 13 \"Permission denied\")"}

What I want : C:\Users\Andri\Desktop\loled.csv

Following is my code :

public bool exportCSV_DataBuku()
    {
        SaveFileDialog save = new SaveFileDialog();
        save.Filter = "Comma Seperated Values / Excel Document (*.csv)|*.csv|All files (*.*)|*.*";
        save.FilterIndex = 1;

        if (save.ShowDialog() == DialogResult.OK)
        {
            query = "SELECT * FROM daftar_buku INTO OUTFILE '" + save.FileName
            + "' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'";
        }
        if(this.OpenConnection() == true)
        {
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.ExecuteNonQuery();
            this.CloseConnection();
            return true;
        } else
        {
            return false;
        }
    }
Nayana_Das
  • 1,789
  • 4
  • 23
  • 48

2 Answers2

0

This is a permission issue you have to run your application as Administrator to be able to write files to the C:\ Directory. If you want to save the output to the "C:\Users\Andri\" why would you use the SaveFileDialog ? you can just write your path into the query command.

by the way before you display the dialog just set the property InitialDirectory of the SaveFileDialog object to your desired path

save.InitialDirectory = "C:\Users\Andri\Desktop\";
save.FileName = "loled.csv";

the above code will open the save dialog pointing to the desktop directory with a default file name "loled.csv"

UPDATE

if you want to always save your file to the path "C:\Users\Andri\Desktop\loled.csv" change the query command into this

 query = "SELECT * FROM daftar_buku INTO OUTFILE 'C:\Users\Andri\Desktop\loled.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'";
Zakaria M. Jawas
  • 447
  • 8
  • 16
  • Is there any code to replace it or give permission to save ? I just make my C directory as example and I want to save it anywhere btw. Sorry for my bad English. – Azarya Andriano Mar 20 '17 at 10:44
  • yep look at this http://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator but i would use try catch and if the permission exception was thrown i will ask the user to change the directory or run the program as administrator. this is the best approach i guess. – Zakaria M. Jawas Mar 20 '17 at 11:24
0

I already found my own answer and want to help you guys if you had the same problem with me.

public bool exportCSV_DataBuku()
{
    SaveFileDialog save = new SaveFileDialog();
    save.Filter = "Comma Seperated Values / Excel Document (*.csv)|*.csv|All files (*.*)|*.*";
    save.FilterIndex = 1;

    string fileName = save.FileName;
    fileName = fileName.Replace("\\","\\\\");

    if (save.ShowDialog() == DialogResult.OK)
    {
        query = "SELECT * FROM daftar_buku INTO OUTFILE '" + fileName
        + "' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'";
    }
    if(this.OpenConnection() == true)
    {
        MySqlCommand cmd = new MySqlCommand(query, connection);
        cmd.ExecuteNonQuery();
        this.CloseConnection();
        return true;
    } else
    {
        return false;
    }
}

Following my added code :

string fileName = save.FileName;
fileName = fileName.Replace("\\","\\\\");
if (save.ShowDialog() == DialogResult.OK)
{
    query = "SELECT * FROM daftar_buku INTO OUTFILE '" + fileName
    + "' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'";
}