Click here to Skip to main content
13,052,342 members (56,366 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
I wrote codes to create Backup of SQL Server 2005 database and to restore it again.
It runs properly but showing some error.
My code is as follows:-
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            Server myServer = new Server(@"MASHKOOR-PC");
                myServer.ConnectionContext.LoginSecure = false;
                myServer.ConnectionContext.Login = "sa";
                myServer.ConnectionContext.Password = "md22mashkoor";
                Database myDatabase = myServer.Databases["Kolkata_Dental"];
                BackupDatabaseFull(myServer, myDatabase);
            catch (Exception ex)
                if (myServer.ConnectionContext.IsOpen)
                Console.WriteLine("Press any key to terminate...");
        private static void BackupDatabaseFull(Server myServer, Database myDatabase)
            Backup bkpDBFull = new Backup();
            bkpDBFull.Action = BackupActionType.Database;
            bkpDBFull.Database = myDatabase.Name;
            bkpDBFull.Devices.AddDevice(@"D:\KolkataDentalFull.bak", DeviceType.File);
            bkpDBFull.BackupSetName = "KolkataDental database Backup";
            bkpDBFull.BackupSetDescription = "KolkataDental database - Full Backup";
            bkpDBFull.ExpirationDate = DateTime.Today.AddDays(1000);
            bkpDBFull.Initialize = false;
            bkpDBFull.PercentComplete += CompletionStatusInPercent;
            bkpDBFull.Complete += Backup_Completed;

        private static void CompletionStatusInPercent(object sender, PercentCompleteEventArgs args)
            Console.WriteLine("Percent completed: {0}%.", args.Percent);
        private static void Backup_Completed(object sender, ServerMessageEventArgs args)
            Console.WriteLine("Hurray...Backup completed.");
        private static void RestoreDatabase(Server myServer, Database myDatabase)
            Restore restoreDB = new Restore();
            restoreDB.Database = myDatabase.Name;
            /* Specify whether you want to restore database or files or log etc */
            restoreDB.Action = RestoreActionType.Database;
            restoreDB.Devices.AddDevice(@"D:\KolkataDentalFull.bak", DeviceType.File);
            /* You can specify ReplaceDatabase = false (default) to not create a new image * 
             * of the database, the specified database must exist on SQL Server instance. * 
             * If you can specify ReplaceDatabase = true to create new database image *
             * regardless of the existence of specified database with same name */
            restoreDB.ReplaceDatabase = true;
            /* If you have differential or log restore to be followed, you would need * 
             * to specify NoRecovery = true, this will ensure no recovery is done after the * 
             * restore and subsequent restores are allowed. It means it will database * in the Restoring state. */
            restoreDB.NoRecovery = true;
            /* Wiring up events for progress monitoring */
            restoreDB.PercentComplete += CompletionStatusInPercent;
            restoreDB.Complete += Restore_Completed;
            /* SqlRestore method starts to restore database * You cab also use SqlRestoreAsync method to perform restore * operation asynchronously */
        private static void Restore_Completed(object sender, ServerMessageEventArgs args)
            Console.WriteLine("Hurray...Restore completed.");

It showed error is as follows:-
Backup failed for Server 'MASHKOOR-PC'.
Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. An attempt was made to load a program with an incorrect format.
I am unable to find out the reason. Kindly help me.
Posted 25-Jun-13 18:41pm
Updated 25-Jun-13 18:52pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to install the 64-bit components for SQL Server 2005.

You can download them from here.[^]

The error is because you are running a 64 bit process and trying to load an x86 DLL. You can also try recompiling your application to be x86 (Properties->Build) and run it again. You probably have it set to AnyCPU right now.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You need to install the Microsoft SQL Server 2005 Management Objects Collection, it should be able to solve the problem.

Download from this link and install.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Jun 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100