int availableSeat = 0;
string query = "select flight_no flight_seat from flight_info where flight_departure_time = @selectedFlight AND flight_from = @flightFrom AND flight_to = @flightTo ";
string url = "Server=localhost;Database=flight;uid=******;password=*****";
con1 = new MySqlConnection(url);
con1.Open();
cmd1 = new MySqlCommand(query,con1);
cmd1.Parameters.AddWithValue("@selectedFlight",comboBox3.SelectedItem);
cmd1.Parameters.AddWithValue("@flightFrom",comboBox1.SelectedItem);
cmd1.Parameters.AddWithValue("@flightTo",comboBox2.SelectedItem);
reader = cmd1.ExecuteReader();
while (reader.Read())
{
availableSeat.Equals(reader["flight_seat"]);
if(availableSeat > 0)
{
MessageBox.Show("The seat is available!!");
}
else
{
MessageBox.Show("Sorry, all seat has been booked!!");
}
}
con1.Close();
正如您在此处所见,我想将SQL中的flight_seat
列存储到availableSeat
,以检查是否有可用的座位。
我该怎么做?因为它不断前进。
答案 0 :(得分:1)
将其更改为:
availableSeat = (int)reader["flight_seat"]);
Equals()
方法用于比较目的(相等)而不是赋值。此外,通过将=
左侧的变量设置为右侧语句的结果,可以在C#中完成赋值。
您需要将reader
中的值转换为int
,因为它存储为object
。您还应检查null
值,因为它来自数据库:
availableSeat = reader["flight_seat"].Equals(DBNull.Value)
? 0
: (int)reader["flight_seat"];
答案 1 :(得分:1)
availableSeat.Equals()
是比较而不是作业。您需要分配值。例如
availableSeat = (int)reader["flight_seat"];