SqlCommand selectCommand=con.CreateCommand();
selectCommand.CommandText="SELECT * FROM table where ID=1";
sqlDataAdapter1.SelectCommand=selectCommand;
con.Open();
sqlDataAdapter1.Fill(ds1,"table");
con.Close();
this.DataGrid1.DataBind();
代码如上,在其他页面(代码基本相同,只有数据库名称和查询条件不同)查询可以实现
但是在主页面却总是实现不了,不知道什么原因

解决方案 »

  1.   

    跟踪调试时也得到了sqlDataAdapter1.SelectCommand的值,就是DataGrid一直显示所有记录
      

  2.   

    SqlCommand selectCommand=con.CreateCommand();
    selectCommand.CommandText="SELECT * FROM table where ID=1";
    sqlDataAdapter1.SelectCommand=selectCommand;
    con.Open();
    sqlDataAdapter1.Fill(ds1,"table");
    con.Close();
    this.DataGrid1.DataSource=sqlDataAdapter1;//加上这句试试。
    this.DataGrid1.DataBind();
      

  3.   

    你没给DataGrid1加上数据源,绑定到哪了??
      

  4.   

    this.DataGrid1.DataSource = ds1.Tables["table"]'
    this.DataGrid1.DataBind();
      

  5.   

    page_load()
    {
      if(!Page.IsPostBack)
    {
    绑定的总数据源
    }
    }
      

  6.   

    SqlCommand selectCommand=con.CreateCommand();
    selectCommand.CommandText="SELECT * FROM table where ID=1";
    sqlDataAdapter1.SelectCommand=selectCommand;
    //con.Open();
    sqlDataAdapter1.Fill(ds1);
    //con.Close();
    this.DataGrid1.DataSource = ds1;
    this.DataGrid1.DataBind();
      

  7.   

    mySQL = "SELECT type from ContractType order by type"
            Dim myConn As OracleConnection = New OracleConnection(ConfigurationSettings.AppSettings("ConnectionString"))
            Dim myCommand As OracleDataAdapter = New OracleDataAdapter(mySQL, myConn)        myCommand.Fill(myDataSet)       Return myDataSet
      

  8.   

    “跟踪调试时也得到了sqlDataAdapter1.SelectCommand的值,就是DataGrid一直显示所有记录”数据的访问没有问题,问题就在你的DataGrid的数据绑定:
    SqlCommand selectCommand=con.CreateCommand();
    selectCommand.CommandText="SELECT * FROM table where ID=1";
    sqlDataAdapter1.SelectCommand=selectCommand;
    con.Open();
    sqlDataAdapter1.Fill(ds1,"table");
    con.Close();     //以上应该没有问题
    ////--------------///
    this.DataGrid1.DataSource = ds1.Tables["table"];   //你怎么没有??
    this.DataGrid1.DataBind();
      

  9.   

    SqlCommand selectCommand=con.CreateCommand();
    selectCommand.CommandText="SELECT * FROM table where ID=1";
    sqlDataAdapter1.SelectCommand=selectCommand;
    //con.Open();
    sqlDataAdapter1.Fill(ds1);
    //con.Close();
    this.DataGrid1.DataSource = ds1;
    this.DataGrid1.DataBind();
      
      

  10.   

    this.DataGrid1.DataSource=sqlDataAdapter1这句话好像不太对
    this.DataGrid1.DataSource = ds1.Tables["table"];  加上了试了一下还是不行
    不是数据绑定问题,因为可以得到所有值,不论是否符合查询条件
      

  11.   

    你用“调试”里的“即时调试”进行跟踪调试,看看你在SQL执行之后返回的 ?ds1.Tables["table"].Row.Count 的值是不是你所要的记录的数目,用?ds1.Tables["table"].Row[0]["ID"]是否存在!!
    我个人认为你的问题并不是很严重,关键是你没有正确的进行跟踪调试! 先分析再调试很重要,不易南辕北辙!!!