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.