ado.net queries – There has got to be a better way.

I'm writing an application that does a LOT of database queries using ADO.net. I have a lot of code like this:

string query = "SELECT r1, r2, r3 FROM MyTable;"; using (SqlConnection sqlcon = new SqlConnection(MyConnectionString)) using (SqlCommand sqlcmd = new SqlCommand(query, sqlcon)) { sqlcon.Open(); using (SqlDataReader sdr = sqlcmd.ExecuteReader()) { if (sdr.HasRows) { while (sdr.Read()) { // Do something with my data here... } } } } 

There probably is a way to create a generic query function so I can do something like:

results = MySqlQueryFunction("SELECT r1, r2, r3 FROM MyTable;"); 

Does anyone have any ideas on this? Any help would be greatly appreciated.

by Gojs2015 via /r/csharp

Leave a Reply