日期:2014-05-18  浏览次数:20546 次

SQL2005 出现 System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0
在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()

谢谢!

------解决方案--------------------
对I/O有访问权限,如果是Vista或者Windows 2008,可以使用“管理员”打开VS2005看看呢,需要提升权限
------解决方案--------------------
Request for the permission of type 'System.Security.Permissions.FileIOPermission

--
这一句说得够明白了