Using LINQ in WSS with VS 2008 Beta 2

原文链接:http://www.cnblogs.com/Glen/archive/2007/08/03/842108.html 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">
Using LINQ in WSS with VS 2008 Beta 2<assemblies>
Using LINQ in WSS with VS 2008 Beta 2        <add assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
Using LINQ in WSS with VS 2008 Beta 2        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
Using LINQ in WSS with VS 2008 Beta 2        <add assembly="System.Data.DataSetExtensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
Using LINQ in WSS with VS 2008 Beta 2        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
Using LINQ in WSS with VS 2008 Beta 2        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
Using LINQ in WSS with VS 2008 Beta 2</assemblies>
</compilation>Using LINQ in WSS with VS 2008 Beta 2

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

 

Using LINQ in WSS with VS 2008 Beta 2<system.codedom>
Using LINQ in WSS with VS 2008 Beta 2      <compilers>
Using LINQ in WSS with VS 2008 Beta 2          <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">
Using LINQ in WSS with VS 2008 Beta 2              <providerOption name="CompilerVersion" value="v3.5"/>
             </compiler>
Using LINQ in WSS with VS 2008 Beta 2          <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" compilerOptions="/optioninfer+" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2              <providerOption name="CompilerVersion" value="v3.5"/>
  Using LINQ in WSS with VS 2008 Beta 2        </compiler>
Using LINQ in WSS with VS 2008 Beta 2      </compilers>
  </system.codedom>

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

 Using LINQ in WSS with VS 2008 Beta 2

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

 

Using LINQ in WSS with VS 2008 Beta 2using System;
Using LINQ in WSS with VS 2008 Beta 2using System.Collections.Generic;
Using LINQ in WSS with VS 2008 Beta 2using System.Linq;
Using LINQ in WSS with VS 2008 Beta 2using System.Text;
Using LINQ in WSS with VS 2008 Beta 2using System.Web.UI;
Using LINQ in WSS with VS 2008 Beta 2using System.Web.UI.WebControls.WebParts;
Using LINQ in WSS with VS 2008 Beta 2using System.Web.UI.WebControls;
Using LINQ in WSS with VS 2008 Beta 2using System.Xml.Serialization;
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2using Microsoft.SharePoint;
Using LINQ in WSS with VS 2008 Beta 2using Microsoft.SharePoint.WebControls;
Using LINQ in WSS with VS 2008 Beta 2using Microsoft.SharePoint.WebPartPages;
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2namespace GlenCode
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2{
Using LINQ in WSS with VS 2008 Beta 2    public class HelloPart35:System.Web.UI.WebControls.WebParts.WebPart
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2    Using LINQ in WSS with VS 2008 Beta 2{
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2        Member Variables#region Member Variables
Using LINQ in WSS with VS 2008 Beta 2        protected GridView _gvProducts = null;
Using LINQ in WSS with VS 2008 Beta 2        protected Literal _litMsg = null;
Using LINQ in WSS with VS 2008 Beta 2        #endregion
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2        Overrides#region Overrides
Using LINQ in WSS with VS 2008 Beta 2        protected override void Render(HtmlTextWriter writer)
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2        Using LINQ in WSS with VS 2008 Beta 2{
Using LINQ in WSS with VS 2008 Beta 2            this._litMsg.RenderControl(writer);
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.RenderControl(writer);           
Using LINQ in WSS with VS 2008 Beta 2        }
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2        protected override void CreateChildControls()
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2        Using LINQ in WSS with VS 2008 Beta 2{
Using LINQ in WSS with VS 2008 Beta 2            //Message 
Using LINQ in WSS with VS 2008 Beta 2            _litMsg = new Literal();
Using LINQ in WSS with VS 2008 Beta 2            _litMsg.Text = ""; //No Message yet
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2            // GridView
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts = new GridView();
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.AutoGenerateColumns = false;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.BorderWidth = 0;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.ShowHeader = true;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.ShowFooter = false;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.HorizontalAlign = HorizontalAlign.Left;
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.DataKeyNames = new string[] Using LINQ in WSS with VS 2008 Beta 2{ "ProductID" };
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.Load += new EventHandler(gvProducts_Load);
Using LINQ in WSS with VS 2008 Beta 2            
Using LINQ in WSS with VS 2008 Beta 2            //this._gvProducts.PageIndexChanging += new GridViewPageEventHandler(gvProducts_PageIndexChanging);
Using LINQ in WSS with VS 2008 Beta 2            //this._gvProducts.Sorting += new GridViewSortEventHandler(gvProducts_Sorting);
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.AlternatingRowStyle.BackColor = System.Drawing.Color.LemonChiffon;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.HeaderStyle.BackColor = System.Drawing.Color.Silver;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.AllowSorting = true;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.AllowPaging = true;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.PageSize = 10;
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2            BoundField bfProd = new BoundField();
Using LINQ in WSS with VS 2008 Beta 2            bfProd.DataField = "ProductName";
Using LINQ in WSS with VS 2008 Beta 2            bfProd.HeaderText = "Product";
Using LINQ in WSS with VS 2008 Beta 2            bfProd.SortExpression = "ProductName";
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.Columns.Add(bfProd);
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2            BoundField bfCat = new BoundField();
Using LINQ in WSS with VS 2008 Beta 2            bfCat.DataField = "CategoryName";
Using LINQ in WSS with VS 2008 Beta 2            bfCat.HeaderText = "Category";
Using LINQ in WSS with VS 2008 Beta 2            bfCat.ReadOnly = true;
Using LINQ in WSS with VS 2008 Beta 2            bfCat.SortExpression = "CategoryName";
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.Columns.Add(bfCat);
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2            BoundField bfSupplier = new BoundField();
Using LINQ in WSS with VS 2008 Beta 2            bfSupplier.DataField = "SupplierName";
Using LINQ in WSS with VS 2008 Beta 2            bfSupplier.HeaderText = "Supplier";
Using LINQ in WSS with VS 2008 Beta 2            bfSupplier.SortExpression = "SupplierName";
Using LINQ in WSS with VS 2008 Beta 2            bfSupplier.ReadOnly = true;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.Columns.Add(bfSupplier);
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2            BoundField bfUnitPrice = new BoundField();
Using LINQ in WSS with VS 2008 Beta 2            bfUnitPrice.DataField = "UnitPrice";
Using LINQ in WSS with VS 2008 Beta 2            bfUnitPrice.HeaderText = "Price";
Using LINQ in WSS with VS 2008 Beta 2            bfUnitPrice.SortExpression = "UnitPrice";
Using LINQ in WSS with VS 2008 Beta 2            bfUnitPrice.HtmlEncode = false;
Using LINQ in WSS with VS 2008 Beta 2            bfUnitPrice.DataFormatString = "{0:c}";
Using LINQ in WSS with VS 2008 Beta 2            bfUnitPrice.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.Columns.Add(bfUnitPrice);
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2            CheckBoxField cbfDiscon = new CheckBoxField();
Using LINQ in WSS with VS 2008 Beta 2            cbfDiscon.DataField = "Discontinued";
Using LINQ in WSS with VS 2008 Beta 2            cbfDiscon.HeaderText = "Discontinued";
Using LINQ in WSS with VS 2008 Beta 2            cbfDiscon.SortExpression = "Discontinued";
Using LINQ in WSS with VS 2008 Beta 2            cbfDiscon.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.Columns.Add(cbfDiscon);
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2            this.Controls.Clear();
Using LINQ in WSS with VS 2008 Beta 2            this.Controls.Add(this._gvProducts);            
Using LINQ in WSS with VS 2008 Beta 2            this.Controls.Add(this._litMsg);
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2        }
Using LINQ in WSS with VS 2008 Beta 2        #endregion
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2        Delegates#region Delegates
Using LINQ in WSS with VS 2008 Beta 2        protected void gvProducts_Load(object sender, EventArgs e)
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2        Using LINQ in WSS with VS 2008 Beta 2{
Using LINQ in WSS with VS 2008 Beta 2            //Detect PostBack
Using LINQ in WSS with VS 2008 Beta 2            if (this._gvProducts.Rows.Count == 0)
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2            Using LINQ in WSS with VS 2008 Beta 2{
Using LINQ in WSS with VS 2008 Beta 2                BindGvProducts();
Using LINQ in WSS with VS 2008 Beta 2            }
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2        }
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2        #endregion
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2        Helper Methods#region Helper Methods
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2        /**//*
Using LINQ in WSS with VS 2008 Beta 2        void FillDataGrid()
Using LINQ in WSS with VS 2008 Beta 2        {
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.DataSource = this._objDataSrcProducts;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.DataBind();
Using LINQ in WSS with VS 2008 Beta 2        }*/
Using LINQ in WSS with VS 2008 Beta 2        
Using LINQ in WSS with VS 2008 Beta 2        private void BindGvProducts()
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2        Using LINQ in WSS with VS 2008 Beta 2{
Using LINQ in WSS with VS 2008 Beta 2            NorthwindContextDataContext ndc = new NorthwindContextDataContext();
Using LINQ in WSS with VS 2008 Beta 2
Using LINQ in WSS with VS 2008 Beta 2            var data = from p in ndc.Products
Using LINQ in WSS with VS 2008 Beta 2                       select new
Using LINQ in WSS with VS 2008 Beta 2Using LINQ in WSS with VS 2008 Beta 2                       Using LINQ in WSS with VS 2008 Beta 2{
Using LINQ in WSS with VS 2008 Beta 2                           ProductID = p.ProductID,
Using LINQ in WSS with VS 2008 Beta 2                           ProductName = p.ProductName,
Using LINQ in WSS with VS 2008 Beta 2                           CategoryName = p.Category.CategoryName,
Using LINQ in WSS with VS 2008 Beta 2                           SupplierName = p.Supplier.CompanyName,
Using LINQ in WSS with VS 2008 Beta 2                           UnitPrice = p.UnitPrice,
Using LINQ in WSS with VS 2008 Beta 2                           Discontinued = p.Discontinued
Using LINQ in WSS with VS 2008 Beta 2                       };
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.DataSource = data;
Using LINQ in WSS with VS 2008 Beta 2            this._gvProducts.DataBind();
Using LINQ in WSS with VS 2008 Beta 2        }
Using LINQ in WSS with VS 2008 Beta 2        #endregion
Using LINQ in WSS with VS 2008 Beta 2    }
Using LINQ in WSS with VS 2008 Beta 2}
Using LINQ in WSS with VS 2008 Beta 2

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

上一篇:浅谈WebSocket协议、WS协议和WSS协议原理及关系


下一篇:【C语言入门教程】7.2 结构体数组的定义和引用