2013-11-26 41 views
0

嗨,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); 
     } 
+0

你的表名爲:' _Animals'? –

+0

不再有一個表與國家在一個表,所以我知道我需要改變{0} _Animal部分,但我不知道如何。 – Beep

回答

1

不要把single quote超過selection_price值。 單引號僅需要varchar(字符串)類型

替換此:

MySqlCommand cd_price = new MySqlCommand(String.Format("SELECT Specie_Price FROM {0}_Animals WHERE Specie ='{1}'", ddlcountry.Text, selection_price), cs); 

有了這樣:

MySqlCommand cd_price = new MySqlCommand(String.Format("SELECT Specie_Price FROM {0}_Animals WHERE Specie ={1}", ddlcountry.Text, selection_price), cs); 

解決方案2: 你需要使用的SelectedItem財產。

替換這些2個查詢:

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); 

用以下2個查詢:

MySqlCommand cd_price = new MySqlCommand(String.Format("SELECT Specie_Price FROM Animals WHERE Specie ='{1}' and Country ='{0}'", ddlcountry.SelectedItem.ToString().Trim(), selection_price), cs); 
MySqlCommand cd_available = new MySqlCommand(String.Format("SELECT Stock FROM Animals WHERE Specie ='{1}' and Country ='{0}'", ddlcountry..SelectedItem.ToString().Trim(), selection_stock), cs); 
+0

這些「Turkey_Animals」表是什麼? – user2864740

+0

不是他們只是把一張桌子上的所有動物都做成了國家的一列,然後纔是UK_Animals,但現在英國和法國的第四個都在桌上動物 – Beep

+0

@Sudhakar Tillapudi它沒有工作 – Beep