2011-05-29 71 views
1

我正在爲此苦苦掙扎。我想從數據庫中獲取某個值等於某行中某個值的ID列表。此調用可能會返回多個ID。我想將值存儲在列表或列表中的數組中,但是我發現這很麻煩。我的代碼了這裏:C#MySql在C#中存儲多個數據庫行

 string strConnection = ConfigurationSettings.AppSettings["ConnectionString"]; 
     MySqlConnection connection = new MySqlConnection(strConnection); 
     MySqlCommand command = connection.CreateCommand(); 
     MySqlDataReader reader; 
     command.CommandText = "SELECT idprojects FROM `test`.`projects` WHERE application_layers = " + applicationTiers + ""; 
     connection.Open(); 

     reader = command.ExecuteReader(); 

任何幫助,將不勝感激

回答

2
 string strConnection = ConfigurationSettings.AppSettings["ConnectionString"]; 
    MySqlConnection connection = new MySqlConnection(strConnection); 

    List<string> array = new List<string>(); 

     using (MySqlCommand cmd = new MySqlCommand("SELECT idprojects FROM `test`.`projects` WHERE application_layers = " + applicationTiers, connection)) 
     { 
      try 
      { 
       using (MySqlDataReader Reader = cmd.ExecuteReader()) 
       { 
        while (Reader.Read()) 
        { 
         array.Add(Reader["idprojects"].ToString()); 
        } 
       } 
      } 

      catch (Exception ex) 
      { 
       throw; 
      } 
     } 

     string[] ret= array.ToArray(); 
相關問題