0

I want to create backup of particular database using C# code, and for that I am using following code.

Code

       Console.WriteLine("Backup manager");

        Backup bkpDBFull = new Backup();

        bkpDBFull.Action = BackupActionType.Database;

        bkpDBFull.Database = "SampleDB";

        bkpDBFull.Devices.AddDevice(@"D:\Temp\Back\fulBak.bak", DeviceType.File);
        bkpDBFull.BackupSetName = "Database backup";
        bkpDBFull.BackupSetDescription = "Code backup";
        bkpDBFull.Initialize = false;

        bkpDBFull.PercentComplete += CompletionStatusInPercent;
        bkpDBFull.Complete += Backup_Completed;

        Server MyServer = new Server("Data Source=.;Initial Catalog=SampleDB;User ID=user;Password=pass");

        bkpDBFull.SqlBackup(MyServer);

and the two event's are following.

CompletionStatusInPercent

    private static void CompletionStatusInPercent(object sender, PercentCompleteEventArgs args)
    {
        Console.Clear();
        Console.WriteLine("Percent completed: {0}%.", args.Percent);
    }

Backup_Completed

    private static void Backup_Completed(object sender, ServerMessageEventArgs args)
    {
        Console.WriteLine("Hurray...Backup completed.");
        Console.WriteLine(args.Error.Message);
    }

In the entire code it getting a only one problem in below code

Problem code

        Server MyServer = new Server("Data Source=.;Initial Catalog=SampleDB;User ID=user;Password=pass");

I think I had put name of sqlserver in wrong way.

I need a solution of how can I fix this issue.

2 Answers 2

2

You've mixed up the constructors. The argument for new Server, is either the name of the server (string), or an instance of the Microsoft.SqlServer.Management.Common.ServerConnection class in the smo name space. It's constructor takes a connectionstring as an argument.

Sign up to request clarification or add additional context in comments.

Comments

0

As here, Tony Hopkinson I had made more research and come up with a solution...

I just simple remove my Problematic code with following code...

        SqlConnection con = new SqlConnection(@"Integrated Security=SSPI; Data Source=.");
        ServerConnection sc = new ServerConnection(con);
        Server MyServer = new Server(sc);

So, I hope this will be useful to many other...

Comments

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.