ASP.NET 2.0之ObjectDataSourc控件

王朝asp·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

学习asp.net2.0有一段时间了,我不是从1.1开始的,所以,也说不出2.0相对于1.1来讲有多大的变化或是在操作上有多大的便利,我这里只把我学习2.0的一些小经验与大家分享.

2.0中有一个ObjectDataSource数据源控件,通过它,可以把中间层或其它层的数据绑定到dropdownlist/datalist/repeater/gridview等数据控件中,为什么这里只ObjectDataSource而不提SqlDataSource或其它的数据源控件呢?因为我觉得SqlDataSource体现不出asp.net在进行项目开发的多层性,在用SqlDataSource的过程中,所有的数据库连接串、sql语句都会在.aspx页面中显示出来,为以后的维护再来诸多不便.为了体现出asp.net的多层开发,我选择了ObjectDataSource,通过它,可以体现出asp.net进行多层开发的优势.

首先,我用的数据库是MS sqlserver2000,开发工具当然是vs2005了.

大家先看一下我的解决方案

这里采用了通用的多层架构,当然,只是简单的实体层/数据层和表现层,这里只为了演示.在这个小项目中,读取的是数据库中一个名为Links数据表的内容.

Entity层:实体层.这里只是对于LinkEntity实体的一个描述.

using System;

using System.Collections.Generic;

using System.Text;

namespace Blog.Entity

{

public class LinksEntity

{

private string _title;

private string _description;

private string _url;

private string _logo;

private int _id;

public int ID

{

get { return this._id; }

set { this._id = value; }

}

public string Title

{

get

{

return this._title;

}

set

{

this._title = value;

}

}

public string Description

{

get { return this._description; }

set { this._description = value; }

}

public string Url

{

get { return this._url; }

set { this._url = value; }

}

public string Logo

{

get { return this._logo; }

set { this._logo = value; }

}

}

}

DAL层:看名字就知道是一个数据访问层了,所有的数据库读写操作都会在这里.

using System;

using System.Collections.Generic;

using System.Collections;

using System.Text;

using System.Configuration;

using System.Web.Configuration;

using System.Data.SqlClient; //使用ms sqlserver

using System.Data;

namespace Blog.DAL

{

public class Links

{

private string _connStr = WebConfigurationManager.ConnectionStrings["defaultConnections"].ConnectionString; //获取在web.config中定义的数据库连接串

public Links() { }

//该方法获取所有links中的内容,这里不用以常用的Dataset/datatable/dataReader返回,而是linksEntity的实例数组

public Blog.Entity.LinksEntity[] getList()

{

ArrayList al = new ArrayList();

string sql = "select * from links";

using (SqlConnection conn = new SqlConnection(_connStr))

{

SqlCommand comm = new SqlCommand(sql, conn);

conn.Open();

using (SqlDataReader r = comm.ExecuteReader())

{

while (r.Read())

{

Blog.Entity.LinksEntity Entity = new Blog.Entity.LinksEntity(); //创建一个新的linksEntity实体

Entity.Description = r["description"].ToString(); //赋值

Entity.ID = (int)r["id"];

//Entity.isIndex = (int)r["isindex"];

// Entity.isLogo = (int)r["isLogo"];

Entity.Logo = r["logo"].ToString();

Entity.Title = r["title"].ToString();

Entity.Url = r["url"].ToString();

al.Add(Entity); //将实体添加至ArrayList

}

}

}

return (Blog.Entity.LinksEntity[])al.ToArray(typeof(Entity.LinksEntity)); //返回

}

public void updateLinks(Blog.En

[1] [2] [3] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航