在vs2005下编写的存储过程代码如下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Xml;
public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void StoredProcedure1()
    {
        SqlConnection cnn = new SqlConnection("context connection=true");
        cnn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cnn;
        cmd.CommandText = "select * from UserTest for xml raw('pesron')";
        XmlReader xr = cmd.ExecuteXmlReader();
        DataSet ds = new DataSet();
        ds.ReadXml(xr);
        ds.WriteXml("d://wu.xml");
    }
};在sql2005下执行该存储过程时,出现以下错误:
消息 6522,级别 16,状态 1,过程 StoredProcedure1,第 0 行
在执行用户定义例程或聚合 'StoredProcedure1' 期间出现 .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)
   at System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding)
   at System.Data.DataSet.WriteXml(String fileName, XmlWriteMode mode)
   at System.Data.DataSet.WriteXml(String fileName)
   at StoredProcedures.StoredProcedure1()

谢谢!

解决方案 »

  1.   

    对I/O有访问权限,如果是Vista或者Windows 2008,可以使用“管理员”打开VS2005看看呢,需要提升权限
      

  2.   

    Request for the permission of type 'System.Security.Permissions.FileIOPermission--
      这一句说得够明白了
      

  3.   

    问题解决了,在sql2005运行:ALTER ASSEMBLY <you_assembly_name> WITH PERMISSION_SET=EXTERNAL_ACCESS 就好了
      

  4.   

    我也遇到的問題也差不多,是Winfrom寫的exe文件,在本地運行沒有問題,但是放在網絡盤上運行就出錯:“Security.SecurityException: 型別 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 的使用權限要求失敗。“但是不知道如何解決,繼續關注中!!