Webpart中用DataGrid绑定SQL数据? [downmoon原作]

自写了一个WebPart 用DataRead列出数据,准备
部署在SharePoint服务器上,想想很容易,一路顺利,可是将导入的WebPart拽到页面时,出来这个错误:

An Error has occurred: Request for the permission of type System.Data.SqlClient.SqlClientPermission, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.

看来WebPart访问SQL有问题:
于是。在WSS的webConfig文件中添加下列两行:

None.gif < SafeControl Assembly = " System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 "  Namespace = " System.Data.SqlClient "  TypeName = " * "  Safe = " True "   />
None.gif   
< SafeControl Assembly = " System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 "  Namespace = " System.Data "  TypeName = " * "  Safe = " True "   />

 

还是不行,
又在C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\config\wss_minimaltrust.config文件中添加下面一行:

 

None.gif    < SecurityClass Name = " SqlClientPermission "  Description = " System.Data.SqlClient.SqlClientPermission, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 " />

 

服务器 iisreset后 还是不行!
这可怪了! 再到服务器上一看,原来数据库采用Windows身份验证模式,而不是混合验证模式

于是,再次将服务器上的ASPNET帐号户设置为所有数据库访问权

还是不行
怀疑是.net运行权限不够
再次在服务器中"信任程序集",将服务器上的System.dll的程序集权限设主完全信任!
还是不行!我晕

再捍源码中连接串如下:
"Data Source=sp-hgh;Initial Catalog=NorthWind;uid-sa;pwd=;"
马上改为:
"Data Source=sp-hgh;Initial Catalog=NorthWind;Integrated Security=SSPI;Trusted_Connection=yes;"

结果还是出不来数据
唉!整个一郁闷

最后捍了XGP的文章《[url=http://xgp1226.blogdriver.com/xgp1226/598244.html]Web部件访问SQL数据库--寻找收获的快乐[/url]》一文,深受启发,又参考MSDN文档,终于成功!
方法:

  然后在wss_minimaltrust.config文件(在Webconfig文件中查找文件的位置)的<SecurityClass>节点中加入子节点:

None.gif < SecurityClass Name = " SqlClientPermission "  Description = " System.Data.SqlClient.SqlClientPermission, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 " />
None.gif 
None.gif 
< SecurityClass Name = " SharePointPermission "  Description = " Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c " />


     
       再在ASP.net(注意必须)的<PermissionSet>节点中加入子节点:

 

None.gif                               < IPermission
None.gif                                    
class = " SqlClientPermission "
None.gif                                    version
= " 1 "
None.gif                                    Unrestricted
= " true "
None.gif                            
/>
None.gif                             
< IPermission  class = " SharePointPermission "
None.gif                                    version
= " 1 "
None.gif                                    ObjectModel
= " True "
None.gif                            
/>

 

再重启IIS
iisreset 后, 一切OK!

我的源码如下:

None.gif using  System;
None.gif
using  System.ComponentModel;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Xml.Serialization;
None.gif
using  Microsoft.SharePoint;
None.gif
using  Microsoft.SharePoint.Utilities;
None.gif
using  Microsoft.SharePoint.WebPartPages;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
namespace  NewDispData
ExpandedBlockStart.gifContractedBlock.gif
... {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// Description for WebPart1.
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif [DefaultProperty("Text"),
InBlock.gif  ToolboxData(
"<{0}:WebPart1 runat=server></{0}:WebPart1>"),
InBlock.gif  XmlRoot(Namespace
="NewDispData")]
InBlock.gif 
public class NewDispData : Microsoft.SharePoint.WebPartPages.WebPart//,ICellConsumer//,IRowProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif
 ...{
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif  
webPart变量#region webPart变量
InBlock.gif  
private const string defaultText = "";
InBlock.gif  
private string text = defaultText;
InBlock.gif  [Browsable(
true),
InBlock.gif  Category(
"Miscellaneous"),
InBlock.gif  DefaultValue(defaultText),
InBlock.gif  WebPartStorage(Storage.Personal),
InBlock.gif  FriendlyName(
"Text"),
InBlock.gif  Description(
"Text Property")]
InBlock.gif  
public string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif  
...{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
...{
InBlock.gif    
return text;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
set
ExpandedSubBlockStart.gifContractedSubBlock.gif   
...{
InBlock.gif    text 
= value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif 
const string connectionStr = "server=downmoon-hgh;database=NorthWind;uid=sa;pwd=sa;";//
InBlock.gif
  protected Label ErrorLabel;
InBlock.gif  
protected DataGrid OrdersGrid; 
InBlock.gif
InBlock.gif  SqlConnection NorthWindConnection 
= new SqlConnection(connectionStr);
InBlock.gif  SqlDataAdapter NorthWindAdp 
= null;
InBlock.gif  DataSet NorthWindDataSet 
= null;
InBlock.gif  
private int _connectionCount = 1;
ExpandedSubBlockEnd.gif  
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif     
创建子控件#region 创建子控件
InBlock.gif  
protected override void CreateChildControls()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
...{
InBlock.gif   
// This label is displayed only if
InBlock.gif   
// an exception is thrown while attempting
InBlock.gif   
// to load the data.
InBlock.gif
   ErrorLabel = new Label();
InBlock.gif   ErrorLabel.Visible 
= false;
InBlock.gif   Controls.Add(ErrorLabel);
InBlock.gif
InBlock.gif   
// Create the data grid
InBlock.gif
   OrdersGrid = new DataGrid();
InBlock.gif   BoundColumn newColumn;
InBlock.gif
InBlock.gif   OrdersGrid.Load 
+= new EventHandler(OrdersLoad);
InBlock.gif   OrdersGrid.AllowPaging 
= false;
InBlock.gif   OrdersGrid.HeaderStyle.Font.Bold 
= true;
InBlock.gif   OrdersGrid.HeaderStyle.Wrap 
= false;
InBlock.gif   OrdersGrid.GridLines 
= System.Web.UI.WebControls.GridLines.Both;
InBlock.gif   OrdersGrid.AutoGenerateColumns 
= false;
InBlock.gif
InBlock.gif   
// Define grid columns
InBlock.gif
   newColumn = new BoundColumn();
InBlock.gif   newColumn.DataField 
= "OrderID";
InBlock.gif   newColumn.HeaderText 
= "Order ID";
InBlock.gif   newColumn.ItemStyle.HorizontalAlign 
= HorizontalAlign.Right;
InBlock.gif   OrdersGrid.Columns.Add(newColumn);
InBlock.gif
InBlock.gif   newColumn 
= new BoundColumn();
InBlock.gif   newColumn.DataField 
= "CustomerID";
InBlock.gif   newColumn.HeaderText 
= "Customer ID";
InBlock.gif   OrdersGrid.Columns.Add(newColumn);
InBlock.gif
InBlock.gif   newColumn 
= new BoundColumn();
InBlock.gif
InBlock.gif   newColumn.DataField 
= "OrderDate";
InBlock.gif   newColumn.HeaderText 
= "Order Date";
InBlock.gif   newColumn.DataFormatString 
= "{0:d}";
InBlock.gif   newColumn.ItemStyle.HorizontalAlign 
= HorizontalAlign.Right;
InBlock.gif   OrdersGrid.Columns.Add(newColumn);
InBlock.gif
InBlock.gif   newColumn 
= new BoundColumn();
InBlock.gif   newColumn.DataField 
= "RequiredDate";
InBlock.gif   newColumn.HeaderText 
= "Required Date";
InBlock.gif   newColumn.DataFormatString 
= "{0:d}";
InBlock.gif   newColumn.ItemStyle.HorizontalAlign 
= HorizontalAlign.Right;
InBlock.gif   OrdersGrid.Columns.Add(newColumn);
InBlock.gif
InBlock.gif   newColumn 
= new BoundColumn();
InBlock.gif   newColumn.DataField 
= "ShippedDate";
InBlock.gif   newColumn.HeaderText 
= "Shipped Date";
InBlock.gif   newColumn.DataFormatString 
= "{0:d}";
InBlock.gif   newColumn.ItemStyle.HorizontalAlign 
= HorizontalAlign.Right;
InBlock.gif   OrdersGrid.Columns.Add(newColumn);
InBlock.gif   
InBlock.gif   newColumn 
= new BoundColumn();
InBlock.gif   newColumn.DataField 
= "Freight";
InBlock.gif   newColumn.HeaderText 
= "Freight Cost";
InBlock.gif   newColumn.DataFormatString 
= "{0:c}";
InBlock.gif   newColumn.ItemStyle.HorizontalAlign 
= HorizontalAlign.Right;
InBlock.gif   OrdersGrid.Columns.Add(newColumn);
InBlock.gif
InBlock.gif   Controls.Add(OrdersGrid);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**////<summary>
InBlock.gif  
/// OrdersLoad
InBlock.gif  
/// handles OrdersGrid.Load.
InBlock.gif  
///</summary>
ExpandedSubBlockEnd.gif  
///

InBlock.gif  public void OrdersLoad(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
...{
InBlock.gif  BindGrid();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
private void BindGrid()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
...{
InBlock.gif   
// Load schema and data into a DataSet.
InBlock.gif
   DataSet ordersSet = new DataSet();
InBlock.gif
InBlock.gif   
// Assume XML data file is in the wpresources folder.
InBlock.gif   
// If File I/O permissions are not available,
InBlock.gif   
// the following will throw a security exception.
InBlock.gif
    try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
...{
InBlock.gif     NorthWindConnection.Open();
InBlock.gif     
string sqlSelectStr = "select * from Orders";
InBlock.gif     NorthWindAdp 
= new SqlDataAdapter(sqlSelectStr,NorthWindConnection);
InBlock.gif     NorthWindDataSet 
= new DataSet();
InBlock.gif     NorthWindAdp.Fill(NorthWindDataSet,
"Orders");
InBlock.gif     NorthWindConnection.Close();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
...{
InBlock.gif     ErrorLabel.Text 
=
InBlock.gif      ex.Message 
+ "<br>" +
InBlock.gif      
"Steps to correct this are included in" +
InBlock.gif      
" the documentation for this sample.";
InBlock.gif     ErrorLabel.Visible 
= true;
InBlock.gif     
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif    
...{
InBlock.gif     NorthWindConnection 
= null;       
InBlock.gif     NorthWindAdp 
= null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif   
// No error if we made it this far.
InBlock.gif
   ErrorLabel.Visible = false;
InBlock.gif
InBlock.gif   
// Use a DataView to filter orders.
InBlock.gif
   
InBlock.gif   
string rowFilter="";
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**/////   if(CustomerId!="")
InBlock.gif
////   {
InBlock.gif
////    rowFilter = "CustomerID = '" + CustomerId + "'";
InBlock.gif
////   }
InBlock.gif
////   if(CustomerId!="")
InBlock.gif
////   {
InBlock.gif
////    rowFilter = "CustomerID = '" + "CHOPS" + "'";
ExpandedSubBlockEnd.gif
////   }

InBlock.gif
InBlock.gif   DataView ordersView 
= new DataView(NorthWindDataSet.Tables["Orders"]);
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**/////   ordersView.RowFilter = rowFilter;
InBlock.gif
InBlock.gif   OrdersGrid.Enabled 
= true;
InBlock.gif   OrdersGrid.DataSource 
= ordersView;
InBlock.gif   OrdersGrid.DataBind();
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockEnd.gif  
#endregion

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// Render this Web Part to the output parameter specified.
InBlock.gif  
/// </summary>
ExpandedSubBlockEnd.gif  
/// <param name="output"> The HTML writer to write out to </param>

InBlock.gif  protected override void RenderWebPart(HtmlTextWriter output)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
...{
InBlock.gif   
//output.Write(SPEncode.HtmlEncode(Text));
InBlock.gif
    EnsureChildControls();
InBlock.gif   
if (ErrorLabel.Visible == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
...{
InBlock.gif    ErrorLabel.RenderControl(output);
InBlock.gif    
return;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
//If connected then display a heading and the grid.
InBlock.gif
   if (_connectionCount > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
...{   
InBlock.gif    output.RenderBeginTag(
"div");
InBlock.gif    output.Write(
"<br>");
InBlock.gif    output.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, 
"bold");
InBlock.gif    output.RenderBeginTag(HtmlTextWriterTag.Span);
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**/////    output.Write("<nobr> Orders for Customer ID: "
ExpandedSubBlockEnd.gif
////     + System.Web.HttpUtility.HtmlEncode(CustomerId) +  "</nobr>");

InBlock.gif    output.RenderEndTag(); //span
InBlock.gif
    output.Write("<br>");
InBlock.gif    output.Write(
"<br>");
InBlock.gif    OrdersGrid.RenderControl(output);
InBlock.gif    output.RenderEndTag(); 
//div
ExpandedSubBlockEnd.gif
   }

InBlock.gif   
if(_connectionCount > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
...{
InBlock.gif    output.RenderBeginTag(
"div");
InBlock.gif    output.Write(
"<br>");
InBlock.gif    output.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, 
"bold");
InBlock.gif    output.AddStyleAttribute(HtmlTextWriterStyle.Color, 
"#000000");
InBlock.gif    output.RenderBeginTag(HtmlTextWriterTag.Span);
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**/////    output.Write("<nobr> Orders for Customer ID: "
InBlock.gif
////     + System.Web.HttpUtility.HtmlEncode(CustomerId));
ExpandedSubBlockEnd.gif
////    output.Write(" </nobr>");

InBlock.gif    output.RenderEndTag(); //span
InBlock.gif
    BindGrid();
InBlock.gif    OrdersGrid.RenderControl(output);
InBlock.gif    output.RenderEndTag(); 
//div
ExpandedSubBlockEnd.gif
   }

InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

 

 

 

转载于:https://www.cnblogs.com/downmoon/archive/2007/12/27/downmoon-webpart.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值