【问题标题】:SQLCLR and Microsoft.SqlServer.Management.Smo.Server..ctor()SQLCLR 和 Microsoft.SqlServer.Management.Smo.Server..ctor()
【发布时间】:2014-06-29 21:42:35
【问题描述】:

我在用 c# 编写的 clr 存储过程中遇到了很大的问题。我已经成功安装了程序集,但是当我调用存储过程时出现以下错误:-

Msg 6522, Level 16, State 1, Procedure RestoreDatabaseClr, Line 0

A .NET Framework error occurred during execution of user-defined routine or     aggregate "RestoreDatabaseClr": 

System.Exception: This functionality is disabled in the SQLCLR. It is recommended that you execute from your client application.
System.Exception: 
at Microsoft.SqlServer.Management.Common.ConnectionManager..ctor()
at Microsoft.SqlServer.Management.Common.ServerConnection..ctor()
at Microsoft.SqlServer.Management.Smo.ExecutionManager..ctor(String name)
at Microsoft.SqlServer.Management.Smo.Server..ctor()
at DBRestoreClr.StoredProcedures.RestoreDatabaseClr(String backupFile, String dbName, String dataFiles, String logFiles)

.

这是我试图在存储过程中执行的 c# 代码:-

using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Server;

namespace DBRestoreClr
{
    public partial class StoredProcedures
    {
        [SqlProcedure()]
        public static int RestoreDatabaseClr(string backupFile, string dbName, string dataFiles, string logFiles)
        {
             var sqlServer = new Server();
        }
    }
}

我正在使用下面的执行

EXEC @ReturnValue RestoreDatabaseClr 'C:\Workspace\DEV\DBRestoreDatabase\tests\Clr\restore.bak', 
                            'restore',
                            'C:\2008\MSSQL10.MSSQLSERVER\MSSQL\DATA\restore',
                            'C:\2008\MSSQL10.MSSQLSERVER\MSSQL\DATA\restore\logs';

有人知道为什么它不起作用吗?

谢谢

安德鲁

【问题讨论】:

  • 为什么要为RESTORE DATABASE T-SQL 命令添加非标准替换?
  • 因为我们的现场有大约 100 个 db 文件(inc 日志),我们有时需要将此还原到 dev,所以我想编写一个 clr 来遍历备份中的这些 db 文件并移动它们恢复到新位置。 smo 类提供者为此提供了完美的库,因此 clr.我现在找到了一种不同的方法 - 我坚持使用 clr 但删除 smo 类并使用内联 sql 即“RESTORE FILESIONLY”与 SqlConnection 和 SqlCommand 以及 SqlDataAdapters\DataSets 循环通过文件 - 完美,跨度>
  • 我动态生成恢复语句,并在连接的 sql 字符串中使用“WITH MOVE”传递给它。

标签: c# sql-server-2008 sqlclr


【解决方案1】:

因为“此功能在 SQLCLR 中被禁用”。 SMO 框架检测到您在 SQL CLR 中并拒绝工作。这样做的原因可能是微软让这一切正常工作的成本很高,这是一个正当的理由。

您为什么不按照消息中的建议从客户端程序中恢复数据库?

【讨论】:

    猜你喜欢
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 2016-04-18
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多