【问题标题】:How to return a string array list in C#如何在 C# 中返回字符串数组列表
【发布时间】:2016-02-25 14:50:27
【问题描述】:

我是一个完全 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]

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

【问题讨论】:

  • 你忘记了c#-5.0标签。
  • “我正在使用 Visual Basic 编写一个 c# 应用程序,该应用程序连接到我创建的 MySQL 数据库。”你是说 Visual Studio 吗?
  • 为什么这些标签被标记为“C”?
  • return list 而不是 return list[0] 它将编译
  • 您是否要创建列表列表?

标签: c# mysql sql c#-5.0


【解决方案1】:

最好使用具有数据属性的类来保存您的信息。 (写在记事本里,如有错误请见谅)

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;}
}

【讨论】:

  • 谢谢,我喜欢这个解决方案并将尝试实施它,看起来它会简化一些事情。
猜你喜欢
  • 2010-11-05
  • 1970-01-01
  • 2021-08-03
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多