0

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?

Soni
  • 19
  • 1
  • 10
  • Please before you mark something duplicated look into the question asked. The "duplicated" link is from 2010, and the Enterprise library doesn't include SQLDatabase class anymore! https://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.data.sql.sqldatabase.aspx – Soni Feb 18 '15 at 09:03
  • I just pulled down the latest source (6.0) and it DOES include that class. `Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase` – JasonMArcher Feb 18 '15 at 20:20
  • Thanks for it, I don't know how I missed it. If I have a change I mark your reply as an answer, even though I'm not too satisfied with this though. Microsoft clearly stopped supporting the SQLDatabase class based on their online documentation. I wonder if there is any kind of replacement to it. – Soni Feb 19 '15 at 09:09

0 Answers0