1

I want to add a simple backup and restore option to my ASP.Net WebApplication. I created the procedure below in my database:

CREATE PROCEDURE dbo.[BackUp]
    @path NVARCHAR(MAX)
AS 
    DECLARE @DataBaseName NVARCHAR(MAX)  = DB_NAME()

    BACKUP DATABASE @DataBaseName
    TO DISK = @path
    WITH FORMAT,
    MEDIANAME = 'Backup'
GO

I created my model using entity framework. the procedure is:

public virtual int BackUp(string path)
{
    var pathParameter = path != null ?
    new ObjectParameter("path", path) :
    new ObjectParameter("path", typeof(string));
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("BackUp", pathParameter);
}

But when I call Backup, I see this error:

Cannot perform a backup or restore operation within a transaction. BACKUP DATABASE is terminating abnormally.

enter image description here

3
  • What's unclear about the error message? Commented Jun 26, 2014 at 19:37
  • I don't know that what is the meaning of that error message and how can i solve that!! Commented Jun 27, 2014 at 5:15
  • 1
    Seems kind of clear. You have a transaction open and the BACKUP statement cannot be run under a transaction. Find out, who opened that transaction. Commented Jun 27, 2014 at 9:29

1 Answer 1

2

I solved that problem by changing Backup function to this:

public void BackUp()
    {

        Kimiakesht entity = new Kimiakesht();
        string dataTime = DateTime.Now.ToString("yyyy-MM-dd") + "-" + DateTime.Now.ToString("HH-mm");
        string directory = HttpContext.Current.Server.MapPath("~/") + "/backups/" + dataTime + "/";
        string fileName = directory + dataTime + ".bak";

        #region Response
        HttpResponse Response = HttpContext.Current.Response;
        Response.Clear();
        Response.BufferOutput = false;
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "inline; filename=\"" + dataTime + "\".zip");
        #endregion

        if (!Directory.Exists(directory))
            Directory.CreateDirectory(directory);


        // Here the procedure is called and executes successfully
        entity.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, "EXEC [dbo].[BackUp] @path = N'" + fileName + "'");

        #region Compress
        using (var memoryStream = new System.IO.MemoryStream())
        {
            using (ZipFile zip = new ZipFile())
            {
                zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
                zip.ParallelDeflateThreshold = -1;
                zip.AddDirectory(directory);
                zip.Save(memoryStream);
            }

            memoryStream.Position = 0;
            var b = new byte[1024];
            int n;
            while ((n = memoryStream.Read(b, 0, b.Length)) > 0)
                Response.OutputStream.Write(b, 0, n);
        }
        #endregion

        Directory.Delete(directory, true);

        Response.Close();
    }

This function create backup from database, compress it and then return it to download as a HttpResponse. Finally deletes the temp directory

UPDATE

this is stored procedure content:

ALTER PROCEDURE [dbo].[BackUp]
    @path NVARCHAR(MAX)
AS 
    DECLARE @DataBaseName NVARCHAR(MAX)  = DB_NAME()

    BACKUP DATABASE @DataBaseName
    TO DISK = @path
    WITH FORMAT,
    MEDIANAME = 'Z_SQLServerBackups'
Sign up to request clarification or add additional context in comments.

1 Comment

Nice solution. could you explain how you made stored procedure on SQL and sample code.thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.