简体   繁体   中英

Inserting with Dapper.Contrib and DynamicParameters

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 .

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;
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM