如何在 smo 中无一例外地使用 smo 恢复您的 SQL 服务器数据库
How restore your SQL Server database with smo without exception in smo
为什么在 WPF 中使用 SMO 恢复 SQL 服务器数据库时出现异常?
这是我的代码:
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");
我得到这个异常:
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
在使用 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);
}
Internet 上有一个资源描述了如何使用 SMO 完成 backup/restore 操作。
查看
Programming SQL Server with SQL Server Management Objects Framework
为什么在 WPF 中使用 SMO 恢复 SQL 服务器数据库时出现异常?
这是我的代码:
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");
我得到这个异常:
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
在使用 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);
}
Internet 上有一个资源描述了如何使用 SMO 完成 backup/restore 操作。
查看 Programming SQL Server with SQL Server Management Objects Framework