I’m having a massive problem with my clr stored proc written in c#. I have successfully installed the assembly but when i call the stored proc i get the below error:-
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)
.
This is c# code i am trying to execute with in the sproc:-
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();
}
}
}
i’m executing using the below
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';
Any one have any ideas why its not working?
Thanks
Andrew
Because “This functionality is disabled in the SQLCLR”. The SMO framework detects that you are in SQL CLR and refuses to work. The reason for that is probably that it was to costly for Microsoft to make it all work, which is a valid reason.
Why don’t you just restore the database from your client program, just as the message suggests?