根据Access Table检查ID并循环访问SQL语句

时间:2010-12-02 13:08:30

标签: c# asp.net sql

我发布question yesterday用于选择gridview中的项目,然后将其添加到列表中,我找到了解决方案,它获取了gridview的值,在这种情况下是ID号,然后输出他们在gridview的另一页上。

我的问题是,是否可以针对访问数据库中的表检查ID的值,然后显示有关该产品的更多信息?

我的购物篮页面上有以下代码:

    protected void Page_Init(object sender, EventArgs e)
    {
        if (Session["CartSess"] != null)
        {
            List<BasketClass> cart = (List<BasketClass>)Session["CartSess"];

            foreach (BasketClass BookID in (List<BasketClass>)Session["CartSess"])
            {
                GridView1.DataSource = cart;

                GridView1.DataBind();
                AccessDataSource1.SelectCommand = "SELECT [BookID], [book_title] FROM [tblBook] WHERE [BookID]=" + BookID;
            }
        }
    }

protected void Page_Init(object sender, EventArgs e) { if (Session["CartSess"] != null) { List<BasketClass> cart = (List<BasketClass>)Session["CartSess"]; foreach (BasketClass BookID in (List<BasketClass>)Session["CartSess"]) { GridView1.DataSource = cart; GridView1.DataBind(); AccessDataSource1.SelectCommand = "SELECT [BookID], [book_title] FROM [tblBook] WHERE [BookID]=" + BookID; } } }

问题是,它只返回一个基于用户选择的最后一个值的值,是否有办法循环遍历每个值,因此它将继续添加到我放在页面上的新gridview中绑定到AccessDataSource1?

1 个答案:

答案 0 :(得分:0)

我会尝试类似的事情:

string items = "";
string sql = "SELECT [BookID], [book_title] FROM [tblBook] WHERE [BookID] in (";
foreach (BasketClass BookID in (List<BasketClass>)Session["CartSess"])
{
    items += BookID & ",";
}
//Remove the trailing ","
items.Remove(str.Length - 1, 1);
GridView1.DataSource = cart;
GridView1.DataBind();
AccessDataSource1.SelectCommand = sql & items & ")";