Best way to query database in Visual C#

Best way to query database in C#
Well, this is actually not an official way but my personal choice. In this instance, I will use an access database as an example.
While querying the database is a long process in C#, it becomes simplified once you use object orientation. First of all, here’s the conventional way of querying the database:

Now, if you have a lot of queries to run, then this would be really painful process to do again and again. Hence, it is better to put the process in a method like this:

The method takes in the select query string and returns the data reader which can then be used to extract the data. To make this more efficient, make a class and put the above method as static. Also, make the connectionString a global variable so that it if you need to change it, then you only have to change it in one place. In the end, the code would look like this:

Later, to access the method, you’ll just have to do:

OleDbDataReader reader = QueryClass.runSelectQuery("SELECT * FROM Customers");

Comment below if you have any queries regarding the code.