ASP.NET2.0的Profile服务实例

一个小型WEB 项目,MYComics的漫画网站 。对微软的讲座断断续续看完后,自己动手照着做了下,看来真是万事事必躬亲才知其中滋味啊。
算是摸了点ASP.NET2.0的皮毛
,这里我主要写写Profile服务,即针对用户收藏架那块。

.开发软件:.NET2005SQL2005Express版本。

   SQL2000也能完成这个项目,只不过在最初我使用SQL2000时,打开源码会有末名奇妙BUG,说连不上SQL2005…..我当时还没装SQL2005....
   折腾了半天,把能试的都试遍了就差重装系统了。发现是微软的一个BUG!,网上说是现在要付费才给弄,最后按一个网友说的直接删除一个文件
才算平息。

.MyComics是一个网站书店,整体来讲,具备的业务功能有,算是需求分析吧:

    .  按类别查看不同的漫画书
    .  
对漫画书的列表可以按不同的字段进行排序
    . 
查看漫画书的详细资料,包括图片
   
. 将选中的漫画书添加到收藏架
      .
收藏架中的漫画书下次登陆时仍然存在


.实施方案:

   1.创建数据库,实现商品查看与管理功能。

   2.实现商品查看与管理功能:主要利用GRIDVIEW,DataList,ObjectDataSource,SQLDataSource四个控件完成。

   3.创建网站母版,规划网站风格.

CREATE  DATABASE  MyComics
GO

USE  MyComics
GO

CREATE  TABLE  [ Books ]  (
    
[ ComicID ]  [ int ]  IDENTITY  ( 100 1 NOT  NULL  ,    
    
[ Title ]  [ varchar ]  ( 64 )   NOT  NULL  ,
    
[ Number ]  [ smallint ]  NOT  NULL  ,
    
[ Year ]  [ smallint ]  NOT  NULL  ,
    
[ Grade ]  [ decimal ] ( 3 1 NOT  NULL  ,
    
[ CGC ]  [ bit ]  NOT  NULL  ,
    
[ BookValue ]  [ money ]  NULL  DEFAULT  ( 0 ) ,
    
[ LargeCover ]  [ varchar ]  ( 64 )   NOT  NULL  ,    
    
[ SmallCover ]  [ varchar ]  ( 64 )   NULL  ,
    
[ Comment ]  [ varchar ]  ( 512 )   NULL  ,    
    
CONSTRAINT  [ PK_Books ]  PRIMARY  KEY    CLUSTERED  
    (
        
[ ComicID ]
    )  
ON  [ PRIMARY ]  
ON  [ PRIMARY ]
GO

INSERT  INTO  Books (Title,  Number Year , Grade, CGC, BookValue,LargeCover, SmallCover, Comment)
  
VALUES  ( ' Bugslayer Comics ' 1 2001 9.0 0 45.00 , ' mm_01.jpg ' ' mm_01_small.jpg ' ,
  
' First title in the Bugslayer Comics series. ' )


4. 收藏架(Favorites )的实现,Profile 的应用。
    以往的做法就是在数据库里建表,通过
ADO.NET 的代码,连接数据库,把想收藏的书目的相关信息存进数据库,实现数据存储。
   Profile服务: 它能保存个人信息,跳过数据库,可以持久保存,支持认证用户和匿名用户,在你下次登入后,数据仍然可见。
  
注意点:

   使用
HttpContext. Current.Profile 属性可以在外部组中访问Profile ,就是指在其他类访Profile.
   例: HttpContext .Current.Profile.GetPropertyValue("A")); A 即为Profile的变量名。(下面代码有用到)

当建立Web应用程序时,要花费了大量的精力用于做一些事情,其中的一个就是写一些用于从数据库存储和获得用户信息的代码。
Profile对象引入帮助我们从乏味的编码工作中解脱出来”.不过说实话,在这个例子里,我没发现它的优越的地方,一样要写数据库
连接的代码,只不过不用新建表,有待进一步研究
.

webconfig里的system.web节点声明Profile的属性,见第5行。
FavoritesProfile的属性名称,FavoritesComics是类名,serializeAs是指定串行化方式Binary

 1  < configuration >
 2     < system .web >
 3       < profile >
 4         < properties >
 5           < add  name =" Favorites "  type ="FavoritesComics"  serializeAs ="Binary"  />
 6         </ properties >
 7       </ profile >
 8     </ system.web >
 9  </ configuration >
10 

接下来,新建一个FavoriteComics类:
public class FavoriteComics

{

    public ArrayList IDs=new ArrayList(); //定义一个数组,存储我将收藏的商品ID

    public void AddComic(int id)…       //加进收藏架

    public void RemoveComic(int id)…    //从收藏架移除

    public bool IsInFavoritesList(int id)…   //判断是否在收藏的数组中

    public SqlDataReader GetFavorites()… //连接数据库的

}

详细代码如下:

[Serializable]
public  class  FavoriteComics
{
    
public  ArrayList IDs = new  ArrayList();
    
public  FavoriteComics()
    {
        
    }
    
public  void  AddComic( int  id)
    {
        IDs.Add(id);
    }
    
public  void  RemoveComic( int  id)
    {
        
for  ( int  i  =  0 ; i  <  IDs.Count; i ++
        {
            
int  temp  =  ( int )IDs[i];
            
if  (temp  ==  id)
            {
                IDs.RemoveAt(i) ;
                
break ;
            }
        }        
    }
    
public  bool  IsInFavoritesList( int  id)
    {
       
return   IDs.Contains(id);
    }
    
public  SqlDataReader GetFavorites()
    {
        
string  connString  =  WebConfigurationManager.ConnectionStrings[ " MyComicsConnectionString " ].ConnectionString;
        //获得连接数据库的字符串
        SqlConnection conn 
=  new  SqlConnection();
        conn.ConnectionString 
=  connString;
        
string  where  =  "  1=2   " ;
        
foreach  ( object  o  in  IDs) 
        {
            where 
=  where  +  "  or ComicID= "  +  ( int )o + "  " ;
        }
        
string  sql  =  " SELECT [ComicID], [Title],  [Year] , [Number], [Grade], [BookValue]FROM [Books] where  "  +  where;
        conn.Open();
        SqlCommand cmd 
=  new  SqlCommand(sql, conn);
        
return  cmd.ExecuteReader();
    }
}


Profile的使用:

这里,针对一个LINKBUTTON(Add this comic to my Favorite list):



 1   protected  void  LinkButton1_Click( object  sender, EventArgs e)
 2      {
 3         
 4           int  id  =  Convert.ToInt32(DetailsView1.Rows[ 1 ].Cells[ 1 ].Text); //由于相关模块没有列出来,这里的作用就是获得ComicID
 5 
 6         
 7           if  (LinkButton1.CommandName  ==  " Add " )           // Page_LoadComplete()事件
 8          {
 9               //使用profile
10               if  (Profile.Favorites  ==  null )
11                  Profile.Favorites  =  new  FavoriteComics();
12              Profile.Favorites.AddComic(id);
13          }
14           else
15              Profile.Favorites.RemoveComic(id);
16      }
17 
18       void  Page_LoadComplete( object  sender, EventArgs e)
19      {
20           // 获得ComicID
21           int  id  =  Convert.ToInt32(DetailsView1.Rows[ 1 ].Cells[ 1 ].Text);
22 
23         
24           if  (Profile.Favorites  ==  null  ||  ! Profile.Favorites.IsInFavoritesList(id))
25          {
26              LinkButton1.Text  =  " Add this comic to my Favorites list " ;
27              LinkButton1.CommandName  =  " Add " ;
28          }
29           else
30          {
31              LinkButton1.Text  =  " Remove this comic from my Favorites list " ;
32              LinkButton1.CommandName  =  " Remove " ;
33          }
34      }
35 


最后定义了一个FavoritesDB class,为的是与ObjectSource绑定,让GridView显示收藏的结果。

 1  public  class  FavoritesDB
 2  {
 3       public  FavoritesDB()
 4      {
 5      
 6       
 7       
 8      }
 9       public  SqlDataReader GetFavorites() 
10      {
11           return  ((FavoriteComics)HttpContext.Current.Profile.GetPropertyValue( " Favorites " )).GetFavorites();
12         
13 
14      }
15 
16       public  void  RemoveComic( int  ComicID) 
17      {
18          ((FavoriteComics)HttpContext.Current.Profile.GetPropertyValue( " Favorites " )).RemoveComic(ComicID);
19      }
20  }
21

Gridview显示结果:


以上是整个完整的过程,核心在于Profile的定义,Favorites类的定义.关于网站其他的部分,有空再写写吧。
power by jerrymu 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值