Inserting with Dapper.Contrib and DynamicParameters

c# dapper wpf

Question

QUESTION :

Is there a clever way to dynamically add parameters to an object and insert them in to a SQL DB?

preferably using Dapper


I'm writing a WPF app where (amongest other things) I'm collecting questionnaire data. I'm using Dapper to map c# objects to SQL statements.

My problem is that I don't want to hard code all of the column names as object properties since I have a lot of them!

So I wanted to use Dapper's DynamicParameters to dynamically generate objects that Dapper can insert into the database by using the Dapper.Contrib Insert method.

I made an abstract class like this:

public abstract class IDbRecord : DynamicParameters
    {
        [Key]
        public string H4Id { get; set; }
    }

and added parameters in another method using the DynamicParameters.Add method.

When my parameters have been added to my IDbRecord derived object I try to insert it.

This results is the Insert medthod trying to insert the public properties of DynamicParameters and not the content of the private parameters Dictonary. Which makes sense when looking at the Readme of Dapper.Contrib. I was just hoping that they had implemented Insert() to grab the parameters Dictonary when the object was derived from DynamicParameters.

Popular Answer

I enden up iterating through my objects and building a SQL INSERT statement with a StringBuilder. Not very elegant. But it works

if (!hasData)
{
   var parameterList = new StringBuilder(null);
   var valuesList = new StringBuilder(null);
   var insertSql = new StringBuilder(null);
   parameterList.AppendFormat("Id, ");
   valuesList.Append(Id + ", ");
   foreach (var questionBase in answerList)
   {
       if (string.IsNullOrEmpty(questionBase.VariableName))
       {
           throw new ArgumentException("Question " + questionBase.QuestionNumber +
                                       " does not have a VariableName");
       }
       if (!string.IsNullOrEmpty(questionBase.VariableName) && questionBase.Answer != null)
       {
           // insert keys (variable names)
           parameterList.AppendFormat("{0}", questionBase.VariableName);

           if (questionBase.QuestionNumber != answerList.Last().QuestionNumber)
           {
               parameterList.Append(", ");
           }

           // insert values
           valuesList.AppendFormat("{0}", questionBase.Answer);
           if (questionBase.VariableName != answerList.Last().VariableName)
               valuesList.Append(", ");
       }
   }

   try
   {
       insertSql.AppendFormat("INSERT INTO {0} ({1}) VALUES ({2})", tableName, parameterList, valuesList);
       Connect(ConnectionHelper.DevConnString,
           c => c.Execute(insertSql.ToString()));
       return true;
   }
   catch (Exception e)
   {
       return false;
   }

}


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why