Getting SqlBulkCopy to show up as sql in MiniProfiler
Asked Answered
S

1

6

I'm using MiniProfiler to profile my sql commands.

One issue I'm dealing with now is repeated INSERT statements generated by linq.

I've converted them into a SqlBulkCopy command, however now it doesn't appear to record it in the sql view in MiniProfiler.

Would there even be an associated command string for a SqlBulkCopy?

Is it possible to get the bulk copy to appear in the list of sql commands?

Can I at least make it counted in the % sql bit?


I'm aware I could use MiniProfiler.Current.Step("Doing Bulk Copy") but that wouldn't count as SQL, and wouldn't show in the listing with any detail.


Current code below:

public static void BulkInsertAll<T>(this DataContext dc, IEnumerable<T> entities)
{
    var conn = (dc.Connection as ProfiledDbConnection).InnerConnection as SqlConnection;
    conn.Open();

    Type t = typeof(T);

    var tableAttribute = (TableAttribute)t.GetCustomAttributes(
        typeof(TableAttribute), false).Single();
    var bulkCopy = new SqlBulkCopy(conn)
    {
        DestinationTableName = tableAttribute.Name
    };

    //....

    bulkCopy.WriteToServer(table);
}
Shang answered 16/9, 2013 at 14:52 Comment(7)
The data is being sent as special TDS packets, but I don't know whether the BULK INSERT is being initiated using SQL. It shows up in SQL Profiler as SQL, but that may be fake.Intercolumniation
I'm working on MiniProfiler 3.0 that adds a more generic CustomTiming class, allowing you to profile the "bulk copy" and have it show up in the UI the same way "sql" does. Once it's out, I'll answer with an example.Barbur
@JarrodDixon: Great! Any idea on timescale (beyond 6-8 weeks)? :)Shang
This weekend, I hope... going to start testing it internally today.Barbur
@JarrodDixon: Cool, looks like I timed my question pretty well!Shang
@JarrodDixon: Sorry to pester, but is there any news on this? I see it's getting updated on Git, but doesn't appear as an update on NuGet.Shang
We're still dog-fooding it internally before releasing it on nuget; feel free to build it from git in the interim.Barbur
W
5

You should be able to use CustomTimings to profile these. These are included in the new v3 version that is now available on nuget.

You can see some example usages of CustomTiming in the sample project where this is used to record http and redis events.

An example of how you could use it with SqlBulkCopy:

string sql = GetBulkCopySql(); // what should show up for the SqlBulkCopy event?
using (MiniProfiler.Current.CustomTiming("SqlBulkCopy", sql)) 
{
  RunSqlBulkCopy(); // run the actual SqlBulkCopy operation
}
Wilek answered 15/2, 2014 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.