2017-03-23 175 views
0

我有一個MySQL左連接查詢。這個查詢運行正常。但是左邊的加入表沒有獲得價值。請通過以下查詢。我工作在C#左連接Mysql查詢導致錯誤

void getstuinfo() 
     { 
      try 
      { 
       MySqlCommand com = new MySqlCommand("select stumaster.stuname,"+ 
       "stumaster.lname,"+ 
       "stumaster.fname,"+ 
       "stumaster.mname,"+ 
       "stumaster.fa_calty,"+ 
       "stumaster.sex,"+ 
       "castmaster.castdisp,"+ 
       "stumaster.castcode,"+ 
       "stumaster.nwscs "+ 
       "from stumaster "+ 
       " left join castmaster on stumaster.castcode = castmaster.castcode "+ 
       " where grno = " + Convert.ToInt32(textBox1.Text).ToString(), con_db.con); 
       MySqlDataReader dr1 = com.ExecuteReader(); 
       if (dr1.HasRows) 
       { 
        while (dr1.Read()) 
        { 
         textBox2.Text = (dr1[("stuname")].ToString()); 
         textBox4.Text = (dr1["lname"]).ToString(); 
         textBox5.Text = (dr1["fname"]).ToString(); 
         textBox6.Text = (dr1["mname"]).ToString(); 
         comboBox5.Text = (dr1["fa_calty"]).ToString(); 
         comboBox1.Text = (dr1["castdisp"]).ToString(); 
         textBox7.Text = (dr1["castcode"]).ToString(); 
         textBox9.Text = (dr1["nwscs"]).ToString(); 
         string wsex = (dr1["sex"]).ToString(); 

         if (wsex == "M") 
         { 
          radioButton1.Checked = true; 
         } 
         else 
         { 
          radioButton2.Checked = true; 
         } 
        } 
        dr1.Close(); 
       } 
       else 
       { 
        MessageBox.Show("Not a Valid G.R.No.", " Student Information "); 
        dr1.Close(); 
        textBox1.Focus(); 
        return; 
       } 
      } 
      catch (FormatException) 
      { 
       MessageBox.Show("Date is Invalid "); 
      } 
     } 
+0

給表名可以提供輸出? – jarg

回答

0

你有grno

stumaster.grno = conditiion 
+0

感謝您的重播。我已糾正它,但沒有結果。意味着它沒有顯示「castmaster.castdisp」值休息查詢執行正常。 –

+0

如果所有的值都填充然後檢查枯萎castdisp是否有價值或不知道mysql的sql bt我們使用isnull(castdisp,0)意味着如果值爲null,那麼用0替換該列的結果 –

+0

檢查枯萎的列有一個值或不是因爲c#會做什麼,它會將null更改爲空字符串,這可能是因爲您正在使用左外部連接而不是僅獲取該列中的數據的原因,它還將包括NULL值 –