嗨,C#的人還是很新的,我很迷茫。MySQL查詢和C#中的多個級聯#
我有兩個下拉列表ddlcountry
(國家)和DdPetPist
(物種)國家選擇填充物種列表與物種在所選擇的國家可用。
代碼波紋管
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MySqlCommand cd2 = new MySqlCommand("SELECT DISTINCT(Country) FROM Animals", cs);
cs.Open();
MySqlDataReader ddlCountry = cd2.ExecuteReader();
ddlcountry.DataSource = ddlCountry;
ddlcountry.DataValueField = "Country";
ddlcountry.DataTextField = "Country";
ddlcountry.DataBind();
cs.Close();
cs.Dispose();
}
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlcountry.Text != string.Empty)
{
MySqlCommand cd = new MySqlCommand(string.Format("SELECT * FROM Animals WHERE Country ='{0}'", ddlcountry.Text), cs);
cs.Open();
MySqlDataReader ddlSpecie = cd.ExecuteReader();
DdPetPist.DataSource = ddlSpecie;
DdPetPist.DataValueField = "Specie";
DdPetPist.DataTextField = "Specie";
DdPetPist.DataBind();
cs.Close();
cs.Dispose();
}
}
這工作得很好,我很喜歡它,雖然我是在保護它免受SQL注入的進程。
問題
我知道想打印出來的信息分爲兩個標籤有兩個查詢的,這個我有問題。到目前爲止,我的標籤將打印出寵物價格和庫存量等信息。但我似乎無法使查詢與不同的國家選擇進行調整。 我已經在這裏呆了幾天了,任何幫助都會很棒,因爲我對C#非常陌生並且仍在學習。
標籤碼和查詢的(不與不同的國家選擇改變)
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string selection_price = DdPetPist.SelectedValue;
string selection_stock = DdPetPist.SelectedValue;
string petPrice = string.Empty;
string available = string.Empty;
MySqlCommand cd_price = new MySqlCommand(String.Format("SELECT Specie_Price FROM Animals WHERE Specie ='{1}'", ddlcountry.Text, selection_price), cs);
MySqlCommand cd_available = new MySqlCommand(String.Format("SELECT Stock FROM Animals WHERE Specie ='{1}'", ddlcountry.Text, selection_stock), cs);
cs.Open();
petPrice = Convert.ToString(cd_price.ExecuteScalar());
available = Convert.ToString(cd_available.ExecuteScalar());
cs.Close();
PetPrice.Text = String.Format("Minimum Donation For A {0} Is £{1}.", selection_price, petPrice);
Availble.Text = String.Format("{0}'s Avalible {1} In Your Country.", selection_stock, available);
}
你的表名爲:' _Animals'? –
不再有一個表與國家在一個表,所以我知道我需要改變{0} _Animal部分,但我不知道如何。 – Beep