如何存储 sqlDataReader 的值?并进行比较?

发布于 2024-12-11 08:11:50 字数 1009 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-12-18 08:11:50

availableSeat.Equals() 是比较而不是赋值。您需要分配该值。例如

availableSeat = (int)reader["flight_seat"];

availableSeat.Equals() is a comparison not an assignment. You would need to assign the value. For example

availableSeat = (int)reader["flight_seat"];
命硬 2024-12-18 08:11:50

将其更改为:

availableSeat = (int)reader["flight_seat"]);

Equals() 方法用于比较目的(相等)而不是赋值。此外,在 C# 中,通过将 = 左侧的变量设置为右侧语句的结果来完成赋值。

您需要将 reader 中的值转换为 int,因为它存储为 object。您还应该检查 null 值,因为它来自数据库:

availableSeat = reader["flight_seat"].Equals(DBNull.Value)
                    ? 0
                    : (int)reader["flight_seat"];

Change it to:

availableSeat = (int)reader["flight_seat"]);

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 an int because it is stored as an object. You should also check for a null value since this is coming from a database:

availableSeat = reader["flight_seat"].Equals(DBNull.Value)
                    ? 0
                    : (int)reader["flight_seat"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文