OleDataReader reader;while (reader.Read())
{
  //...
}

解决方案 »

  1.   

    DataReader dr = cmd.ExecuteReader();
    if(dr.read()) //check whether the DataReader has got some values
    {
      ...
    }
    dr.close();
      

  2.   

    SqlCommand thisCommand=thisConnection.CreateCommand();
    thisCommand.CommandText="SELECT * FROM 班级";
    SqlDataReader thisReader=thisCommand.ExecuteReader();
    while(thisReader.Read())
    {
    if(thisReader["年级"].ToString().Trim()==comboBox1.Text.Trim() && thisReader["班级名称"].ToString().Trim()==textBox1.Text.Trim())
    {
    MessageBox.Show("该班已存在!");
    i=0;
    thisReader.Close();
    thisConnection.Close();
    break;
    }
    else
    {
    i=1;
    }
    } thisReader.Close(); if(i==1)
    {
    SqlDataAdapter thisAdapter=new SqlDataAdapter("SELECT * FROM 班级",thisConnection);
    SqlCommandBuilder thisBuilder=new SqlCommandBuilder(thisAdapter); DataSet thisDataSet=new DataSet(); thisAdapter.Fill(thisDataSet,"班级"); DataRow thisRow=thisDataSet.Tables["班级"].NewRow();
    thisRow["年级"]=comboBox1.Text.Trim();
    thisRow["班级名称"]=textBox1.Text.Trim();
    thisRow["人数"]="0";
    thisRow["班主任"]=textBox2.Text.Trim();
    thisRow["班主任手机"]=textBox3.Text.Trim();

    thisDataSet.Tables["班级"].Rows.Add(thisRow); thisAdapter.Update(thisDataSet,"班级"); thisConnection.Close(); MessageBox.Show("修改成功!"); this.Close();
      

  3.   

    用IsNull方法。可以判断某个字段内容是否为空值
      

  4.   

    SqlCommand thisCommand=thisConnection.CreateCommand();
    thisCommand.CommandText="SELECT * FROM 班级";
    //把数据访问放到try ... catch ... 里,看看是否有异常抛出
    try
    {
    SqlDataReader thisReader=thisCommand.ExecuteReader();
    while(thisReader.Read())
    {
    if(thisReader["年级"].ToString().Trim()==comboBox1.Text.Trim() && thisReader["班级名称"].ToString().Trim()==textBox1.Text.Trim())
    {
    MessageBox.Show("该班已存在!");
    i=0;
    thisReader.Close();
    thisConnection.Close();
    break;
    }
    else
    {
    i=1;
    }
    } thisReader.Close(); if(i==1)
    {
    SqlDataAdapter thisAdapter=new SqlDataAdapter("SELECT * FROM 班级",thisConnection);
    SqlCommandBuilder thisBuilder=new SqlCommandBuilder(thisAdapter); DataSet thisDataSet=new DataSet(); thisAdapter.Fill(thisDataSet,"班级"); DataRow thisRow=thisDataSet.Tables["班级"].NewRow();
    thisRow["年级"]=comboBox1.Text.Trim();
    thisRow["班级名称"]=textBox1.Text.Trim();
    thisRow["人数"]="0";
    thisRow["班主任"]=textBox2.Text.Trim();
    thisRow["班主任手机"]=textBox3.Text.Trim();

    thisDataSet.Tables["班级"].Rows.Add(thisRow); thisAdapter.Update(thisDataSet,"班级"); thisConnection.Close(); MessageBox.Show("修改成功!"); this.Close();
    }
    catch(Exception e)
    {
      MessageBox.Show(e.Message);
    }