Ilist绑定Datasource
以前在写datalist控件的时候想用sqldatareader来读取数据库然后绑定到datalist的datasource.
最后查询了一下MSDN发现这是官方的解释:(绑定数据源必须是实现 System.Collections.IEnumerable 接口(例如 System.Data.DataView、System.Collections.ArrayList 或 System.Collections.Hashtable)或 IListSource 接口的对象,才能绑定到从 BaseDataList 类派生的控件。)MSDN:http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.basedatalist.datasource(v=VS.80).aspx
以下是代码:
这是数据访问层(
代码
//sqlhelper
#region 执行一个返回读取器的sql命令
/// <summary>
/// 用执行的数据库连接执行一个返回读取器的sql命令
/// </summary>
/// <param name="commandType">命令类型(存储过程, 文本, 等等)</param>
/// <param name="commandText">存储过程名称或者sql命令语句</param>
/// <param name="commandParameters">执行命令所用参数的集合</param>
/// <returns>包含结果的读取器</returns>
public static SqlDataReader ExecuteGetReader(CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
//创建一个SqlCommand对象
SqlCommand command = new SqlCommand();
//创建一个SqlConnection对象
SqlConnection connection = new SqlConnection(ConnectionString);
//在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在,
//因此commandBehaviour.CloseConnection 就不会执行
try
{
//调用 PrepareCommand 方法,对 SqlCommand 对象设置参数
PrepareCommand(command, connection, null, commandType, commandText, commandParameters);
//调用 SqlCommand 的 ExecuteReader 方法
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
//清除参数
command.Parameters.Clear();
return reader; //注意不能关闭连接,否则调用方无法读取数据。
}
catch
{
//关闭连接,抛出异常
connection.Close();
throw; //抛出什么?
}
}
#endregion
// 数据层
代码
public IList<Eshop_Model.Product> GetNewProduct()
{
IList<Eshop_Model.Product> list = new List<Eshop_Model.Product>();
Eshop_Model.Product product = null;
string CmdText = "GetNewProduct";
using (SqlDataReader dr = SQLHelper.ExecuteGetReader(System.Data.CommandType.StoredProcedure, CmdText, null))
{
while (dr.Read())
{
product = new Eshop_Model.Product(dr.GetInt32(0), dr.GetString(1));
list.Add(product);
}
}
return list;
}
)
这是实体层(
代码
[Serializable]
public class Product
{
//商品编号
private int _ProductID;
//商品姓名
private string _ProductName;
public Product(int productId, string productName,)
{
this._ProductID = productId;
this._ProductName = productName;
}
public int ProductID
{
get { return this._ProductID; }
set { this._ProductID = value; }
}
public string ProductName
{
get { return this._ProductName; }
set { this._ProductName = value; }
}
}
)
这是业务逻辑层(
Eshop_Dal.ProductInfo product=new Eshop_Dal.ProductInfo();
public IList<Eshop_Model.Product> GetNewProduct()
{
return product.GetNewProduct();
}
)
这是WEB页面(
代码
<asp:Repeater ID="NewProduct" runat="server">
<HeaderTemplate>
<div style="background-color:Blue; width:auto; height:10px;">新到商品</div>
</HeaderTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='~/ProductList.aspx?id=<%#DataBinder.Eval(Container.DataItem, "ProductID")%>'></asp:HyperLink>
<asp:HyperLink ID="HyperLink1" runat="server" Text=' <%#DataBinder.Eval(Container.DataItem, "ProductName")%>'></asp:HyperLink>
</ItemTemplate>
</asp:Repeater>
代码
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
ShowNewProduct();
ShowPopularProduct();
}
}
Eshop_Bll.ProductInfo product = new Eshop_Bll.ProductInfo();
void ShowNewProduct()
{
System.Collections.Generic.IList<Eshop_Model.Product> productlist = product.GetNewProduct();
NewProduct.DataSource = productlist;
NewProduct.DataBind();
}

浙公网安备 33010602011771号