情况是这样子的:我现在要插入数据 就是在 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);
}
}
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);
}
}
{
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);
}
}
高级的
。net体系结构 泛型 对象和类型 继承 数组 委托和事件 反射 内存管理和指针 等等