如何在C#中返回字符串数组列表

时间:2014-04-03 12:40:34

标签: c# mysql sql c#-5.0

我是一个完全100%的初学者,所以请提前原谅我的无知!!

有人可以帮我从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]

有人可以帮助解决这个非常简单的问题吗?

1 个答案:

答案 0 :(得分: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;}
}