3

I need to import millions of records in multiple sql server relational tables.

TableA(Aid(pk),Name,Color)----return id using scope identity 
TableB(Bid,Aid(fk),Name)---Here we need to insert Aid(pk) which we got using scocpe Identity

How I can do bulk insert of collection of millions of records using dapper in one single Insert statement

1 Answer 1

5

Dapper just wraps raw ADO.NET; raw ADO.NET doesn't offer a facility for this, therefore dapper does not. What you want is SqlBulkCopy. You could also use a table-valued-parameter, but this really feels like a SqlBulkCopy job.

In a pinch, you can use dapper here - Execute will unroll an IEnumerable<T> into a series of commands about T - but it will be lots of commands; and unless you explicitly enable async-pipelining, it will suffer from latency per-command (the pipelined mode avoids this, but it will still be n commands). But SqlBulkCopy will be much more efficient.

If the input data is an IEnumerable<T>, you might want to use ObjectReader from FastMember; for example:

IEnumerable<SomeType> data = ...
using(var bcp = new SqlBulkCopy(connection))
using(var reader = ObjectReader.Create(data, "Id", "Name", "Description"))
{
    bcp.DestinationTableName = "SomeTable";
    bcp.WriteToServer(reader);
}
Sign up to request clarification or add additional context in comments.

9 Comments

Can I do it using table value variable .if yes can you give me Example as per case defined in question.. How I can pass table type variable using dapper n insert data in multiple relational tables
@Rakesh yes, dapper can use TVPs - but you need to a: define a custom type on the server, b: populate your data into a DataTable of the right shape, and c: pass that DataTable in as a parameter to dapper, remembering to call dataTable.SetTypeName("YourCustomType") so that ADO.NET can connect things together
Thanks for your reply. But if you can provide some sample code about bulk insert in multiple tables as cade define in question using TVP parameter in store proc than it will be gr8 help for me.
@Rakesh conn.Execute(SQL, new { param = table }); - or perhaps more usefully: stackoverflow.com/questions/6232978/…
@Rakesh via the "with output" clause
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.