Instead of telling you to use an ORM, I'm going to show you how I do it (typed from memory) (and I'm going to be downmodded and flamebaited and who knows what else):
I see you do what I do, which is namely:
SqlDataReader myReader = null;
SqlCommand SQLcmd = new SqlCommand();
SqlConnection SQLConn = new SqlConnection();
String SQLStatement = "Select this 'nd That";
SQLConn.ConnectionString = SQLConnectionString;
SQLConn.Open();
SQLcmd.Connection = SQLConn;
SQLcmd.CommandText = SQLStatement;
myReader = SQLcmd.ExecuteReader();
while (myReader.Read())
{
//Do some awesome
}
SQLConn.Close();
}
And so I do something similar. Notice that you're creating new SqlCommand, new SqlConnection, and you're using a common SqlConnectionString (which you need to be able to reload from a central place, yah?) so I do this:
public class Global{
public string ConnectionString {get;set;}
public Global(){
ConnectionString = //load from wherever, this allows me to reload it later, via API? ;)
}
public SqlCommandFactory(string sprocName, SqlConnection con){
return new SqlCommand{
CommandText = sprocName,
Connection = con,
CommandTimeout = 0,
ConnectionTimeout = 0,
CommandType = StoredProcedure
};
}
}
//in my other class that uses this code:
public List<string> /* for simplicity sake, strigns */ GetListOfStringsFromDatabase(){
List<string> returnValue = new List<string>();
// use of using ensures that I don't forget to clean something up here!!!!
using ( SqlConnection con = new SqlConnection(Global.ConnectionString) ) {
SqlCommand cmd = Global.SqlCommandFactory("mysproc", con);
cmd.Parameters.Add( "@param1", SqlDbType.VarChar ).Value = "somestring";
con.Open();
using ( SqlDataReader reader = cmd.ExecuteReader() ) {
while (reader.Read()) {
returnResult.Add( reader[0].ToString() );
}
}
}
return returnValue;
}
But the ORM would probably be better. Just not something that we feel works well for our situation.