2

This question is answered many times in Stackoverflow, But I didn't get proper solution for my project.

Let me show you my code first:

namespace ConsoleDBManagement
{
    class Program
    {
        static void Main(string[] args)
        {
            //Metioned here your database name
            string dbname = "newDb";
            SqlConnection sqlcon = new SqlConnection();
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();

            sqlcon.ConnectionString = @"Server=ABC-PC\SQLEXPRESS;database=" + dbname + ";uid=dran;pwd=sri;";

            //Enter destination directory where backup file stored
            string destdir = "D:\\Working Projects";

            //Check that directory already there otherwise create 
            if (!System.IO.Directory.Exists(destdir))
            {
               System.IO.Directory.CreateDirectory("D:\\Working Projects");
            }
            try
            {
                //Open connection
                sqlcon.Open();
                //query to take backup database
                //System.IO.File.Create("D:\\Working Projects\\FullBackUp.BAK");

                sqlcmd = new SqlCommand("backup database newDb to disk='" + destdir + "\\FullBackUp.BAK'", sqlcon);
                sqlcmd.ExecuteNonQuery();
                //Close connection
                sqlcon.Close();
                //Response.Write("Backup database successfully");
            }
            catch (Exception ex)
            {
                //Response.Write("Error During backup database!");
            }
        }
    }
}

I am getting exception while query executed.

Cannot open backup device 'D:\Working Projects\FullBackUp.BAK'. Operating system error 3 (The system cannot find the path specified.).

BACKUP DATABASE is terminating abnormally.

Please give me your suggestions.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Trusha Savsani
  • 489
  • 1
  • 11
  • 31
  • 1
    does the folder `D:\Working Projects\ ` exist on the machine named `ABC-PC`? Sql uses the directory of the server running the sql, not the client connecting to it. – Scott Chamberlain Oct 28 '15 at 05:10
  • Silly question, but that directory definitely exists on your file system? – Steve Oct 28 '15 at 05:10
  • Yes my folder d:\Working Projects\ is there. – Trusha Savsani Oct 28 '15 at 05:13
  • Does `System.IO.Directory.Exists(destdir)` return true? And where does the error actually occur? – Steve Oct 28 '15 at 05:21
  • Thanks to all for reply. Yes I am getting true from System.IO.Directory.Exists(destdir) – Trusha Savsani Oct 28 '15 at 05:36
  • And I am getting error from "sqlcmd.ExecuteNonQuery();" statement – Trusha Savsani Oct 28 '15 at 05:39
  • 2
    Maybe there are no permissions to this folder/disk for sql server account. You have permissions so your program, which runs under your account, has access to this folder. But sql server may runs under another account that doesn't have permissions – ventik Oct 28 '15 at 06:06

1 Answers1

1

When you run backup and/or other external file related commands when using a SQL auth login, the Windows security context is that of the SQL Service.

Your question is duplicate of Backup Permissions. Either grant permission for the SQL Service account/group, or run the backups using a Windows auth login that has permissions to the path.

Community
  • 1
  • 1
Andrew Loree
  • 263
  • 1
  • 8