converting resultset from OleDbDataReader into list

后端 未结 3 1411
[愿得一人]
[愿得一人] 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:18

    Assume you have defined a class that is something like

    class MyData
    {
        public string Name {get; set;}
        public int FinalConc {get; set;} // or whatever the type should be
    }
    

    You would iterate through the results of your query to load a list.

    List list = new List();
    while (myReader.Read())
    {
        MyData data = new MyData();
        data.Name = (string)myReader["name"];
        data.FinalConc = (int)myReader["finalconc"]; // or whatever the type should be
        list.Add(data);
    }
    
    // work with the list
    

    If you just need one of the given fields, you can forego the class definition and simply have a List, where T is the type of whatever field you want to hold.

提交回复
热议问题