Using LINQ in WSS with VS 2008 Beta 2

In this article, I do a test in WSS about using linq to read database.

What you need to do my test:
1.  VS 2008 Beta2
2. Windows Server 2003 with WSS 3.0 installed
3. Sql Server 2005 or Sql Server 2005 express with Northwind database intalled.

Now, let's begin. 

1. Configure the Web.config

First, you have to install .Net Framework 3.5. Then, you have to configure the web.config of your WSS web site. Just reference the file auto-generated by VS2008 Beta 2.

 Pay attention to that: the trust level must be changed to Full.

<trust level="Full"/> 

In the compilation elements, add following assemblies.

 

 

< compilation  debug ="false" >
None.gif
< assemblies >
None.gif        
< add  assembly ="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"   />
None.gif        
< add  assembly ="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
None.gif        
< add  assembly ="System.Data.DataSetExtensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
None.gif        
< add  assembly ="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
None.gif        
< add  assembly ="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
None.gif
</ assemblies >
</ compilation > None.gif

 Out of the <system.web> elements, add these:

 

None.gif < system .codedom >
None.gif      
< compilers >
None.gif         
< compiler  language ="c#;cs;csharp"  extension =".cs"  compilerOptions ="/warnaserror-"  warningLevel ="4"  type ="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
None.gif             
< providerOption  name ="CompilerVersion"  value ="v3.5" />
            
</ compiler >
None.gif         
< compiler  language ="vb;vbs;visualbasic;vbscript"  extension =".vb"  compilerOptions ="/optioninfer+"  type ="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > None.gif
None.gif              
< providerOption  name ="CompilerVersion"  value ="v3.5" />
  None.gif        
</ compiler >
None.gif      
</ compilers >
  
</ system.codedom >

 2. Creating a C# class library project. In the project setting page, signing this project.

 

3. Create a LINQ Northwind Context, reference the Scott Gu' blog about how to to it.

http://weblogs.asp.net/scottgu/archive/2007/05/29/linq-to-sql-part-2-defining-our-data-model-classes.aspx

4.Create a class which inherit from System.Web.UI.WebControls.WebParts.WebPart

 

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Linq;
None.gif
using  System.Text;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Xml.Serialization;
None.gif
None.gif
using  Microsoft.SharePoint;
None.gif
using  Microsoft.SharePoint.WebControls;
None.gif
using  Microsoft.SharePoint.WebPartPages;
None.gif
None.gif
namespace  GlenCode
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class HelloPart35:System.Web.UI.WebControls.WebParts.WebPart
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Member Variables#region Member Variables
InBlock.gif        
protected GridView _gvProducts = null;
InBlock.gif        
protected Literal _litMsg = null;
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Overrides#region Overrides
InBlock.gif        
protected override void Render(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._litMsg.RenderControl(writer);
InBlock.gif            
this._gvProducts.RenderControl(writer);           
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void CreateChildControls()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Message 
InBlock.gif
            _litMsg = new Literal();
InBlock.gif            _litMsg.Text 
= ""//No Message yet
InBlock.gif
InBlock.gif            
// GridView
InBlock.gif
            this._gvProducts = new GridView();
InBlock.gif            
this._gvProducts.AutoGenerateColumns = false;
InBlock.gif            
this._gvProducts.BorderWidth = 0;
InBlock.gif            
this._gvProducts.ShowHeader = true;
InBlock.gif            
this._gvProducts.ShowFooter = false;
InBlock.gif            
this._gvProducts.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
InBlock.gif            
this._gvProducts.HorizontalAlign = HorizontalAlign.Left;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this._gvProducts.DataKeyNames = new string[] dot.gif"ProductID" };
InBlock.gif            
this._gvProducts.Load += new EventHandler(gvProducts_Load);
InBlock.gif            
InBlock.gif            
//this._gvProducts.PageIndexChanging += new GridViewPageEventHandler(gvProducts_PageIndexChanging);
InBlock.gif            
//this._gvProducts.Sorting += new GridViewSortEventHandler(gvProducts_Sorting);
InBlock.gif
            this._gvProducts.AlternatingRowStyle.BackColor = System.Drawing.Color.LemonChiffon;
InBlock.gif            
this._gvProducts.HeaderStyle.BackColor = System.Drawing.Color.Silver;
InBlock.gif            
this._gvProducts.AllowSorting = true;
InBlock.gif            
this._gvProducts.AllowPaging = true;
InBlock.gif            
this._gvProducts.PageSize = 10;
InBlock.gif
InBlock.gif            BoundField bfProd 
= new BoundField();
InBlock.gif            bfProd.DataField 
= "ProductName";
InBlock.gif            bfProd.HeaderText 
= "Product";
InBlock.gif            bfProd.SortExpression 
= "ProductName";
InBlock.gif            
this._gvProducts.Columns.Add(bfProd);
InBlock.gif
InBlock.gif            BoundField bfCat 
= new BoundField();
InBlock.gif            bfCat.DataField 
= "CategoryName";
InBlock.gif            bfCat.HeaderText 
= "Category";
InBlock.gif            bfCat.ReadOnly 
= true;
InBlock.gif            bfCat.SortExpression 
= "CategoryName";
InBlock.gif            
this._gvProducts.Columns.Add(bfCat);
InBlock.gif
InBlock.gif            BoundField bfSupplier 
= new BoundField();
InBlock.gif            bfSupplier.DataField 
= "SupplierName";
InBlock.gif            bfSupplier.HeaderText 
= "Supplier";
InBlock.gif            bfSupplier.SortExpression 
= "SupplierName";
InBlock.gif            bfSupplier.ReadOnly 
= true;
InBlock.gif            
this._gvProducts.Columns.Add(bfSupplier);
InBlock.gif
InBlock.gif            BoundField bfUnitPrice 
= new BoundField();
InBlock.gif            bfUnitPrice.DataField 
= "UnitPrice";
InBlock.gif            bfUnitPrice.HeaderText 
= "Price";
InBlock.gif            bfUnitPrice.SortExpression 
= "UnitPrice";
InBlock.gif            bfUnitPrice.HtmlEncode 
= false;
InBlock.gif            bfUnitPrice.DataFormatString 
= "{0:c}";
InBlock.gif            bfUnitPrice.ItemStyle.HorizontalAlign 
= HorizontalAlign.Right;
InBlock.gif            
this._gvProducts.Columns.Add(bfUnitPrice);
InBlock.gif
InBlock.gif            CheckBoxField cbfDiscon 
= new CheckBoxField();
InBlock.gif            cbfDiscon.DataField 
= "Discontinued";
InBlock.gif            cbfDiscon.HeaderText 
= "Discontinued";
InBlock.gif            cbfDiscon.SortExpression 
= "Discontinued";
InBlock.gif            cbfDiscon.ItemStyle.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif            
this._gvProducts.Columns.Add(cbfDiscon);
InBlock.gif
InBlock.gif            
this.Controls.Clear();
InBlock.gif            
this.Controls.Add(this._gvProducts);            
InBlock.gif            
this.Controls.Add(this._litMsg);
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Delegates#region Delegates
InBlock.gif        
protected void gvProducts_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Detect PostBack
InBlock.gif
            if (this._gvProducts.Rows.Count == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                BindGvProducts();
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Helper Methods#region Helper Methods
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif        void FillDataGrid()
InBlock.gif        {
InBlock.gif            this._gvProducts.DataSource = this._objDataSrcProducts;
InBlock.gif            this._gvProducts.DataBind();
ExpandedSubBlockEnd.gif        }
*/

InBlock.gif        
InBlock.gif        
private void BindGvProducts()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            NorthwindContextDataContext ndc 
= new NorthwindContextDataContext();
InBlock.gif
InBlock.gif            var data 
= from p in ndc.Products
InBlock.gif                       select 
new
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif                           ProductID 
= p.ProductID,
InBlock.gif                           ProductName 
= p.ProductName,
InBlock.gif                           CategoryName 
= p.Category.CategoryName,
InBlock.gif                           SupplierName 
= p.Supplier.CompanyName,
InBlock.gif                           UnitPrice 
= p.UnitPrice,
InBlock.gif                           Discontinued 
= p.Discontinued
ExpandedSubBlockEnd.gif                       }
;
InBlock.gif            
this._gvProducts.DataSource = data;
InBlock.gif            
this._gvProducts.DataBind();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

5. Build the Project. Put the dll into the bin of the WSS extended Web.

6.In the webpart gallary of the site collection, populate the webpart.

7.Add the webpart to one of your web page.

转载于:https://www.cnblogs.com/Glen/archive/2007/08/03/842108.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值