【发布时间】:2016-12-17 00:59:58
【问题描述】:
我有 2 个盒子,我想从 sql 中填充第一个,并根据第一个填充第二个(也查询 sql)
public void Country()
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT CountryName FROM Country", conn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr = dt.NewRow();
dr["CountryName"] = "";
dt.Rows.InsertAt(dr, 0);
this.country.DisplayMember = "CountryName";
this.country.ValueMember = "CountryName";
this.country.DataSource = dt;
}
}
}
}
public void City()
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT CityName FROM City", conn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr = dt.NewRow();
dr["CityName"] = "";
dt.Rows.InsertAt(dr, 0);
this.country.DisplayMember = "CityName";
this.country.ValueMember = "CityName";
this.country.DataSource = dt;
}
}
}
}
T A B L E S
国家
PK 国名
城市
PK 城市名
FK 国家/地区名称
我相信我应该更改 City 的 SqlCommand,也许是 WHERE 语句?因此,如果选择 Country1,则在 City 框中仅显示 City1,如果 Country2 则显示 City2,依此类推。 我该如何排序,有人知道吗?谢谢
【问题讨论】: