5

I want to do a bulk insert. A is an ID and B is a list of IDs. My insert statement looks like this but it is wrong. How do I rewrite it to work? The only solution I can think of is using a foreach loop outside the statement

.Execute(@"insert into MyTable(a,b) select @a, @b", new {a, b})
  • It looks like there might be a bulk insert int some extension methods: https://github.com/tmsmith/Dapper-Extensions/issues/18 – Pete Garafano Jan 18 '14 at 21:07

1 Answers1

8

Try this:

var abs = b.Select(id => new { a, b = id });
int numInserted = connection
    .Execute(@"insert into MyTable(a,b) VALUES(@a, @b)", abs);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Here I was thinking I should rewrite the statement. That works. Accepted –  Jan 18 '14 at 21:25
  • On second thought the for loop might be cleaner but I'll leave it as is –  Jan 18 '14 at 21:26