C# 如何从sqlDataReader存储值?比较一下呢?
正如您在这里看到的,我想将SQL中的C# 如何从sqlDataReader存储值?比较一下呢?,c#,mysql,datareader,C#,Mysql,Datareader,正如您在这里看到的,我想将SQL中的flight\u seat列存储到availableSeat中,以检查是否有可用的座位 我该怎么做?因为它总是转到其他位置。将其更改为: int availableSeat = 0; string query = "select flight_no flight_seat from flight_info where flight_departure_time = @selectedFlight AND flight_from = @flightFrom AN
flight\u 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();
Equals()
方法用于比较目的(相等),而不是赋值。此外,通过将=
左侧的变量设置为右侧语句的结果,可以在C#中完成赋值
您需要将读取器
中的值强制转换为int
,因为它存储为对象
。您还应该检查null
值,因为该值来自数据库:
availableSeat = (int)reader["flight_seat"]);
availableSeat.Equals()
是一种比较而不是赋值。您需要指定该值。比如说
availableSeat = reader["flight_seat"].Equals(DBNull.Value)
? 0
: (int)reader["flight_seat"];
嗯,我觉得有个问题。它表示“抛出了System.invalidCaseException类型的exeption:\n指定的强制转换无效”,则您的
flight\u座位
不是int
值。您应该检查MySQL数据库中的底层类型。例如,如果是某种类型的varchar
,则需要将availableSeat
定义为字符串。
availableSeat = (int)reader["flight_seat"];