【发布时间】: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]它将编译 -
您是否要创建列表列表?