2014-04-03 30 views
-2

我是一個完整的100%初學者,所以請提前原諒我的無知!如何返回C#中的字符串數組列表#

有人可以幫我從我的c#數據庫查詢返回這個列表數組嗎?我正在使用Visual Studio編寫一個c#應用程序,它連接到我創建的MySQL數據庫。

這是我遇到的問題代碼。 (我已經搜索很多次,但我只是不理解的東西,請大家幫忙!)

public List<string>[] clientList() // Select from customer table in MySQL db 
    { 

     string query = "SELECT " + thisItem + " FROM " + thisTable; 
     // Create list to store the results result 
     List<string>[] list = new List<string>[9]; 

      list[0] = new List<string>(); 
      list[1] = new List<string>(); 
      list[2] = new List<string>(); 
      list[3] = new List<string>(); 
      list[4] = new List<string>(); 
      list[5] = new List<string>(); 
      list[6] = new List<string>(); 
      list[7] = new List<string>(); 
      list[8] = new List<string>(); 

     if (this.OpenConnection() == true) // Open connection 
      { 
       MySqlCommand cmd = new MySqlCommand(query, connection); // Create Command 
       MySqlDataReader dataReader = cmd.ExecuteReader(); // Create data reader and Execute command 

       // Read data and store in list 
       // Request data from SQL db 
       Console.WriteLine("AQUIRRING DATA FROM SQL SERVER...."); 
       while (dataReader.Read()) 
       { 
        if (thisItem == "*" && (thisTable == "CUSTOMER") || (thisTable == "customer")) 
        { 
         list[0].Add(dataReader["ID"] + ""); 
         list[1].Add(dataReader["NAME"] + ""); 
         list[2].Add(dataReader["COMPANY"] + ""); 
         list[3].Add(dataReader["PLANT"] + ""); 
         list[4].Add(dataReader["CITY"] + ""); 
         list[5].Add(dataReader["STATE"] + ""); 
         list[6].Add(dataReader["CREATED"] + ""); 
         list[7].Add(dataReader["VIEWED"] + ""); 
         list[8].Add(dataReader["MODIFIED"] + ""); 

        } 
        else 
        { 
         list[0].Add(dataReader[thisItem] + ""); 
        } 
       }  

       dataReader.Close(); 

       this.CloseConnection(); //Close Connection 
       return list[0];   // Return list to be used 
      } 
      else 
      { 
       return list[0];   // Return non-updated list if connection failed 
      } 

當我調用此函數(如下:)

public static List<string>[] queryList = new List<string>[9]; 
queryList = sqlConn.clientList(); // Return client name list 

然而,當我這樣做,而不是獲取MySQL數據庫中的字符串數據,我反而回:

System.Collections.Generic.List`1[System.String] 

有人可以幫助這個,最有可能的,非常簡單的問題?

+4

您忘記了'c#-5.0'標籤。 –

+2

「我正在編寫一個使用visual basic的c#應用程序,它連接到我創建的MySQL數據庫。」你的意思是Visual Studio? –

+0

爲什麼thos標記爲「C」? –

回答

2

最好使用具有數據屬性的類來保存您的信息。 (寫在記事本中,所以請原諒任何錯誤)

public class Whatever 
{ 
    public List<Company> clientList() // Select from customer table in MySQL db 
    { 
     string query = "SELECT " + thisItem + " FROM " + thisTable; 
     // Create list to store the results result 
     List<Company> list = new List<Company>(); 
     if (this.OpenConnection() == true) // Open connection 
     { 
      MySqlCommand cmd = new MySqlCommand(query, connection); // Create Command 
      MySqlDataReader dataReader = cmd.ExecuteReader(); // Create data reader and Execute command 

      // Read data and store in list 
      // Request data from SQL db 
      Console.WriteLine("AQUIRRING DATA FROM SQL SERVER...."); 
      while (dataReader.Read()) 
      { 
       if (thisItem == "*" && (thisTable == "CUSTOMER") || (thisTable == "customer")) 
       { 
        Company newCompany = new Company(); 

        newCompany.ID = dataReader["ID"].ToString(); 
        newCompany.Name = dataReader["NAME"].ToString(); 
        ... 
        ... 
        ... (You get the jist of it.) 

        list.add(newCompany); 
       } 

      }  

      dataReader.Close(); 

      this.CloseConnection(); //Close Connection 
      return list;  // Return list to be used 
     } 
     return list; 
    } 
} 

public class Company 
{ 
    ID {get;set;} 
    NAME {get;set;} 
    COMPANY {get;set;} 
    PLANT {get;set;} 
    CITY {get;set;} 
    STATE {get;set;} 
    CREATED {get;set;} 
    VIEWED {get;set;} 
    MODIFIED {get;set;} 
} 
+0

謝謝,我喜歡這個解決方案,並試圖實現它,它看起來會簡化一些東西。 –

相關問題