在C#中调用dataset中的table[0]时老是提示找不到table[0]怎么办?我也给dataset用fill填充了,就是提示找不到table[0]呀,请各位高手帮忙呀

解决方案 »

  1.   

    strSql = strSql + " where InventoryScheduleID ='" + InventoryScheduleID +"'";
    strSql = strSql +" and CountNo = " + InCountNO ;
    strSql = strSql + " and CountLine =" + InCountLine ; DataSet ds = new DataSet();
    try
    {
    SqlCommand cmd = new SqlCommand(strSql, cn);
    SqlDataAdapter myDataAdapter = new SqlDataAdapter();
    myDataAdapter.SelectCommand = cmd;
    myDataAdapter.Fill(ds);

    cmd.Dispose();
    myDataAdapter.Dispose();
    cn.Close();
    // ds = DBProc.OpenDataSet(strSql);
    }
    catch(SqlReadFailedException ex)
    {
    message.ShowMessage("DataSetOpenFailed", ex.Message);
    return false;
    }
    // int TablesCount = ds.Tables.Count;
    // if (TablesCount == 0)
    // {
    // ds.Reset();
    // ds = DBProc.OpenDataSet(strSql);
    // }
    if(ds.Tables[0].Rows.Count == 0)
    {
    return false;
    }
    else
    {
    return true;
    }
    以上就是我写的一段程序,当执行到if(ds.Tables[0].Rows.Count == 0)时就报错说找不到table[0],我找了好几天了也没找出错,头都大了。
      

  2.   

    SqlDataAdapter myDataAdapter = new SqlDataAdapter(strSql, cn);
    myDataAdapter.Fill(ds,"MyDataTable");
    这样子看看