如何从一列中检索数据是指同一个表中的另外两列?

时间:2022-09-21 15:12:51

I have a table name

我有一个表名

flight_info (flight_id, flight_no, depart_from, destination, depart_time, arrive_time)

flight_info(flight_id,flight_no,depart_from,destination,depart_time,arrival_time)

Now I want to retrieve only

现在我只想检索

flight_id on the basis of column depart_from and destination

flight_id基于列depart_from和目的地

I wrote the SQL as following:

我写了如下SQL:

string str = "SELECT fligth_id FROM flight_info WHERE  depart_from = @depart AND destination = @destination"

While running, it's showing an error pointing @depart and @destination. Can you help me, How can I specify those scalar variable.?

在运行时,它显示指向@depart和@destination的错误。你能帮我吗?我怎样才能指定那些标量变量?

I tried it..

我尝试过这个..

SqlDataReader myReader;
string depart = myReader["depart_from"].ToString();
string destination = myReader["destination"].ToString();

But, it's not working. Help needed.

但是,它不起作用。需要帮助。

1 个解决方案

#1


1  

You are using parameters in your query, but you didn't specify them.

您在查询中使用参数,但未指定参数。

Try like this;

试试这样;

using (SqlConnection connection = new SqlConnection(connectionString))
{
   connection.Open();
   using (SqlCommand command = new SqlCommand("SELECT fligth_id FROM flight_info WHERE  depart_from = @depart AND destination = @destination", connection))
   {
        command.Parameters.Add("@depart", depart);
        command.Parameters.Add("@destination", destination);
        SqlDataReader myReader = command.ExecuteReader();
        while (myReader.Read())
        {
           int fligth_id = reader.GetInt32(0);
        }
   }
}

Since your query returns only fligth_id column, you can't access depart_from and destination columns with that way. SqlDataReader reads database rows one-by-one.

由于您的查询仅返回fligth_id列,因此您无法使用该方式访问depart_from和destination列。 SqlDataReader逐个读取数据库行。

If you return other columns in your query like;

如果您在查询中返回其他列,则;

SELECT fligth_id, depart_from, destination

You can read them the same way like;

你可以用同样的方式阅读它们;

while (myReader.Read())
{
    int fligth_id = reader.GetInt32(0);
    string depart_from = reader.GetString(1);
    string destination = reader.GetString(2);
}

#1


1  

You are using parameters in your query, but you didn't specify them.

您在查询中使用参数,但未指定参数。

Try like this;

试试这样;

using (SqlConnection connection = new SqlConnection(connectionString))
{
   connection.Open();
   using (SqlCommand command = new SqlCommand("SELECT fligth_id FROM flight_info WHERE  depart_from = @depart AND destination = @destination", connection))
   {
        command.Parameters.Add("@depart", depart);
        command.Parameters.Add("@destination", destination);
        SqlDataReader myReader = command.ExecuteReader();
        while (myReader.Read())
        {
           int fligth_id = reader.GetInt32(0);
        }
   }
}

Since your query returns only fligth_id column, you can't access depart_from and destination columns with that way. SqlDataReader reads database rows one-by-one.

由于您的查询仅返回fligth_id列,因此您无法使用该方式访问depart_from和destination列。 SqlDataReader逐个读取数据库行。

If you return other columns in your query like;

如果您在查询中返回其他列,则;

SELECT fligth_id, depart_from, destination

You can read them the same way like;

你可以用同样的方式阅读它们;

while (myReader.Read())
{
    int fligth_id = reader.GetInt32(0);
    string depart_from = reader.GetString(1);
    string destination = reader.GetString(2);
}