converting resultset from OleDbDataReader into list

后端 未结 3 1415
[愿得一人]
[愿得一人] 2021-01-26 16:26

Consider a Winforms app connecting to a SQL Server 2008 database and running a SQL SELECT statement:

string myConnectionString = \"Provider=SQLOLEDB         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-26 17:10

    List of what? Do you have a class setup that has properties for name and finalconc? Saying you do, and it looks like this:

    public class QueryResult
    {
        public string Name { get; set; }
        //not sure what finalconc type would be, so here just using string
        public string FinalConc { get; set; }
    }
    

    Then you would do something like this:

    var queryResults = new List();
    using(var myReader = myCommand.ExecuteReader())
    {
        while(myReader.Read())
        {
            queryResults.Add(new QueryResult
                { 
                    Name = myReader.GetString(myReader.GetOrdinal("name")), 
                    FinalConc = myReader.GetString(myReader.GetOrdinal("finalconc"))
                });
        }
    }
    

提交回复
热议问题