如何检索实际的列值。 [英] how to retrieve actual column value.?

查看:86
本文介绍了如何检索实际的列值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的工作中,我有一张桌子



In my work I have a table

reservation_available(seat_id, seat_type, flight_id, available)





其中flight_id是外键,seat_type连续有两个值(经济,商业)。要检索seat_id并且我写道:





where flight_id is a foreign key and seat_type has two value(Economic, Business) consecutively. To retrieve the seat_id and available I wrote:

using (SqlCommand command = new SqlCommand("SELECT seat_id, seat_type, available FROM reservation_available WHERE seat_type = 'Economic' AND reservation_available.flight_id = '" + flight_id + "' ", connection))
               {

                   string s_ID = seat_id.ToString();
                   string e_avail = EconomicAvailable.ToString();
                   string st = seat_type;


                   SqlDataReader myReader = command.ExecuteReader();

                   while (myReader.Read())
                   {
                       s_ID = myReader[0].ToString();
                       st = myReader[1].ToString();
                       e_avail = myReader[2].ToString();

                   }
                   int sId = Convert.ToInt32(s_ID);
                   int eAvail = Convert.ToInt32(e_avail);

                   myReader.Close();
                   set2(sId, eAvail, st);
               }





以及seat_type =Business的类似代码。



但检索到的数据仅显示seat_type =''Business'的seat_id。但我想为所选的seat_type分别设置seat_id。



需要帮助



and similar code for the seat_type = "Business".

But retrieved data only shows the "seat_id" of seat_type = ''Business''. But I want respective seat_id for selected seat_type.

Help needed

推荐答案

有两个可能的原因这个:



1)您的查询根本没有返回任何记录。在这种情况下,将使用您在声明参数变量时设置的默认值调用 set2

2)您的查询返回的次数超过一个记录。在这种情况下,将调用 set2 ,仅返回最后一条此类记录。



您想要的第一件事要做的是使用调试器并查看正在发生的事情。但是在string s_ID = ...行有一个断点,然后单步执行以查看返回的内容。
There are two possible reasons for this:

1) Your query is returning no records at all. In which case, set2 will be called with the default values you set when you declared the parameter variables.
2) Your query is returning more than one record. In which case, set2 will be called with the last such record returned only.

The first thing you want to do is use a debugger and look at what is happening. but a breakpoint at the "string s_ID = ..." line and step through to see what is being returned.


需要考虑的一些要点:

SQL查询可能返回0,1或更多行。在0行的情况下,你将变量初始化为一些默认值 - 不确定你是否理解在 SqlDataReader myReader ... 之前你在这三行中做了什么?

然后在循环时正确使用 - 但是每次循环都会覆盖数据。您应该从数据中创建一个合适的对象,并将该对象添加到List。

而不是字符串的奇怪转换,您可以从datareader获取int值(如果您的表设置正确...)。

那个 set2 函数有什么作用?它是否使用从第二个查询中收到的值覆盖您从第一个查询收到的值?
Some points to consider:
the SQL query may return 0, 1, or more rows. In case of 0 rows, you initialised the variables to some default values - not sure if you understand what you do in those three lines before SqlDataReader myReader...?
Then you correctly use a while loop - but you overwrite the data with every loop. You should create an appropriate object from the data, and add that object to a List.
Instead of the odd conversions from strings, you can get int values from the datareader (if your table is set up correctly...).
What does that set2 function do? Does it overwrite the values you received from the first query with the values you received from the second query?


这篇关于如何检索实际的列值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆