0

我正在研究 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')

我该如何解决这个错误?

4

1 回答 1

0

您需要“外部访问”而不是“安全”权限集。看:

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

尝试:

使用权限设置 = EXTERNAL_ACCESS 
于 2011-06-17T20:06:33.093 回答