简体   繁体   English

使用mysqlBackup备份数据库时拒绝访问路径

[英]Access path denied when backing up database using mysqlBackup

I'm trying to back up my database from mysql local server using this code: 我正在尝试使用以下代码从mysql本地服务器备份数据库:

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. 我的路径位于Visual Studio项目目录中。

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 只是一个建议,但是您可以尝试在目录路径中使用斜杠,例如,将newFolderPath分配行更改为

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\\ 如果这样没有帮助,请尝试使用短且不包含空格或破折号(-)之类的特殊字符的路径,例如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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM