简体   繁体   中英

How restore your SQL Server database with smo without exception in smo

Why am I getting an exception when restoring a SQL Server database using SMO in WPF?

This is my code:

ServerConnection conRestore = new ServerConnection(LaServerInstance);

Server ServerRestore = new Server(conRestore);
Restore RestoreObject = new Restore();

RestoreObject.Action = RestoreActionType.Database;
RestoreObject.Database = "Mainwaterphantom";

BackupDeviceItem source = new BackupDeviceItem(open.FileName, DeviceType.File);

RestoreObject.Devices.Add(source);
RestoreObject.ReplaceDatabase = true;

RestoreObject.SqlRestore(ServerRestore);

MessageBox.Show("Successful Restore");

and I get this exception:

An exception of type Microsoft.SqlServer.Management.Smo.FailedOperationException occurred in Microsoft.SqlServer.SmoExtended.dll but was not handled in user code. The innerexception give me this: Restore failed to server 'Data source=L1038\parvaresh;Initial catalog=Mainwaterphantom;integrated security=true

When working with SMO, when an exception arises, usually, the most inner one contains the information that you need.

In order to collect all exception text, you have to travel through the exception hierarchy.

That could be accomplished by a small extension as shown in the snippet below.

using System;
using System.Collections.Generic;

namespace Converter.Extension
{
    /// <summary>
    /// When working with SMO, when an exception arises, 
    /// usually, the most inner one contains the information that you need. 
    /// In order to collect all exception text, you have to travel through the exception hierarchy.
    /// </summary>
    public static class ExceptionExtensionMethods
    {

        public static IEnumerable<TSource> CollectThemAll<TSource>(
        this TSource source,
        Func<TSource, TSource> nextItem,
        Func<TSource, bool> canContinue)
        {
            for (var current = source; canContinue(current); current = nextItem(current))
            {
                yield return current;
            }
        }

        public static IEnumerable<TSource> CollectThemAll<TSource>(
            this TSource source,
            Func<TSource, TSource> nextItem)
            where TSource : class
        {
            return CollectThemAll(source, nextItem, s => s != null);
        }
    }

}

Then, after put you code in try-catch block and in the catch block collect all texts as follows

catch (Exception ex)
      {


              errMessage = string.Join(Environment.NewLine + "\t", ex.CollectThemAll(ex1 => ex1.InnerException)
                    .Select(ex1 => ex1.Message));
Console.WriteLine(errMessage);
}

There is a resource on the Internet that describe how to accomplished backup/restore operation by using SMO.

Check out Programming SQL Server with SQL Server Management Objects Framework

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM