-1

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.

Community
  • 1
  • 1

3 Answers3

1

You need to look at Path.GetInvalidPathChars Method to determine the list of invalid characters.

The array returned from this method is not guaranteed to contain the complete set of characters that are invalid in file and directory names. The full set of invalid characters can vary by file system. For example, on Windows-based desktop platforms, invalid path characters might include ASCII/Unicode characters 1 through 31, as well as quote ("), less than (<), greater than (>), pipe (|), backspace (\b), null (\0) and tab (\t).

And if I am not mistaken I see a pipe (|) character in the file names you have provided.

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
0

Two Thing here ,

first check the folder "backup" exist and second use this method to remove illegal character :

 public string StripIllegalChars(string _input)
    {
        int CharPos = 0;
        char[] stChars = System.IO.Path.GetInvalidPathChars();
        string Result = _input;

        CharPos = 0;


        foreach (char achr in stChars)
        {


            CharPos = _input.IndexOf(achr);


            if (CharPos > 0)
            {

                Result = Result.Replace(achr.ToString(), "");

            }

        }

        return Result;
    }

also u can use this from ms : http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars.aspx

Ahad Porkar
  • 1,666
  • 2
  • 33
  • 68
  • i tried: FileToMove = "" + System.Environment.CurrentDirectory + "\\CMS_DB.mdf"; MoveLocation = "" + System.Environment.CurrentDirectory + "\\backup\\CMS_DB.mdf"; FileToDel = "" + System.Environment.CurrentDirectory + "\\backup\\CMS_DB.mdf"; but still getting the error CMS_DB.mdf is being used by another process.. – Raj Sharma Aug 19 '13 at 06:56
  • This is another error. it means your DB is locked inside Porgram or sql instance. try to close any connection from file and then move it to your backup destination. – Ahad Porkar Aug 19 '13 at 08:30
  • i closed the connection but still i am getting the same error... i tried both con.Dispose() or con.Close()...is there any another way to close the connection properly before backup the database – Raj Sharma Aug 19 '13 at 18:17
  • how can i unlock the DB ? – Raj Sharma Aug 19 '13 at 18:20
0

|DataDirectory| is a substitution string you can use to configure the location of your database file separately, but it only works when defining an ADO.NET connection.

This post may be of some help to you. I'll quote the parts that may help you the most:

So where does DataDirectory come from? It's one of the deployment settings defined by installers:

  • .MSI installers define it as the app's target folder.
  • ClickOnce defines a special data folder in your project.
  • Web apps use the App_Data folder.
  • The Visual Studio debugger uses the debug folder.

So you'll need to alter your application, and substitute the "|DataDirectory|" part of your strings to the correct physical path according to your target deployment environment.

Community
  • 1
  • 1
OnoSendai
  • 3,960
  • 2
  • 22
  • 46
  • i tried: FileToMove = "" + System.Environment.CurrentDirectory + "\\CMS_DB.mdf"; MoveLocation = "" + System.Environment.CurrentDirectory + "\\backup\\CMS_DB.mdf"; FileToDel = "" + System.Environment.CurrentDirectory + "\\backup\\CMS_DB.mdf"; but still getting the error CMS_DB.mdf is being used by another process.. – Raj Sharma Aug 19 '13 at 06:55
  • That's a different issue: You may have the file still open somewhere else. Check if the connections are properly disposed of before you attempt this method. – OnoSendai Aug 19 '13 at 16:57
  • i closed the connection but still i am getting the same error... i tried both con.Dispose() or con.Close()...is there any another way to close the connection properly before backup the database – Raj Sharma Aug 19 '13 at 18:19