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

发布于 2024-11-16 03:29:43 字数 2251 浏览 3 评论 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')

我该如何修复这个错误?

i am working on SQLCLR integration with Visual C# 2010, what i am trying to do is, i have a folder, which contains some images, my function, iterates thru the file in folder, and get the basic file info like its height width etc..

i created a dll from Visual C# 2010 and added as an assembly in Sql Server 2008, and also created function, but when i try to select function i gets the below error..

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)

below is my .net function..

[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];
    }

};

here is how i created assembly in sql server..

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

and created a sql function like below.

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

and calling function like below.

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

How can I fix this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

峩卟喜欢 2024-11-23 03:29:43

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

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

尝试:

 with permission_set = EXTERNAL_ACCESS 

You need "External Access" and not "SAFE" permission set. See:

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

Try:

 with permission_set = EXTERNAL_ACCESS 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文