代码如下
if (!Page.IsPostBack)
        {
            string productID;
            productID = Request.QueryString["id"].ToString();
            Hashtable myCar;
            if (Session["myCar"] == null)
            {
                myCar = new Hashtable();
            }
            else
            {
                myCar = (Hashtable)Session["myCar"];
            }
            if (myCar.Contains(productID))
            {
                myCar[productID] = (int)myCar[productID] + 1;
            }
            else
            {
                myCar.Add(productID, 1);
            }
            Session["myCar"] = myCar;
            string strProducts;
            strProducts = "('";
            foreach (string x in myCar.Keys)
            {
                strProducts = strProducts + x + "','";
            }
            strProducts += "')";
            string  strSql;
           
            strSql = "select pc_id,pc_name,pc_price from product where pc_id in " + strProducts;
            OleDbDataAdapter myAdapt = new OleDbDataAdapter(strSql, db.connstring);
            DataSet myDS = new DataSet();
            myAdapt.Fill(myDS, "prdocut");
            DataTable myTable;
            //myDS.Tables["tb_product"].DefaultView;
            myTable = myDS.Tables["prdocut"];
            if (myTable.Rows.Count == 0)
            {
                Response.Write("aa");
            }
            DataColumn myColumn = new DataColumn();
            myColumn.ColumnName = "fld_num";
            myColumn.DataType = System.Type.GetType("System.Int32");
            myColumn.DefaultValue = 0;
            myTable.Columns.Add(myColumn);
            if (myTable.Rows.Count == 0)
            {
                GridView1.Visible = false;
            }
            else
            {
                GridView1.Visible = true;
            }
            DataColumn[] Keys = new DataColumn[1]; ;
            Keys[0] = myTable.Columns["pc_id"];
            myTable.PrimaryKey = Keys;
            foreach (string x in myCar.Keys)
            {
                myTable.Rows.Find(x)["fld_num"] = (int)myCar[x];
            }
            GridView1.DataSource = myTable;
            GridView1.DataBind();
        }
   这里的strSql = "select pc_id,pc_name,pc_price from product where pc_id in " + strProducts;
 pc_id 可不可以 弄成自动编号的  我本来是自动编号  可是出错  我只能改成文本的了这里的in 是什么意思  
是查询 所有 strProducts 里的数据吗
 strProducts = "('";
            foreach (string x in myCar.Keys)
            {
                strProducts = strProducts + x + "','";
            }
            strProducts += "')";这里的 
strProducts = "('";
strProducts += "')";
又有什么用 是不是字符串 要这样加 如果是数字应该怎么改 没了自动编号 很不方便请高手 教我 把pc_id改回 自动编号不出错的方法 

解决方案 »

  1.   

    1、这里的in 是什么意思  :查询strProducts 表的所有 pc_id 
    2、strProducts += "')";:拼接sql字符串
    3、设置自动编号 ALTER   TABLE   strProducts   ALTER   COLUMN  pc_id(IDENTITY(1,1))(前提是 表里没有数据且字段为数据类型)建议多看看书(你提的问题都是很基础的东西)
      

  2.   

    多谢LS的  我现在的问题是  把pc_id这个主键 改成自动编号 怎么保证不出错我现在是改成文本 才正常运行 
      

  3.   

    首先必须了解 数据库 sql语句中 IN 的用法例:select * form citys where city IN ('chengdu',chongqin')这句意思 从citys 表中查找多有city='chengdu'或者city='chongqin'的信息IN 后面的信息必须用括号()括起来
     
    所以 楼主的
    strProducts = "('";
      foreach (string x in myCar.Keys)
      {
      strProducts = strProducts + x + "','";
      }
      strProducts += "')";这里的  
    strProducts = "('";
    strProducts += "')";
    意思很明显啦!而pc_id 如果改成自动编号的话后面的strProducts应做相应修改
    请楼主试试
      

  4.   

    in表示pc_id是否出现在后面的括号中列出的标识中
    http://msdn.microsoft.com/zh-cn/library/ms177682.aspx
    如果pc_id是数字类型,则后面括号中的id都不要加单引号:
    string strProducts;
                strProducts = "('";
                foreach (string x in myCar.Keys)
                {
                    strProducts = strProducts + x + "','";
                }
                strProducts += "')";
    这句:
    myTable.Rows.Find(x)["fld_num"] = (int)myCar[x];
    改成:
    myTable.Rows.Find(Convert.ToInt32(x))["fld_num"] = (int)myCar[x];
      

  5.   

    1)in是指重数组myCar.Keys里面取值。
    2)strProducts = "('";
    strProducts += "')"值取出数组里面的数组后一什么形式显示;这里加这句话指显示为字符串(奔驰,宝马,保时捷)这样的形式
    3)strSql = "select pc_id,pc_name,pc_price from product where pc_id in " + strProducts;你数据源里面的数据怎么排列的我不晓得怎么写。
      

  6.   

     strProducts = "('";
      foreach (string x in myCar.Keys)
      {
      strProducts = strProducts + x + "','";
      }
      strProducts += "')";拼接查询条件如:myCar.Keys 有:1,2,3
    ==>  strProducts="('1,2,3,')"; 这个方式会多一个“,”
                strSql = "select pc_id,pc_name,pc_price from product where pc_id in " + strProducts;
    ==》
                strSql = "select pc_id,pc_name,pc_price from product where pc_id in ('1,2,3,')" ;如果pc_id 是自动编号,上面 不能拼接 ‘’ 这对单引号!还有拼接最好用StringBuilderStringBuilder sb =new StringBuilder();
      foreach (string x in myCar.Keys)
                {
                    if(sb.Length>0){
    sb.Append(",");
    }
    sb.Append(x);
                }
    strSql = "select pc_id,pc_name,pc_price from product ";
    if(sb.Length>0){
    strSql += " where pc_id in ("+sb.ToString() +")";
    }