Fill an array (or arraylist) from SqlDataReader

前端 未结 7 566
滥情空心
滥情空心 2020-12-01 05:36

Is there a way to fill an array via a SqlDataReader (or any other C# ADO.NET object) without looping through all the items? I have a query that is returning a single column

7条回答
  •  爱一瞬间的悲伤
    2020-12-01 06:10

    You have to loop, but there are projects that can make it simpler. Also, try not to use ArrayList, use List instead.

    You can checkout FluentAdo for one: http://fluentado.codeplex.com

        public IList List()
        {
            var list = new FluentCommand("SELECT ID, UserName, Password FROM UserAccount")
                .SetMap(reader => new UserAccount
                {
                    ID = reader.GetInt("ID"),
                    Password = reader.GetString("Password"),
                    UserName = reader.GetString("UserName"),
                })
                .AsList();
    
            return list;
        }
    

提交回复
热议问题