Can there be anyway I'm able to get this to database code any shorter ? It really works fine, but appears very verbose and repetitive. Wrapping a lot of it inside a method could be better I guess, but can there be other improvement that may be designed to shorten it ?

using (IDbConnection theConn = GetDatabaseConnection())
using (IDbCommand theCmd = theConn.CreateCommand())
    theCmd.CommandText = @"INSERT INTO table(one, two, three,four)
                                   VALUES (@one,@two,@three,@four)";
    var parameterOne = theCmd.CreateParameter();
    parameterOne.ParameterName = "@one";
    parameterOne.Value = "text";

    var parameterTwo = theCmd.CreateParameter();
    parameterTwo.ParameterName = "@two";
    parameterTwo.Value = "text";

    var parameterThree = theCmd.CreateParameter();
    parameterThree.ParameterName = "@three";
    parameterThree.Value = "text";

    var parameterFour = theCmd.CreateParameter();
    parameterFour.ParameterName = "@four";
    parameterFour.Value = "text";


If you do not want make use of a full OR/M why dont try Dapper?

If you're searching for an over-all change, I suggest while using Microsoft Entity Framework (or other OR/M). This can lead to a smaller amount code as you don't have to create this type of low-level code again. And delay pills work well with lots of, many databases. You are able to alter the underlying database with relatively small changes.

If you're just searching for something new during this method, well, then I'd go using the solution you already pointed out yourself. Proceed and refactor each repeating paragraph right into a method.


var parameterOne = theCmd.CreateParameter();
parameterOne.ParameterName = "@one";
parameterOne.Value = "text";

Into (for instance):

private void addParameter(IDbCommand theCmd, string paramName, string paramValue )
    var createdParameter = theCmd.CreateParameter();
    createdParameter.ParameterName = paramName;
    createdParameter.Value = paramValue ;

Also, when calling the mmethod, I favor determining some constants for that parameter names rather than writing the string every time again. This spares me of spelling mistakes.

Define them within an appropriate scope, like as class people, if you are using them in additional than this process (that we guess will sure be).