我正在学习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(字符串查询),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59378564/

10-13 07:32
查看更多