日期:2014-05-18 浏览次数:21034 次
在执行用户定义例程或聚合 'sp_sync_data' 期间出现 .NET Framework 错误: System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. System.Security.SecurityException: at StoredProcedures.SyncData() 。
using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.IO; using System.Net; using System.Text; using System.Collections; using System.Security.Permissions; public partial class StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] public static void SyncData() { string conn_string = "context connection = true"; string uri = "{0}?o={1}&r={2}"; string sql = "select syncuri,oid,reservea from mydatatable"; SqlDataAdapter sda = new SqlDataAdapter(sql, conn_string); try { DataTable dt = new DataTable(); sda.Fill(dt); for (int i = 0; i < dt.Rows.Count; i++) { string syncuri = dt.Rows[i]["syncuri"].ToString(); string oid = dt.Rows[i]["oid"].ToString(); string reservea = dt.Rows[i]["reservea"].ToString(); string syncresult = GetHttpRequest(string.Format(uri, syncuri, oid, reservea)); if (syncresult == "1") { //etc } } } catch (Exception ex) { throw ex; } } private static string GetHttpRequest(string uri) { #region 这样也不成,晕 WebPermission oWebPermision = new WebPermission(PermissionState.None); oWebPermision.AddPermission(NetworkAccess.Connect, uri); oWebPermision.AddPermission(NetworkAccess.Accept, uri); oWebPermision.Demand(); #endregion string strResult = string.Empty; WebRequest oRequest = null; WebResponse oResponse = null; StreamReader oStreamReader = null; try { oRequest = WebRequest.Create(uri); oRequest.ContentType = "application/x-www-form-urlencoded"; oRequest.Method = "GET"; oResponse = oRequest.GetResponse(); oStreamReader = new StreamReader(oResponse.GetResponseStream(), Encoding.Default); strResult = oStreamReader.ReadToEnd(); oStreamReader.Close(); oStreamReader = null; oResponse.Close(); oResponse = null; } catch(Exception ex) { throw ex; } return strResult; } };