So, I recently asked a question about a class called SQLHelper, that was designed to cut down duplicated code whenever you connect to a SQL server, create a stored procedure command, etc.
Essentially, from this:
string connectionString = (string)
ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("INSERT_PERSON",connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@Name",SqlDbType.NVarChar,50));
command.Parameters["@Name"].Value = txtName.Text;
command.Parameters.Add(new SqlParameter("@Age",SqlDbType.NVarChar,10));
command.Parameters["@Age"].Value = txtAge.Text;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
...it would do this:
SqlHelper.ExecuteNonQuery(connection,"INSERT_PERSON",
new SqlParameter("@Name",txtName.Text), new SqlParameter("@Age",txtAge.Text));
Unfortunately the SQLHelper class vanished and is not available anymore in Enterprise library.
Would anybody know what could I use to avoid long code to set the connection up, create a store procedure, create parameters, set them, open the connection, execute the command and close the connection?
This would be required in each method that accesses that database and therefore is a considerable amount of duplicated code.
Is there way to avoid this?