情况是这样子的:我现在要插入数据  就是在 kongfang=‘是’ 中的房间插入数据  并且输入到数据库中 房间号还得是这个房间号 如果在数据库中没有相对应的房间号 那就不能插入数据。大体就是这样 高手速来解答!!!!!房间号是 RoomNum int类型, tbRoomNum是相对应的输入房间号的textbox。 
private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                string updateUserInfo = string.Format("update UserInfo set RoomNum='{0}',Person='{1}'",tbRoomNum.Text.Trim(),tbPerson.Text.Trim());
                string selKongFang = string.Format("select kongfang from UserInfo where RoomNum = '{0}'",tbRoomNum.Text.Trim());
                SqlCommand com = new SqlCommand(selKongFang, DBhelper.connection);
                DBhelper.connection.Open();
                SqlDataReader reader=com.ExecuteReader();
                
                if (tbRoomNum.Text.Trim() == "")
                {
                    MessageBox.Show("房间号不能为空");
                }
                else
                {
                    while(reader.Read())
                    {
                        if (reader[0].ToString() == "是")
                        {
                            //MessageBox.Show("该房可以出租");
                            SqlCommand com1 = new SqlCommand(updateUserInfo, DBhelper.connection);
                            SqlDataAdapter adapter = new SqlDataAdapter(com1);
                            DataSet dataset = new DataSet();
                            adapter.Fill(dataset,"UserInfo");
                        
                            
                        }
                        else
                        {
                            MessageBox.Show("该房已出租");
                        }
                        DBhelper.connection.Close();
                    }                }                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

解决方案 »

  1.   

    你不熟悉基本的ado.net编程,写的代码不对!private void btnOk_Click(object sender, EventArgs e)
      {
      try
      {
      //判断应该提前,如果都没有输入房间号,是否都不需要去执行?
      if (tbRoomNum.Text.Trim() == "")
      {
      MessageBox.Show("房间号不能为空");
      }
      else
      {
      //最好用参数方式,不要用字符串拼接,容易出问题。
      string updateUserInfo = string.Format("update UserInfo set RoomNum='{0}',Person='{1}'",tbRoomNum.Text.Trim(),tbPerson.Text.Trim());  string selKongFang = string.Format("select kongfang from UserInfo where RoomNum = '{0}'",tbRoomNum.Text.Trim());  SqlCommand com = new SqlCommand(selKongFang, DBhelper.connection);
      DBhelper.connection.Open();
      SqlDataReader reader=com.ExecuteReader();
      
      //如果一个RoomNum在UserInfo中只有一个记录,就不要循环了
      reader.Read()
      if (reader.getString(0) == "是")
      {
       //关闭reader
       reader.Close();
       
       //MessageBox.Show("该房可以出租");
       SqlCommand com1 = new SqlCommand(updateUserInfo, DBhelper.connection);
       if(DBhelper.connection.State=ConnectionState.Closed)
       {
          DBhelper.connection.Open();
       }
             try
          {
              com1.ExcuteNoQuery();//执行更新语句,不需要返回
          }
          catch(Exception ex)
          {
         MessageBox.show(ex.Message);  
       }
      }
      else
      {
      MessageBox.Show("该房已出租");
      }
      //关闭前要判断状态
      if(DBhelper.connection.State=ConnectionState.Open)
        DBhelper.connection.Close();
      }  }   
      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }
      }
      

  2.   

    C#编程语言详解  最基础的   asp.net 入门经典中文(第4版)
    高级的   
    。net体系结构   泛型  对象和类型  继承  数组  委托和事件 反射 内存管理和指针 等等
      

  3.   

    那应该在update之前先判断房间号是否存在或者用程序控制房间号,用下拉框把值给它赋上,保证房间号是有效的,下拉框可以动态从数据库拿值,不过效率会低。