Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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");
            try
            {
                myServer.ConnectionContext.LoginSecure = false;
                myServer.ConnectionContext.Login = "sa";
                myServer.ConnectionContext.Password = "md22mashkoor";
                myServer.ConnectionContext.Connect();
                Database myDatabase = myServer.Databases["Kolkata_Dental"];
                BackupDatabaseFull(myServer, myDatabase);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.InnerException.Message);
            }
            finally
            {
                if (myServer.ConnectionContext.IsOpen)
                myServer.ConnectionContext.Disconnect();
                Console.WriteLine("Press any key to terminate...");
                Console.ReadKey(); 
                
            }
        }
        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;
            bkpDBFull.SqlBackup(myServer);
 

        }
        private static void CompletionStatusInPercent(object sender, PercentCompleteEventArgs args)
        {
            Console.Clear();
            Console.WriteLine("Percent completed: {0}%.", args.Percent);
        }
        private static void Backup_Completed(object sender, ServerMessageEventArgs args)
        {
            Console.WriteLine("Hurray...Backup completed.");
            Console.WriteLine(args.Error.Message);
        }
        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 */
            restoreDB.SqlRestore(myServer);
        }
        private static void Restore_Completed(object sender, ServerMessageEventArgs args)
        {
            Console.WriteLine("Hurray...Restore completed.");
            Console.WriteLine(args.Error.Message);
        } 
 
    }
}
It showed error is as follows:-
Backup failed for Server 'MASHKOOR-PC'.
Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=9.0.242.0, 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 19:41pm
Edited 25-Jun-13 19:52pm
v2
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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.
 
http://download.microsoft.com/download/4/4/D/44DBDE61-B385-4FC2-A67D-48053B8F9FAD/SQLServer2005_XMO_x64.msi
 
Thanks
--RA
  Permalink  

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 26 Jun 2013
Copyright © CodeProject, 1999-2014
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