//查询排量方法
        private void Selpinpai(string pai)
        {
            string sql =string.Format("select * from carsinfo where discharge='%{0}%'",pai);
            try
            {
                DataSet ds = new DataSet();
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                ds.Tables["CarsInfo"].Rows.Clear();
                while (reader.Read())
                {
                    datagridview1.DataSource = ds.Tables["CarsInfo"];
                }
                reader.Close();            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }        }我是从数据库查出数据,先清空DataGridView然后将查出的内容填充到控件中,目前控件没有反应,sql没问题,传参也没问题。

解决方案 »

  1.   

    数据根本没有读到DataSet里去嘛,建议你改用SqlDataAdapter吧。
      

  2.   

    SqlDataReader 这个会长时间占用连结
      

  3.   


    奥,使用SqlDataAdapter的作用和SqlDataReader一样吗?也可以根据查询语句进行查询吗?
      

  4.   

    private void Selpinpai(string pai)
      {
      string sql =string.Format("select * from carsinfo where discharge='%{0}%'",pai);
      try
      {
      DataSet ds = new DataSet();
      SqlCommand command = new SqlCommand(sql, DBHelper.connection);
      DBHelper.connection.Open();
      SqlDataReader reader = command.ExecuteReader();
     
      datagridview1.DataSource =reader;
     
      reader.Close();
     DBHelper.connection.close();  }
      catch (Exception ex)
      {
      Console.Write(ex.Message);
      }
      finally
      {
      DBHelper.connection.Close();
      }  }
      

  5.   

    你都没有把数据付给DataSet,可以直接把reader赋给DataGridView.DataSource或者把reader里面的数据取出来放在集合里面在赋给DataGridView.DataSource也是可以的
      

  6.   


     //查询排量方法
            private void Selpinpai(string pai)
            {
                
                string sql =string.Format("select * from carsinfo where discharge='%{0}%'",pai);
                try
                {
                    DataSet ds = new DataSet();
                    SqlDataAdapter adapter = new SqlDataAdapter(sql,DBHelper.connection);
                    DBHelper.connection.Open();
                    
                    adapter.Fill(ds,"CarsInfo");
                    
                    datagridview1.DataSource =ds.Tables["CarsInfo"];
                    
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                }
                finally
                {
                    DBHelper.connection.Close();
                }        }为什么我写成这样,但是查询的时候DataGridView中还是没有数据显示
      

  7.   

    有两种解决办法:用模糊查询的:把sql语句中的select * from carsinfo where discharge='%{0}%'",pai 的等于号改成like 在使用模糊查询时,是不能直接用等于号的 ;要是你不想用模糊查询的话:你把那两个百分号去掉,