ASP.NET - database backup on button click in asp.net

Asked By Reena Jain on 19-Nov-09 02:04 AM
Hi,
I want to take the database backup on a button click from asp.net front end. I'm using vs 2008 and sql server 2005.Can any one suggest me a proper method or complete code to do the same .

thanks in advance

database backup on button click in asp.net - mv ark replied to Reena Jain on 19-Nov-09 02:30 AM

Check this article - http://www.dotnetfunda.com/articles/article489-backup-and-restore-database-in-aspnet.aspx


Re - Huggy Bear replied to Reena Jain on 19-Nov-09 02:33 AM

Below article gives you the complete instructions for Backup and restoring the sql server database using C#.net

http://www.geekpedia.com/tutorial180_Backup-and-restore-SQL-databases.html

no, i want backup of whole database not of tables - Reena Jain replied to mv ark on 19-Nov-09 02:55 AM

and for it i'm using the following coding
string connStr = ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString.ToString();
 protected void BackUpNow(object sender, EventArgs e)
    {

 using (SqlConnection conn = new SqlConnection(connStr))
            {
                 string query="backup database phcdogser1 To Disk='" + Server.MapPath("~/backup") + "\\fileBackup.bak'";
                 using (SqlCommand cmd = new SqlCommand(query, conn))
                 {
                     conn.Open();
                     cmd.ExecuteNonQuery();
                     conn.Close();
                   
                 }

            }
        

            lblMessage.Text = "Backup for  <b>Database</b> successful!";
}
 this query run perfectly in sql server but on button click in asp.net its give following error


BACKUP DATABASE permission denied in database 'phcdogser1'.
BACKUP DATABASE is terminating abnormally
Re - Huggy Bear replied to Reena Jain on 19-Nov-09 03:11 AM
This error is being thrown because the user credentials that you use in the connection string doesn't have the database backup permission. Provide the backup access of the database to the user in the database connection string that you use in your asp.net application.
now i'm getting the following error - Reena Jain replied to Huggy Bear on 19-Nov-09 05:33 AM
Thanks for reply too soon but its giving me the following error
System.Data.SqlClient.SqlException: Cannot open backup device 'd:\hshome\syscraft\mlmpages.com\backup\MLMBackup.bak'. Operating system error 3(The system cannot find the path specified.). BACKUP DATABASE is terminating abnormally. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at DatabaseAdmin.BackUpNow(Object sender, EventArgs e) in d:\hshome\syscraft\mlmpages.com\DatabaseAdmin.aspx.cs:line 33

please reply if any idea
Re - Huggy Bear replied to Reena Jain on 19-Nov-09 05:51 AM
Make sure the path d:\hshome\syscraft\mlmpages.com\backup\ exists and it has sufficient permission for SQL server to write the .bak file in there.
you are right might be its permission problem - Reena Jain replied to Huggy Bear on 20-Nov-09 12:59 AM
how to set permission then because i've given permission in sql server and in folder also. then also problem is same.
Re - Huggy Bear replied to Reena Jain on 20-Nov-09 01:28 AM
Add the ASPNET user and provide read and write permissions onto the output folder. Lets hope for the good.
how should can i do it - Reena Jain replied to Huggy Bear on 20-Nov-09 01:51 AM
plz provide any example, because i've done everything but problem is same even on another live server, this error is coming after click on back up button

System.Data.SqlClient.SqlException: Cannot open backup device 'D:\Hosting\4696353\html\backup\MLMBackup.bak'. Operating system error 3(The system cannot find the path specified.). BACKUP DATABASE is terminating abnormally. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at DatabaseAdmin.BackUpNow(Object sender, EventArgs e) in d:\hosting\4696353\html\DatabaseAdmin.aspx.cs:line 33
Re - Huggy Bear replied to Reena Jain on 20-Nov-09 02:15 AM
Reena if you check http://forum.dotnetpanel.com/forums/p/642/6284.aspx posts, it clearly witnesses that the SQL user account should be provided with permissions to the target folder.

Another thing, Do you have the empty <yourbackupfilename>.bak file in target folder? If not please create one and try.