如何存储 sqlDataReader 的值?并进行比较?
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
中,以检查是否有可用座位。
我怎样才能做到这一点?因为它不断地去往其他地方。
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();
As you can see here, I would like to store flight_seat
column from SQL into availableSeat
in order to check whether there is a seat available.
How can I do that? because it keeps going to else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
availableSeat.Equals()
是比较而不是赋值。您需要分配该值。例如availableSeat.Equals()
is a comparison not an assignment. You would need to assign the value. For example将其更改为:
Equals()
方法用于比较目的(相等)而不是赋值。此外,在 C# 中,通过将=
左侧的变量设置为右侧语句的结果来完成赋值。您需要将
reader
中的值转换为int
,因为它存储为object
。您还应该检查null
值,因为它来自数据库:Change it to:
The
Equals()
method is used for comparison purposes (equality) and not assignment. Furthermore, assignments are done in C# by setting the variable on the left of the=
to the result of the statement on the right.You need to cast the value in the
reader
to anint
because it is stored as anobject
. You should also check for anull
value since this is coming from a database: