This isn’t related to a particular issue BUT is a question regarding “best practise”.
For a while now, when I need to get data straight from the database I’ve been using the following method – I was wondering if there’s a faster method which I don’t know about?
DataTable results = new DataTable();
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Name"]))
{
connection.Open();
using (SqlCommand command = new SqlCommand("StoredProcedureName",connection))
{
command.CommandType = CommandType.StoredProcedure;
/*Optionally set command.Parameters here*/
results.Load(command.ExecuteReader());
}
}
/*Do something useful with the results*/
There are indeed various ways of reading data;
DataTableis quite a complex beast (with support for a number of complex scenarios – referential integrity, constraints, computed values, on-the-fly extra columns, indexing, filtering, etc). In a lot of cases you don’t need all that; you just want the data. To do that, a simple object model can be more efficient, both in memory and performance. You could write your own code aroundIDataReader, but that is a solved problem, with a range of tools that do that for you. For example, you could do that via dapper with just:which then very efficiently populates a
List<SomeTypeOfRow>, without all theDataTableoverheads. Additionally, if you are dealing with very large volumes of data, you can dothis in a fully streaming way, so you don’t need to buffer 2M rows in memory:
For completeness, I should explain
optionalParameters; if you wanted to pass@id=1,@name="abc", that would be just:which is, I think you’ll agree, a pretty concise way of describing the parameters. This parameter is entirely optional, and can be omitted if no parameters are required.
As an added bonus, it means you get strong-typing for free, i.e.
rather than having to talk about
row["Id"],row["Name"]etc.