使用mysqlBackup备份数据库时拒绝访问路径
本文关键字:拒绝访问 路径 数据库 mysqlBackup 备份 使用 | 更新日期: 2023-09-27 18:26:55
我正试图使用以下代码从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.
我的路径位于visualstudio项目目录中。
另外,创建一个新目录也在工作,所以我不知道可能出了什么问题。
这只是一个建议,但您可以尝试在目录路径中使用尾部斜杠,即将newFolderPath分配行更改为
var newFolderPath = Path.Combine(root, folder) + Path.DirectorySeparatorChar;
如果这没有帮助,请尝试使用一个短路径,并且不包含空格或破折号(-)等特殊字符,例如C:''testpath''
您试图保存为文件夹,而不是文件。修复方法如下:
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();
}
}