0

我有一个填充组合框的查询,并根据组合框的选择将值传递给另一个查询。

我到目前为止的代码是:

    MySqlCommand SelectCommandAirport = new MySqlCommand("SELECT AirportName, DataTable  FROM    AirportList;", myConnAirport);
MySqlDataReader myAirportReader;
    myConnAirport.Open();

    myAirportReader = SelectCommandAirport.ExecuteReader();
    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(new DataColumn("AirportName"));
    dataTable.Columns.Add(new DataColumn("DataTable"));
    comboBox1.DataSource = dataTable;
    comboBox1.ValueMember = "AirportName";
    comboBox1.DisplayMember = "DataTable";
try
    {
       while (myAirportReader.Read())
       {
          DataRow row = dataTable.NewRow();
          row["AirportName"] = myAirportReader[1];
          row["DataTable"] = myAirportReader[0];
          dataTable.Rows.Add(row);

          SelectAirport.sAirport = comboBox1.SelectedItem.ToString();
       }
     }
 catch (Exception ex)
     {
     MessageBox.Show(ex.Message);
     }

然后我有一个第二个查询,它应该使用组合框选择的结果作为第二个查询的变量。

    string airportid;
    airportid = SelectAirport.sAirport;
    MySqlCommand SelectCommand = new MySqlCommand("SELECT ArriveDepart, Flight, FlightDate, ScheduledTime, IATALookup, Terminal, RemarkswithTime, Logo   FROM '" + airportid + "' WHERE Flight = '" + this.flightno_txt.Text + "';", myConnFlight);

当我运行代码时,我收到一条 SQL 错误,上面写着“System.Data.DataRowView WHERE Flight =”。

因为这不起作用,我可以假设代码是正确的。谁能看到我哪里出错了。

提前谢谢了。

DCJ

当我运行代码时,我得到一个显示内容的 SQL 错误

4

1 回答 1

0

这称为级联下拉列表 - 最好的例子是“选择你的国家,然后选择你的省份”

ASP.Net 现在提供了一项功能,使这比以前容易得多。如果您不能使用此功能,那么您已经在做的事情非常正确 - 选择一个,然后提交到下一个查询并从中加载框。

这是来自 ASP.Net AJAX 库... http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx

此外,您在代码示例中还有额外的工作。当您运行 ExecuteReader 命令时,它会返回一个 DataReader 对象,该对象可以直接绑定到您的控件,如下所示...

MySqlCommand SelectCommandAirport = new MySqlCommand("SELECT AirportName, DataTable  FROM    AirportList;", myConnAirport);

MySqlDataReader myAirportReader;
myConnAirport.Open();

myAirportReader = SelectCommandAirport.ExecuteReader();
comboBox1.DataSource = dataTable;
comboBox1.ValueMember = "AirportName";
comboBox1.DisplayMember = "DataTable";
comboBox1.DataBind();

这往往比创建项目并将它们复制到您的组合框中更好。

我怀疑您的 SQL 错误是因为您将 AirportName 视为表名,而不是数据值。因此,您的查询应该更像这样(我喜欢使用 String.Format 更清楚地指示字符串中的参数位置):

String.Format("SELECT ArriveDepart, Flight, FlightDate, ScheduledTime, IATALookup,
    Terminal, RemarkswithTime, Logo  
    FROM AIRPORT_TABLE WHERE AirportName = '{0}'
         AND Flight = {1}", airportId, flightno_txt.Text)

如果这确实是它应该工作的方式,你需要用你的表的实际名称替换 AIRPORT_TABLE。只是一个猜测。

于 2013-04-26T17:39:47.033 回答