SQLCLR与Visual C#集成的安全问题

时间:2011-06-17 19:19:11

标签: asp.net sqlclr securityexception

我正在使用SQLCLR与Visual C#2010集成,我想要做的是,我有一个文件夹,其中包含一些图像,我的功能,通过文件夹中的文件进行迭代,并获得基本文件信息,如高度宽度等。

我从Visual C#2010创建了一个dll,并在Sql Server 2008中添加了一个程序集,并且还创建了函数,但是当我尝试选择函数时,我得到了以下错误..

A .NET Framework error occurred during execution of user-defined routine or aggregate "fn_GetFiles":   
System.Security.SecurityException: Request failed.  
System.Security.SecurityException:   
   at UserDefinedFunctions.GetFileList(String FolderPath)  
   at UserDefinedFunctions.GetFileInfo(String folderPath)

下面是我的.net功能..

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]  
public partial class UserDefinedFunctions  
{  
    [SqlFunction(FillRowMethodName = "FillRow")]  
    public static IEnumerable GetFileInfo(string folderPath)  
    {  
        // Put your code here
        return GetFileList(folderPath);
    }

    public static ArrayList GetFileList(string FolderPath)
    {
        ArrayList li = new ArrayList();
        foreach (String s in Directory.GetFiles(FolderPath, "*.jpg"))
        {
            FileInfo info = new FileInfo(s);
            object[] column = new object[3];
            column[0] = Path.GetFileName(s);
            column[1] = info.Length;
            column[2] = s;

            li.Add(column);
        }
        return li;
    }

    private static void FillRow(Object obj, out string filename, out string fileSize, out string filePath)
    {
        object[] row = (object[])obj;
        filename = (string)row[0];
        fileSize = (string)row[1];
        filePath = (string)row[2];
    }

};

这是我如何在sql server中创建程序集..

CREATE assembly GetFileList from 'E:\NBM Sites\DontDelete\SampleCLRIntegration.dll' with permission_set = safe

并创建了一个像下面这样的sql函数。

ALTER FUNCTION fn_GetFiles   
(  
    @folderPath nvarchar(max)  
)  
RETURNS TABLE   
(  
    [filename] nvarchar(max),  
    fileSize nvarchar(max),  
    filePath nvarchar(max)  
)  
AS EXTERNAL NAME GetFileList.UserDefinedFunctions.GetFileInfo;  

并调用函数如下。

select * from dbo.fn_GetFiles('E:\NBM Sites\DontDelete')

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

您需要“外部访问”而非“安全”权限集。参见:

http://msdn.microsoft.com/en-us/library/ms345106.aspx

尝试:

 with permission_set = EXTERNAL_ACCESS