0

我正在尝试使用以下代码从 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();
  }
}

午餐后这条线

mb.ExportToFile(newFolderPath);

我明白了

access to the path ... is denied.

我的路径位于 Visual Studio 项目目录中。

新目录的创建也正在工作,所以我不知道可能出了什么问题。

4

2 回答 2

0

只是一个建议,但您可以尝试在目录路径中使用斜杠,即将 newFolderPath 分配行更改为

var newFolderPath = Path.Combine(root, folder) + Path.DirectorySeparatorChar;

如果这没有帮助,请尝试使用较短且不包含空格或破折号 (-) 等特殊字符的路径,例如 C:\testpath\

于 2014-10-18T13:44:51.540 回答
0

您正在尝试另存为文件夹,而不是文件。这是修复:

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();
    }
}
于 2017-03-08T09:32:20.800 回答