c# - 在C#类中调用List<string>[] Select(string query)

标签 c# mysql visual-studio

我正在学习 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/

相关文章:

c# - ASP.NET 页面 'Page_Load' 在母版页的 'Page_Load' 事件之前触发?

c# - Rx 处置订阅

c# - 可以使用 ConcurrentDictionary 进行一对多映射吗?

mysql - 在 SQL 中比较同一个表中的两行是否相等

mysql - 将行转换为mysql数据库中的列

visual-studio - 在 "D"驱动器上安装 Visual Studio 2008 Sp1

c# - PeekLock 模式下的 Peek() 方法是否锁定服务总线消息?

mysql - mongodb性能相关参数

c# - 在 Visual Studio 中获取 C# 类的所有接口(interface)

visual-studio - 是否可以在 Razor 模板中设置断点?