Passing a table as input to dapper in dotnet core

When I tried to pass the table as a parameter to the stored procedure through Dapper, I came up with this SO response , which indicates that it is directly supported in Dapper.

However, the DataTable is not implemented in the point kernel , so this will not work.

Is there another easy way to pass a user / user defined table as a parameter to a stored procedure using Dapper?

+4
source share
2 answers

Now you will need to create a class that implements SqlMapper.IDynamicParameters for each type of table value.

( )

http://todothinkofname.net/2016/05/30/Dapper-with-Net-core/

  public class ParameterTvp : SqlMapper.IDynamicParameters
  {
      private readonly IEnumerable<string> _parameters;

      public ParameterTvp(IEnumerable<string> parameters)
      {
          _parameters = parameters;
      }

      public void AddParameters(IDbCommand command, SqlMapper.Identity identity)
      {
          var sqlCommand = (SqlCommand) command;
          sqlCommand.CommandType = CommandType.StoredProcedure;
          var items = new List<SqlDataRecord>();
          foreach (var param in _parameters)
          {
              var rec = new SqlDataRecord(new SqlMetaData("Parameter", SqlDbType.NVarChar, 100));
              rec.SetString(0, param);
              items.Add(rec);
          }

          var p = sqlCommand.Parameters.Add("@param", SqlDbType.Structured);
          p.Direction = ParameterDirection.Input;
          p.TypeName = "ParameterTableType";
          p.Value = items;
      }
  }

  using (var connection = new SqlConnection(connectionString))
  {
    var parameters = new ParameterTvp(new List<string>() { "XXXXXXXX" });
    connection.Execute("test", parameters);
  }
+3

IEnumerable<SqlDataRecord>, . System.Data, .

0

Source: https://habr.com/ru/post/1674560/


All Articles