2015-08-23 39 views
1

我需要知道如何獲得所有的#2的clientID,所以我可以得到他們分配給的程序...到目前爲止,我的代碼只獲得#2的第一個clientID在它...從sqldatabase表獲得公共ID號#

int progs; 
string Command = @"select * from clientprogram where clientProgClientID = @clientID;"; 
using (MySqlConnection mConnection = new MySqlConnection(mycon)) 
{ 
    mConnection.Open(); 
    using (MySqlCommand cmd2 = new MySqlCommand(Command, mConnection)) 
    { 
      cmd2.Parameters.Add(new MySqlParameter("@clientID", lblcID.Text)); 
      using (MySqlDataReader reader = cmd2.ExecuteReader()) 
      { 
       if (reader.Read()) 
       { 
        progs = (int)reader["clientProgramID"]; 
        cmbProgram.Items.Add(progs); 
       } 
      } 
    } 
    mConnection.Close(); 
} 
+0

應該'if(reader.Read())'是一個循環嗎? – amdixon

回答

1

當然,它只會得到第一個結果,因爲你只讀了一次。使用while(reader.Read())代替if(reader.Read())

while(reader.Read()) 
{ 
     progs = (int)reader["clientProgramID"]; 
     cmbProgram.Items.Add(progs); 
}       
+0

哈哈哈是的,我沒有注意到...謝謝。 – jgf

1

變化

if (reader.Read()) 

while(reader.Read()) 

側面說明;不使用select *時,你只需要一列數據,使用select Coumnname from yourtable

3

變化

if (reader.Read()) 

while (reader.Read()) 

這似乎應該是在一個循環中添加記錄遞歸而不是僅僅第一個

0
int progs; 
string Command = @"select * from clientprogram where clientProgClientID =  @clientID;"; 
using (MySqlConnection mConnection = new MySqlConnection(mycon)) 
{ 
mConnection.Open(); 
using (MySqlCommand cmd2 = new MySqlCommand(Command, mConnection)) 
    { 
     cmd2.Parameters.Add(new MySqlParameter("@clientID", lblcID.Text)); 
     using (MySqlDataReader reader = cmd2.ExecuteReader()) 
     { 
      while (reader.Read()) // CHANGE TO THIS 
      { 
       progs = (int)reader["clientProgramID"]; 
       cmbProgram.Items.Add(progs); 
      } 
     } 
} 
mConnection.Close(); 

}