При работе с SMO, когда возникает исключение, обычно самое внутреннее содержит информацию, которая вам нужна.
Чтобы собрать весь текст исключения, вам нужно пройти через иерархию исключений.
Это может быть достигнуто небольшим расширением, как показано во фрагменте ниже.
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);
}
}
}
Затем, после помещения вашего кода в блок try-catch и в блок catch, соберите все тексты следующим образом
catch (Exception ex)
{
errMessage = string.Join(Environment.NewLine + "\t", ex.CollectThemAll(ex1 => ex1.InnerException)
.Select(ex1 => ex1.Message));
Console.WriteLine(errMessage);
}
В Интернете есть ресурс, который описывает, каквыполнено резервное копирование / восстановление с помощью SMO.
Извлечение Программирование SQL Server с помощью инфраструктуры объектов управления SQL Server