using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;using System.Data.OleDb;public partial class fenye_Gou_Wu_Che : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {        //购物车信息显示
        this.Label1.Text = "请确认您的购物清单";        //给出数据库路径和连接方式
        string strPath = "App_Data/db.mdb";
        string strConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"] + strPath;        //生成OleDbConnection实例,构建数据库连接
        OleDbConnection cn = new OleDbConnection(strConStr);
        cn.Open();        //构建数据库实例
        DataSet ds = new DataSet();        //构建表实例
        DataTable dt = ds.Tables.Add("GouWuChe");
        //构建表结构
        dt.Columns.Add("id", typeof(string));
        dt.Columns.Add("zc", typeof(string));
        dt.Columns.Add("zname", typeof(string));
        dt.Columns.Add("xg", typeof(string));
        dt.Columns.Add("price", typeof(string));
        //定义键
        DataColumn[] myKey = new DataColumn[1];
        myKey[0] = dt.Columns["id"];        //将Session里的数据提取出来,并显示
        string strSessionID = Convert.ToString(Session["id"]);
        string[] strSessionIDS = strSessionID.Split('&');
        for (int i = 0; i < strSessionIDS.Length - 1; i++)
        {
            string DataID = "id=" + strSessionIDS[i];            //给出SQL语句
            string strSQL = "Select * From zs Where " + DataID;
                       //构建OleDbCommand实例,导出数据
            OleDbCommand com = new OleDbCommand(strSQL, cn);
                       //构建OleDbDataReader实例,载入数据
            OleDbDataReader oddr = com.ExecuteReader(CommandBehavior.CloseConnection);
            oddr.Read();
           
            //将oddr(OleDbDataReader)里的数据添加到dt(DataTable)
            DataRow dr = dt.NewRow();
            dr[0] = oddr[0].ToString();
            dr[1] = oddr[1].ToString();
            dr[2] = oddr[2].ToString();
            dr[3] = oddr[3].ToString();
            dr[4] = oddr[4].ToString();
            dt.Rows.Add(dr);
        }
        cn.Close();        this.GridView1.DataSource = ds;
        this.GridView1.DataKeyNames = new string[] { "id" };
        this.GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int intSelectLineNo = Convert.ToInt32(GridView1.Rows[e.RowIndex].RowIndex.ToString());        //将与之对应的Session里的数据删除
        string strSessionID = Convert.ToString(Session["id"]);
        string[] strSessionIDS = strSessionID.Split('&');
        strSessionID = "";
        for (int i = 0; i < strSessionIDS.Length - 1; i++)
        {
            if (intSelectLineNo == i)
            {
                continue;
            }
            else
            {
                strSessionID = strSessionID + strSessionIDS[i] + "&";
            }
        }
        Session["id"] = null;
        Session["id"] = strSessionID;        Response.Redirect("Gou_Wu_Che.aspx");
    }
}这是我的购物车页面代码,只能加一个表中数据库到购物车,怎么改可以吧三个表的数据加入同一个购物车?

解决方案 »

  1.   

    你可以加第一个,难道还不可以加第二个,直接再把你第一个表加载的代码改下对象名字,SQL语句,再放到你的购物车里不就是把第二个表的数据放进去了?
      

  2.   

    加入购物车,在你代码里其实就是将记录加入Session在Session中加入集合,比如
    Session["Order"]=字典集合 key表示相应的表,value就是相关的id集合或者根据几个表分别创建Session["id1"] Session["id2"]....将记录加入
      

  3.   

    创建一个object类型的购物车,这个购物出再加个属性 是 类型名称,全部加进object中,最后要使用的时候 根据类型名称去 强转object为指定类型就好了