I'm getting this error when trying to backup and restore database.
Back end:
SQL server 2008
Front end: C#
Illegal characters in path during database backup and restore using 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);
}
}
Please help on this.