It is possible yes.
You need to mount in your C# code the sql command of backup and run it, something like:
Open the connection and mount a query which executes the command to do the backup as the desired way to save your backup
public void ExecutarBackup(string caminho)
{
suaConexao = new SqlConnection(...);
var query = "Backup database NomeSeuBanco to disk='" + caminho + "'";
var cmd = new SqlCommand(query, suaConexao);
cmd.ExecuteNonQuery();
}
Edition/Complement:
And how would I put it in a view ? Like a button ? Or something like that.
Your View can have a control for the user to inform the path in which he wants to save the backup and a button. When clicking the button occurs the Post for a Action of your Controller, passing the path the user wants to save the backup as a parameter.
That one Action can call a service/method to generate the backup.
[HttpPost]
public ActionResult SalvarBakup(SeuViewModel seuViewModel)
{
_servicoBackup.ExecutarBackup(seuViewModel.Caminho);
return View();
}
Below exemplifies the View:
@model SeuViewModel
@using (Html.BeginForm("SalvarBakup", "SeuController"))
{
@Html.TextBoxFor(model => model.Caminho)
<input type="submit" value="Salvar Backup" />
}
Related: http://answall.com/questions/39978/backup-database-sqlserver
– gmsantos
I believe that if we base the question linked by @gmsantos (as well as the answer given there), just call those commands with the method Sqlcommand.Executenonquery. But I’m not 100% sure. If someone checks that this is correct and posts as an answer, they will have my +1.
– Oralista de Sistemas
Doing this by code c# ? Because what I saw in the answer is only by SQL Server itself.
– Érik Thiago
Anyone else? Is there then any way to make a script and put in the project and run that script via application ?
– Érik Thiago
I wanted to understand why an application has to generate and restore the backup of a database. It doesn’t make any sense to me.
– Leonel Sanches da Silva
I don’t need to restore, only generate. I don’t know either, but if the client asked, I’ll put right. I even understand. Even if the bank creates, and this is automated, it would be one more way to generate the backup if it doesn’t work the other way.
– Érik Thiago
And how do I restore or use a certain backup but by the view also?
– Alvim Praia