Note that there are some explanatory texts on larger screens.

plurals
  1. POBest Practice: creating a data access class
    text
    copied!<p>I have the following class which I am using to read in large amounts of data from an Access database.</p> <pre><code>public class ConnectToAccess { private readonly string _connectionString; public ConnectToAccess(String connectionString) { _connectionString = connectionString; } public List&lt;String&gt; GetData(String sql) { var data = new List&lt;String&gt;(); using (var connection = new OleDbConnection(_connectionString)) { using (var command = connection.CreateCommand()) { command.CommandText = sql; command.CommandType = CommandType.Text; connection.Open(); using (var reader = command.ExecuteReader()) { if (reader != null &amp;&amp; reader.HasRows) while (reader.Read()) { data.Add(reader["First Name"] + " " + reader["Last Name"]); } } } } return data; } } </code></pre> <p>As is, this code is working and is successfully pulling data in from the database. However, I would like to enhance the GetData() method to make it more dynamic. I would like it to somehow return a list of anonymous objects, where each object has properties relating to the columns of the dataset returned.</p> <p>I've been coding in .Net for a while, but I'm still rather new at many concepts. I'm not quite sure how to create this list of anonymous objects that mirror the columns in the dataset most effectively. I'm also not sure what return type I would use in this case, I'm thinking maybe List. Then I suppose I would need to use reflection to pull the data out of those anonymous objects and transfer it into where it needs to go.</p> <p>If anyone can help me with any significant part of this puzzle, I would be most obliged.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload