Request for the permission of type System.Data.SqlClientPermission,System.Data,Version=1.0.5000.0,Culture=neutral,PublicKeyToken=b77a5c561934e089 failed.
后来在一个有关SPS的英文论坛中找到了解决的方法:
打开在SPS服务器的虚拟目录中web.config文件,在该文件中有个节点:
<trustLevel name="WSS_Medium" policyFile="C:Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\config\wss_minimaltrust.config"/>,
该节点记录了wss_minimaltrust.config文件所在的目录,根据这个目录找到该文件。
然后在wss_minimaltrust.config文件的<SecurityClass>节点中加入子节点:
<SecurityClass Name="SqlClientPermission" Description="System.Data.SqlClient.SqlClientPermission, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>,
再在<PermissionSet>节点中加入子节点:
<IPermissionSet class="SqlClientPermission" version="1" Unrestricted="true"/>
下面是我写的一个简单的访问数据库的例子,该例子从Goods数据库中的GoodsInformation表中查询有关Goods的信息,
然后把查询到的信息以表格的形式显示在Web部件中:
protected override void RenderWebPart(HtmlTextWriter output)
{
string dataSource = "Data Source = 192.168.0.200;";
string initialCatalog = "Initial Catalog = Goods;";
string userId = "User ID = sa;";
string password = "Password =;";
string connectionStr = dataSource + initialCatalog + userId + password;
SqlConnection goodsConnection = new SqlConnection(connectionStr);
SqlDataAdapter goodsAdp = null;
DataSet goodsDataSet = null;
try
{
goodsConnection.Open();
string sqlSelectStr = "select * from GoodsInformation";
goodsAdp = new SqlDataAdapter(sqlSelectStr,goodsConnection);
goodsDataSet = new DataSet();
goodsAdp.Fill(goodsDataSet);
goodsConnection.Close();
}
catch(Exception ex)
{
output.Write(ex.Message);
return;
}
finally
{
goodsConnection = null;
goodsAdp = null;
}
int rowCount = goodsDataSet.Tables[0].Rows.Count;
int tableHeight = rowCount * 25;
output.Write("<TABLE id=\"NewTable\" style=\"Z-INDEX: 101; LEFT: 56px; WIDTH: 600px; TOP: 136px; HEIGHT: "+ tableHeight + "px\"" +
"cellSpacing=\"1\" cellPadding=\"1\" width=\"550\" border=\"0\">");
for(int rowIndex=0; rowIndex<rowCount; rowIndex++)
{
string name = goodsDataSet.Tables[0].Rows[rowIndex].ItemArray[0].ToString();
string price = goodsDataSet.Tables[0].Rows[rowIndex].ItemArray[1].ToString();
string manufacturer = goodsDataSet.Tables[0].Rows[rowIndex].ItemArray[2].ToString();
System.Web.UI.WebControls.HyperLink hlinkManufacturer = new HyperLink();
hlinkManufacturer.Text = manufacturer;
hlinkManufacturer.NavigateUrl = "http://www.google.com";
hlinkManufacturer.Target = "_blank";
output.Write("<TR bgColor=#dcdcdc>");
output.Write("<TD style=\"WIDTH: 85px\"><FONT face=\"MS UI Gothic\">" + name + "</FONT></TD>");
output.Write("<TD style=\"WIDTH: 100px\"><FONT face=\"MS UI Gothic\">" + price + "</FONT></TD>");
output.Write("<TD><FONT face=\"MS UI Gothic\">");
hlinkManufacturer.RenderControl(output);
output.Write("</FONT></TD>");
output.Write("</TR>");
}
output.Write("</TABLE>");
goodsDataSet = null;
}
浙公网安备 33010602011771号