嘗試備份和還原數據庫時出現此錯誤。數據庫備份和還原期間路徑中的非法字符C#
數據庫備份過程中路徑後端:
SQL server 2008
前端: C#
非法字符,並使用C#
private void backToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
string FileToMove = null;
string MoveLocation = null;
string FileToDel = null;
FileToMove = "|DataDirectory|\\CMS_DB.mdf";
MoveLocation = "|DataDirectory|\\backup\\CMS_DB.mdf";
FileToDel = "|DataDirectory|\\backup\\CMS_DB.mdf";
if (MessageBox.Show("Are you sure you want to backup current database?", "CONFIRMATION", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
{
System.IO.File.Delete(FileToDel);
System.IO.File.Copy(FileToMove, MoveLocation);
MessageBox.Show("Database successfully backup!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
string FileToMove = null;
string MoveLocation = null;
string FileToDel = null;
FileToMove = "|DataDirectory|\\CMS_DB.mdf";
MoveLocation = "|DataDirectory|\\backup\\CMS_DB.mdf";
FileToDel = "|DataDirectory|\\backup\\CMS_DB.mdf";
if (MessageBox.Show("Are you sure want to permanently replace current database with the backup database?", "CONFIRMATION", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
{
System.IO.File.Delete(FileToDel);
System.IO.File.Copy(FileToMove, MoveLocation);
MessageBox.Show("Database successfully restored!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
請在此幫助恢復。
我試過了:FileToMove =「」+ System.Environment.CurrentDirectory +「\\ CMS_DB.mdf」; MoveLocation =「」+ System.Environment.CurrentDirectory +「\\ backup \\ CMS_DB.mdf」; FileToDel =「」+ System.Environment.CurrentDirectory +「\\ backup \\ CMS_DB.mdf」;但仍然收到錯誤CMS_DB.mdf正在被另一個進程使用.. –
這是另一個錯誤。這意味着你的數據庫被鎖定在Porgram或sql實例中。嘗試關閉文件中的任何連接,然後將其移至備份目標。 –
我關閉了連接,但仍然收到相同的錯誤... 我試過con.Dispose()或con.Close()...有沒有其他方法在備份數據庫之前正確關閉連接 –