我正在学习 C#,但我没有找到我的问题的信息。我在网上找到了这段代码。 这是一个类中的函数。但我不知道如何调用它、执行它。
我从来没有使用过这种类型的函数“List[] Select(string query)”
有人可以帮助我吗?
感谢您的回答,
class DBConnect
{
// ------- Code hided for Open/close mysql connection //
public List<string>[] Select(string query)
{
//string query = "SELECT id, aff_client, entreprise, ip_client FROM liste_machines ORDER BY entreprise ASC, aff_client ASC";
//Create a list to store the result
List<string>[] list = new List<string>[3];
list[0] = new List<string>();
list[1] = new List<string>();
list[2] = new List<string>();
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
list[0].Add(dataReader["id"] + "");
list[1].Add(dataReader["aff_client"] + "");
list[2].Add(dataReader["entreprise"] + "");
list[3].Add(dataReader["ip_client"] + "");
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
//return list to be displayed
return list;
}
else
{
return list;
}
}
}
最佳答案
对于您的问题,如何调用DBConnect类中的方法(函数)select,代码如下。
public class MyClass
{
public void Mymethod()
{
DBConnect db = new DBConnect();//Object creation
List<string>[] result = db.Select("MyQuery");//Method (Function) calling
foreach (List<string> item in result)//To access each List<string> from the array of List<string>
{ // Do something with item if required
foreach (string innerItem in item) // To access each string from the List<string>
{
//Do something with innerItem
}
}
}
}
对象名称位于 List<string>[] list = new List<string>[3];
可能是像 arrayData 而不是 list 之类的东西,就好像我们使用 list 作为对象名称一样,这可能会误导我们。
关于c# - 在C#类中调用List<string>[] Select(string query),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59378564/