Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have a form and there is two Button in my form one for Select Folder and 2nd for take back up of database which is currently used for my project i want to take backup of my database on backup button click.
and also take Restore database using C#
Any One Help me.
I'm trying this code button i am facing error.
Thanks in Advance.

error is
system.sql.sqlclint.sqlexception:Cannot open Backup device.
operating system error. access denied.
Backup database terminating abnormally


What I have tried:

C#
using (SqlConnection conn4 = new SqlConnection(DbConfigClass.connectDb()))
            {
                string database = conn4.Database.ToString();
                try
                {
                    if (PathtextBox.Text == string.Empty)
                    {

                    }
                    else
                    {                       
                        string cmd = "BACKUP DATABASE ["+database+"] TO DISK '"+PathtextBox.Text+"\\"+"Database"+"-"+DateTime.Now.Ticks.ToString("yyyy-MM-dd--HH-mm-ss")+ ".back'";
                        using (SqlCommand comand = new SqlCommand(cmd, conn4))
                        {
                            if (conn4.State != ConnectionState.Open)
                            {
                                conn4.Open();
                            }
                            comand.ExecuteNonQuery();
                            conn4.Close();
                            MessageBoxClass.ShowSucessMessage("Successful to backup data","Successful");
                        }
                    }
                }
                catch (Exception es)
                {
                    MessageBoxClass.ShowErrorMessage("" + es, "");
                }
            }
Posted
Updated 26-Apr-18 1:09am
v4
Comments
Richard Deeming 25-Apr-18 15:49pm    
If you want someone to help you fix an error, then you need to tell us what the error is.

Click the green "Improve question" link and add the full details of the error to your question.
Fahid Zahoor 26-Apr-18 7:08am    
system.sql.sqlclint.sqlexception:Cannot open Backup device.
operating system error. access denied.
Backup database terminating abnormally
Richard Deeming 26-Apr-18 9:08am    
The account that SQL is running under doesn't have access to the path you've specified.

Either specify a path which the account does have access to, or change the permissions on the path you're using.
Fahid Zahoor 26-Apr-18 13:11pm    
please mention this error in my coding thanks
Richard Deeming 26-Apr-18 13:12pm    
The "error" is that you're trying to backup to a folder which SQL can't write to.

Choose a different folder.

1 solution

 
Share this answer
 

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