I'm trying to back up my database from mysql local server using this code:
string folder = DateTime.Now.Date.ToString("yyyy-MM-dd");
var root = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),"Database backup");
var newFolderPath = Path.Combine(root, folder);
if (!Directory.Exists(newFolderPath)) // if it doesn't exist, create
Directory.CreateDirectory(newFolderPath);
MySqlConnection myCon = frmStudentsSignup.establishConnectionToMysql();
using(MySqlCommand cmd = new MySqlCommand()) {
using(MySqlBackup mb = new MySqlBackup(cmd)) {
cmd.Connection = myCon;
myCon.Open();
mb.ExportToFile(newFolderPath);
myCon.Close();
}
}
After lunching this line
mb.ExportToFile(newFolderPath);
I get
access to the path ... is denied.
My path is located at visual studio project directory.
Also the creating of a new directory is working so I have no idea what could be wrong.
Just a suggestion, but you can try with a trailing slash in the directory path, ie change the newFolderPath assignment line to
var newFolderPath = Path.Combine(root, folder) + Path.DirectorySeparatorChar;
If that doesn't help, try with a path that is short and contains no special characters like spaces or dashes (-), eg C:\\testpath\\
You are trying to save as a folder, not file. Here is the fix:
string folder = DateTime.Now.Date.ToString("yyyy-MM-dd");
var root = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Database backup");
var newFolderPath = Path.Combine(root, folder);
if (!Directory.Exists(newFolderPath)) // if it doesn't exist, create
Directory.CreateDirectory(newFolderPath);
// Fixed
string newFileFolderPath = Path.Combine(newFolderPath, "mybackup.sql");
MySqlConnection myCon = frmStudentsSignup.establishConnectionToMysql();
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = myCon;
myCon.Open();
mb.ExportToFile(newFileFolderPath);
myCon.Close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.