我正在尝试使用以下代码从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项目目录。
此外,新目录的创建正在进行,因此我不知道可能出现的问题。
答案 0 :(得分:0)
只是一个建议,但您可以在目录路径中尝试使用尾部斜杠,即将newFolderPath分配行更改为
var newFolderPath = Path.Combine(root, folder) + Path.DirectorySeparatorChar;
如果这没有帮助,请尝试使用短路径并且不包含空格或破折号( - )等特殊字符,例如C:\ testpath \
答案 1 :(得分: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();
}
}