Phenix Development
RSS 2.0

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Sign In
Wednesday, January 31, 2007

Working with some older code I found to make a connection and iterate through took a bit of repetitous code.  

    string sql = "SELECT * FROM Version WHERE [Field1] = @Field1 AND [Field2] = @Field2";
    using (SqlConnection connection = new SqlConnection(connectionStr))
    {
        connection.Open();
        using (SqlCommand command = connection.CreateCommand())
        {
            command.CommandText = sql;
            command.CommandType = CommandType.Text;
            command.Parameters.AddWithValue("@Field1", "Value1");
            command.Parameters.AddWithValue("@Field2", "Value2");
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    //Read the Fields
                }
            }
        }
    }

 

As stated by a good friend: "Less Code, works better".  So it was decided (by me) to change this to a simple Sql Reader.  This new class implements IDisposable and creates the SqlConnection and SqlCommand on creation then disposes them when the instance is disposed.

    public class SimpleSqlReader: IDisposable
    {
        SqlConnection connection;
        SqlCommand command;
        SqlDataReader reader;

        public SimpleSqlReader(string connectionString, string commandText, bool isStoredProcedure)
        {           
            this.connection = new SqlConnection(connectionString);
            this.connection.Open();

            this.command = this.connection.CreateCommand();
            this.command.CommandText = commandText;
            if (isStoredProcedure)
                this.command.CommandType = CommandType.StoredProcedure;
            else
                this.command.CommandType = CommandType.Text;
        }

        public SimpleSqlReader AddParam(string parameterName, object value)
        {
            this.command.Parameters.AddWithValue(parameterName, value);           
            return this;
        }

        public void Execute()
        {
            reader = command.ExecuteReader();
        }

        public SqlDataReader Reader
        {
            get { return reader; }
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (reader != null && !reader.IsClosed)
            {
                reader.Close();
                reader.Dispose();
            }
            command.Dispose();
            connection.Close();
            connection.Dispose();
        }

        #endregion
    }

OK so it may not be less code now, but over the course of the project it will be.

    using (SimpleSqlReader reader = new SimpleSqlReader(connectionStr, sql, false))
    {
        reader.AddParam("@Field1", "Value1")
            .AddParam("@Field2", "Value2")
            .Execute();
        while (reader.Reader.Read())
        {
            //Read the Fields
        }
    }

Posted on Wednesday, January 31, 2007 3:16:48 PM (Eastern Standard Time, UTC-05:00)  #   Comments [2]