在写一个CRL存储过程时,里面调用了IO读文件:
StreamReader sr = File.OpenText(strFileName);
可是调试跟踪到这里就断掉,异常信息为:
System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.查了相关资料后,在代码里加了2句:
FileIOPermission perm = new FileIOPermission(FileIOPermissionAccess.AllAccess, strFileName);
        perm.Demand();        StreamReader sr = File.OpenText(strFileName);
可是好象没有作用……
请各位大大帮帮忙解决下!如何在CRL存储过程中进行文件操作呢?

解决方案 »

  1.   

    汗一个,写错了!
    是CLR……
      

  2.   

    是不是WEBFORM和WINFORM没有关系,因为根本没有界面啊……
    只是一个CLR存储过程!
    编译后和普通的存储过程一样!
    VS2005里的SQL项目!
      

  3.   

    部署到SQL2005中运行提示如下错误:
    消息 6522,级别 16,状态 1,过程 WebCount,第 0 行
    在执行用户定义例程或聚合 'WebCount' 期间出现 .NET Framework 错误: 
    System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
    System.Security.SecurityException: 
       at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
       at System.Security.CodeAccessPermission.Demand()
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
       at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
       at System.IO.StreamReader..ctor(String path)
       at System.IO.File.OpenText(String path)
       at StoredProcedures.WebCount(String strLogFilePath)
      

  4.   

    --是数据登录的权限不够.添加程序集的时候.需要有"UNSAFE" 权限的.CREATE ASSEMBLY tvfEventLog 
    FROM 'D:\IISWebRoot\SQLCLR\MyDbApp\MyDbApp\bin\Release\MyDbApp.dll' 
    WITH  PERMISSION_SET = UNSAFE
    GO
      

  5.   

    谢谢楼上的,按照你的说法做了,但是提示如下错误:CREATE ASSEMBLY WebCountSql 
    FROM 'F:\MyExe\WebCountSql\bin\Debug\WebCountSql.dll' 
    WITH  PERMISSION_SET = UNSAFE
    GO
    消息 10327,级别 14,状态 1,第 1 行
    针对程序集 'WebCountSql' 的 CREATE ASSEMBLY 失败,因为程序集 'WebCountSql' 未获授权(PERMISSION_SET = UNSAFE)。 当符合以下两个条件之一时,将对程序集授权: 数据库所有者(DBO)具有 UNSAFE ASSEMBLY 权限,而且数据库的 TRUSTWORTHY 属性处于打开状态;或者,程序集签名时所使用的证书或非对称密钥所对应的登录名具有 UNSAFE ASSEMBLY 权限我是用的WINDOWS身份登录的SQL!
    还是不明白怎么授权啊?