Click here to Skip to main content
15,887,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm making the code for Backup, this is my code which iam using in my project, their is an error in executenonquery , check the code and tell me how can i slove this problem

and this error comes
XML
Could not locate entry in sysdatabases for database 'abcd'. No entry found with that name. Make sure that the name is entered correctly.
BACKUP DATABASE is terminating abnormally.

C#
   string dbname = "abcd";
    SqlConnection sqlcon = new SqlConnection();
    SqlCommand sqlcmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();

protected void btn_Click(object sender, EventArgs e)
    {
        //Mentioned Connection string make sure that user id and password sufficient previlages
        sqlcon.ConnectionString= @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\abcd.mdf ;Integrated Security=True;User Instance=True";



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

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

           sqlcmd = new SqlCommand("backup database " + dbname + " to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
            sqlcmd.ExecuteNonQuery();
            //Close connection
            sqlcon.Close();
            Response.Write("Backup database successfully");
        }
      // catch (Exception ex)
        {
            Response.Write("Error During backup database!");
        }
    }
Posted
Updated 5-Oct-12 19:46pm
v2
Comments
Kenneth Haugland 6-Oct-12 2:00am    
Dont use capital letters in your heading, use sentence case. Capital letters are considered shouting, just so you know :-)
Ravimcts 6-Oct-12 2:14am    
aaha.

 
Share this answer
 
Comments
Ravimcts 6-Oct-12 2:52am    
Zoltan Zorgo, the backup is successfully Run but the restore is not working the error is comes is "Exclusive access could not be obtained because the database is in use.
RESTORE DATABASE is terminating abnormally.
Changed database context to 'master'."
Zoltán Zörgő 6-Oct-12 5:55am    
Of course, you can not restore a database you are executing statements in. Be aware to change to (USE ...) a different database, before trying to backup or restore one.
Ravimcts 6-Oct-12 7:59am    
sry, i cant understand what ur trying to tell me.. can u explain in simple way more..
Zoltán Zörgő 6-Oct-12 12:32pm    
Just this: you have to ensure that the database you want to restore is not in use.
plz use cmd.executequerry in place of cmd.executenon querry
 
Share this answer
 
Comments
Ravimcts 6-Oct-12 7:52am    
i think vijay, there is nothing like executequerry
[no name] 8-Oct-12 8:23am    
actually i mean command.executescalar

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900