Skip to main content
edited tags
Link
Eldros
  • 459
  • 1
  • 4
  • 18
Small formatting correction and added the C# tag
Source Link
Brian Reichle
  • 2k
  • 1
  • 19
  • 26
public bool spInsertFeedback(string name, string subject, string message)
        {
            int rows = 0;

            SqlConnection connection = new SqlConnection(cnnString);
            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand("[dbo].[spInsertFeedback]", connection);
                command.CommandType = CommandType.StoredProcedure;

                // params
                SqlParameter messageName = new SqlParameter("@name", SqlDbType.VarChar);
                messageName.Value = name;
                SqlParameter messageSubject = new SqlParameter("@subject", SqlDbType.VarChar);
                messageSubject.Value = subject;
                SqlParameter messageText = new SqlParameter("@message", SqlDbType.VarChar);
                messageText.Value = message;

                // add params
                command.Parameters.Add(messageName);
                command.Parameters.Add(messageSubject);
                command.Parameters.Add(messageText);

                rows = command.ExecuteNonQuery();
            }
            catch
            {
                return (rows != 0);
            }
            finally
            {
                connection.Close();
            }

            return (rows != 0);
        }
public bool spInsertFeedback(string name, string subject, string message)
        {
            int rows = 0;

            SqlConnection connection = new SqlConnection(cnnString);
            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand("[dbo].[spInsertFeedback]", connection);
                command.CommandType = CommandType.StoredProcedure;

                // params
                SqlParameter messageName = new SqlParameter("@name", SqlDbType.VarChar);
                messageName.Value = name;
                SqlParameter messageSubject = new SqlParameter("@subject", SqlDbType.VarChar);
                messageSubject.Value = subject;
                SqlParameter messageText = new SqlParameter("@message", SqlDbType.VarChar);
                messageText.Value = message;

                // add params
                command.Parameters.Add(messageName);
                command.Parameters.Add(messageSubject);
                command.Parameters.Add(messageText);

                rows = command.ExecuteNonQuery();
            }
            catch
            {
                return (rows != 0);
            }
            finally
            {
                connection.Close();
            }

            return (rows != 0);
        }
public bool spInsertFeedback(string name, string subject, string message)
{
    int rows = 0;

    SqlConnection connection = new SqlConnection(cnnString);
    try
    {
        connection.Open();
        SqlCommand command = new SqlCommand("[dbo].[spInsertFeedback]", connection);
        command.CommandType = CommandType.StoredProcedure;

        // params
        SqlParameter messageName = new SqlParameter("@name", SqlDbType.VarChar);
        messageName.Value = name;
        SqlParameter messageSubject = new SqlParameter("@subject", SqlDbType.VarChar);
        messageSubject.Value = subject;
        SqlParameter messageText = new SqlParameter("@message", SqlDbType.VarChar);
        messageText.Value = message;

        // add params
        command.Parameters.Add(messageName);
        command.Parameters.Add(messageSubject);
        command.Parameters.Add(messageText);

        rows = command.ExecuteNonQuery();
    }
    catch
    {
        return (rows != 0);
    }
    finally
    {
        connection.Close();
    }

    return (rows != 0);
}
Tweeted twitter.com/#!/StackCodeReview/status/73086211190829056
Source Link
slandau
  • 173
  • 1
  • 5

Data Access Layer Code

Wondering if I could do this a little bit better. Currently, my DAL has a blank constructor that sets the connection string from the web.config.

private string cnnString;

public DAL()
{
    cnnString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
}

Then each method in here is mapped directly to a stored procedure, and they ALL look something like this:

public bool spInsertFeedback(string name, string subject, string message)
        {
            int rows = 0;

            SqlConnection connection = new SqlConnection(cnnString);
            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand("[dbo].[spInsertFeedback]", connection);
                command.CommandType = CommandType.StoredProcedure;

                // params
                SqlParameter messageName = new SqlParameter("@name", SqlDbType.VarChar);
                messageName.Value = name;
                SqlParameter messageSubject = new SqlParameter("@subject", SqlDbType.VarChar);
                messageSubject.Value = subject;
                SqlParameter messageText = new SqlParameter("@message", SqlDbType.VarChar);
                messageText.Value = message;

                // add params
                command.Parameters.Add(messageName);
                command.Parameters.Add(messageSubject);
                command.Parameters.Add(messageText);

                rows = command.ExecuteNonQuery();
            }
            catch
            {
                return (rows != 0);
            }
            finally
            {
                connection.Close();
            }

            return (rows != 0);
        }

Obviously some return a DataSet or a list of an object or something, where as this one just returns whether or not any rows were affected.

However, each method does things this way, and I just feel like I have a lot of redundant code, and I'd like to simplify it. From the other classes, I'm calling the DAL like this:

DAL dal = new DAL();
bool success = dal.spInsertFeedback(name, subject, message);
return Json(success);

Thanks guys.