Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In my asp.net web application, i want to add button to backup my project database. now i want to click backup button then a "myDB.bak" file stored in in computer. how can i do it. how can i do it for code fast method in asp.net mvc.

What I have tried:

C#
Public ActionResult BackupDatabase()
        {
var dbPath = Server.MapPath("~/App_Data/DBBackup.bak");
using (var db = new ProjectDbContext())
        {
var cmd = String.Format("BACKUP DATABASE {0} TO DISK='{1}' WITH FORMAT, MEDIANAME='DbBackups', MEDIADESCRIPTION='Media set for {0} database';"
                            , "YourDB", dbPath);
db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, cmd);
                    }
Posted
Updated 2-Dec-16 2:32am
Comments
Ehsan Sajjad 2-Dec-16 7:32am    
what is happening right now with this way?
Philippe Mori 2-Dec-16 10:58am    
You probably don't want backup/restore code to be in your ASP.NET MVC code. How would you do a restore when the site is not working because the database is corrupted. Obviously, you don't want to show such command to all users and thus the site would depends on the logged user which will typically depends on a working database.

1 solution

Or, you could do this: Backing up an SQL Database in C#[^]
 
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