《Asp.net2.0开发指南》-ObjectDataSource 典型应用2 绑定业务逻辑层的实现

去年在长沙,电脑送人了,在家闲着没事买了这本书《Asp.net2.0开发指南》对我这个菜鸟来说,这本书还可以,介绍了一些asp.net2.0中的新知识。个人感觉C#2.0基础部分太没必要了,有点浪费了我的MONEY。
对于业务实体这块比较感兴趣,所以仔细看了ObjectDataSource部分。尽管如此,我是即没看的很明白也没做的很明白。
定义实体类Author :没有啥问题

using System;
using System.Collections.Generic;
using System.Text;

namespace LogicLibrary
{
    
public class Author
    
{
        
private string _id;
        
private string _firstname;
        
private string _lastname;
        
private string _state;
        
        
public Author()
        
{ }
        
public Author(string id,string firstname,string lastname,string state)
        
{
            
this.ID = id;
            
this.FirstName = firstname;
            
this.LastName = lastname;
            
this.State = state;
        }

        
public string ID
        
{
            
get
            
{
                
return _id;
            }

            
set
            
{
                _id 
= value;
            }

        }

        
public string FirstName
        
{
            
get
            
{
                
return _firstname;
            }

            
set
            
{
                _firstname 
= value;
            }

        }

        
public string LastName
        
{
            
get
            
{
                
return _lastname;
            }

            
set
            
{
                _lastname 
= value;
            }

        }

        
public string State
        
{
            
get
            
{
                
return _state;
            }

            
set
            
{
                _state 
= value;
            }

        }


    }

}


定义业务逻辑组件
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace LogicLibrary
{
    
public class AuthorsComponent
    
{
        
public AuthorsComponent()
        
{ }
        
//
        public List<Author> GetAuthorsByState(string state, string sortExpression)
        
{
            List
<Author> authors = new List<Author>();
            DataTable dt 
= AuthorDB.AuthorDB.GetAuthorsByState(state);
            
foreach (DataRow row in dt.Rows)
            
{
                authors.Add(
new Author((string)row["id"],(string)row["lastname"],(string)row["firstname"],(string)row["state"]));
            }

            authors.Sort(
new AuthorComparer(sortExpression));
            
return authors;
        }

        
//
        public int UpdateAuthor(Author a)
        
{
            
return AuthorDB.AuthorDB.UpdateAuthor(a.ID,a.LastName,a.FirstName,a.State);
        }

        
public int UpdateAuthor(string ID,string LastName,string FirstName,string State)
        
{
            
return AuthorDB.AuthorDB.UpdateAuthor(ID,LastName,FirstName,State);
        }

        
//
        public List<string> GetStates()
        
{
            List
<string> states = new List<string>();
            DataSet ds 
= AuthorDB.AuthorDB.GetStates();
            
foreach (DataRow row in ds.Tables[0].Rows)
                states.Add((
string)row["state"]);
            
return states;
        }

    }

    
//自定义排序 实现IComparer接口
    public class AuthorComparer : IComparer<Author>
    
{
        
private string _sortColumn;
        
private bool _reverse;
        
//构造函数
        public AuthorComparer(string sortExpression)
        
{
            _reverse 
= sortExpression.ToLowerInvariant().EndsWith(" desc");
            
if (_reverse)
            
{
                _sortColumn 
= sortExpression.Substring(0, sortExpression.Length - 5);
            }

            
else
            
{
                _sortColumn 
= sortExpression;
            }

        }

        
//实现接口的Compare方法 比较两个Author对象实例
        public int Compare(Author a,Author b)
        
{
            
int retVal = 0;
            
switch (_sortColumn)
            
{
                
case "ID":
                    retVal 
= String.Compare(a.ID, b.ID, StringComparison.InvariantCultureIgnoreCase);
                    
break;
                
case "FirstName":
                    retVal 
= String.Compare(a.FirstName, b.FirstName, StringComparison.InvariantCultureIgnoreCase);
                    
break;
                
case "LastName":
                    retVal 
= String.Compare(a.LastName, b.LastName, StringComparison.InvariantCultureIgnoreCase);
                    
break;
                
case "State":
                    retVal 
= String.Compare(a.State, b.State, StringComparison.InvariantCultureIgnoreCase);
                    
break;
            }

            
return (retVal*(_reverse?-1:1));
        }

    }

    

}

定义数据访问组件:这里数据来源于xml文件
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;

namespace AuthorDB
{
    
public class AuthorDB
    
{
        
//string str = System.Configuration.ConfigurationSettings.AppSettings["xmlFile"].ToString().Trim();
        public AuthorDB()
        
{ }
        
//
        public static DataSet GetStates()
        
{
            
string str = @"C:\Documents and Settings\lzfan\My Documents\Visual Studio 2005\WebSites\ObjectDataSourceExample\App_Data\Author.xml";
            DataSet ds 
= new DataSet();
            ds.ReadXml(str,XmlReadMode.ReadSchema);
            
return ds;
        }

        
public static DataTable GetAuthorsByState(string state)
        
{
            
string str = @"C:\Documents and Settings\lzfan\My Documents\Visual Studio 2005\WebSites\ObjectDataSourceExample\App_Data\Author.xml";
            DataSet ds 
= new DataSet();
            ds.ReadXml(str,XmlReadMode.ReadSchema);
            
//DataRow[] drs = ds.Tables[0].Select("state="+state.ToString().Trim());
            DataView dv = new DataView(ds.Tables[0],state,null,DataViewRowState.CurrentRows);
            
return dv.Table;

        }

        
public static int UpdateAuthor(string au_id,string au_lname,string au_fname,string state)
        
{
            
int i = 0;
            
return i;
            
//
        }

    }

}
页面文件Default.aspx
<body>
    
<form id="form1" runat="server">
        
<div>
            
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="obj1" AutoPostBack="True"  >    
            
</asp:DropDownList>
            
<asp:objectdatasource id="obj1" runat="server" typename="LogicLibrary.AuthorsComponent"
                selectmethod
="GetStates"></asp:objectdatasource>
            
<asp:gridview id="gridview1" runat="server" datasourceid="objectdatasource2" allowpaging="true"
                allowsorting
="true" autogeneratecolumns="false" enabletheming="true">
                
<columns>
                    
<asp:commandfield showeditbutton="true" canceltext="cancle" />
                    
<asp:boundfield datafield="id" headertext="id" sortexpression="id" />
                    
<asp:boundfield datafield="firstname" headertext="firstname" sortexpression="firstname" />
                    
<asp:boundfield datafield="lasename" headertext="lasename" sortexpression="lasename" />
                    
<asp:boundfield datafield="state" headertext="state" sortexpression="state" />
                
</columns>
            
</asp:gridview>
        
        
<asp:objectdatasource id="objectdatasource2" runat="server" typename="LogicLibrary.AuthorsComponent"
            dataobjecttypename
="LogicLibrary.Author" selectmethod="GetAuthorsByState" updatemethod="UpdateAuthor"
            sortparametername
="sortExpression">
            
<selectparameters>
                
<asp:controlparameter  controlid="DropDownList1" name="state" PropertyName="SelectedValue"/>
            
</selectparameters>
        
</asp:objectdatasource>
       
</div>
    
</form>
</body>
XML数据源文件
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:od="urn:schemas-microsoft-com:officedata">
  
<xsd:schema>
    
<xsd:element name="dataroot">
      
<xsd:complexType>
        
<xsd:sequence>
          
<xsd:element ref="Author" minOccurs="0" maxOccurs="unbounded"/>
        
</xsd:sequence>
        
<xsd:attribute name="generated" type="xsd:dateTime"/>
      
</xsd:complexType>
    
</xsd:element>
    
<xsd:element name="Author">
      
<xsd:annotation>
        
<xsd:appinfo>
          
<od:index index-name="id" index-key="id " primary="no" unique="no" clustered="no"/>
        
</xsd:appinfo>
      
</xsd:annotation>
      
<xsd:complexType>
        
<xsd:sequence>
          
<xsd:element name="id" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
            
<xsd:simpleType>
              
<xsd:restriction base="xsd:string">
                
<xsd:maxLength value="50"/>
              
</xsd:restriction>
            
</xsd:simpleType>
          
</xsd:element>
          
<xsd:element name="firstname" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
            
<xsd:simpleType>
              
<xsd:restriction base="xsd:string">
                
<xsd:maxLength value="50"/>
              
</xsd:restriction>
            
</xsd:simpleType>
          
</xsd:element>
          
<xsd:element name="lastname" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
            
<xsd:simpleType>
              
<xsd:restriction base="xsd:string">
                
<xsd:maxLength value="50"/>
              
</xsd:restriction>
            
</xsd:simpleType>
          
</xsd:element>
          
<xsd:element name="state" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
            
<xsd:simpleType>
              
<xsd:restriction base="xsd:string">
                
<xsd:maxLength value="50"/>
              
</xsd:restriction>
            
</xsd:simpleType>
          
</xsd:element>
        
</xsd:sequence>
      
</xsd:complexType>
    
</xsd:element>
  
</xsd:schema>
  
<dataroot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" generated="2007-04-13T15:35:20">
    
<Author>
      
<id>1</id>
      
<firstname>123</firstname>
      
<lastname>321</lastname>
      
<state>China</state>
    
</Author>
    
<Author>
      
<id>2</id>
      
<firstname>456</firstname>
      
<lastname>654</lastname>
      
<state>USA</state>
    
</Author>
    
<Author>
      
<id>3</id>
      
<firstname>789</firstname>
      
<lastname>987</lastname>
      
<state>Japan</state>
    
</Author>
  
</dataroot>
</root>

问题:几个组件编译都通过了,但是运行时会出现如下问题,不知道是为什么
Column 'state' does not belong to table dataroot. 
Source Error: 


Line 37:             DataSet ds = AuthorDB.AuthorDB.GetStates();
Line 38:             foreach (DataRow row in ds.Tables[0].Rows)
Line 39:                 states.Add((string)row["state"]);
Line 40:             return states;
 

posted on 2007-04-13 17:08  散步的蠕虫  阅读(804)  评论(0编辑  收藏  举报

导航