How to use SQL bulk copy to retrieve the IDs of added data records database identity sqlbulkcopy sql-server


I have an ADO.NET DataTable with about 100,000 records. In this table there is a column xyID which has no values in it, because the column is an auto-generated IDENTITY in my SQL Server database.

I need to retrieve the generated IDs for other processes. I am looking for a way to bulk copy this DataTable into the SQL Server database, and within the same "step" to "fill" my DataTable with the generated IDs.

How can I retrieve the identity values of records inserted into a table using the SqlBulkCopy class?

3/24/2010 2:29:41 PM

Popular Answer

I'm currently doing something like this:

DataTable objects = new DataTable();
DataColumn keyColumn = new DataColumn("name", typeof(string));
DataColumn versionColumn = new DataColumn("version", typeof(int));
versionColumn.DefaultValue = iVersionID;


foreach (KeyValuePair<string, NamedObject> kvp in Directory)
    NamedObject o = kvp.Value;
    DataRow row = objects.NewRow();
    row[0] = o.Name;

using (SqlBulkCopy updater = new SqlBulkCopy(conn,
        SqlBulkCopyOptions.TableLock | SqlBulkCopyOptions.UseInternalTransaction, null))
    updater.DestinationTableName = "object_table";

string sQuery = @"SELECT id, name FROM object_table WHERE version = @ver";
using (SqlCommand command = new SqlCommand(sQuery, conn))
    SqlParameter version = new SqlParameter("@ver", SqlDbType.Int, 4);
    version.Value = versionID;

    command.CommandTimeout = 600;

    using (SqlDataReader reader = command.ExecuteReader())
        while (reader.Read())
            string key = (string)reader[1];

            NamedObject item = Directory[key];
            item.ID = (int)reader[0];

Note that our data design enables filtering for all our new objects by using the version ID; every row we're adding will have the same version id, and we've previously removed any rows in the database that already had this version id.

However, my select query is currently timing out in ExecuteReader, even with that 10-minute window. So this is not our final solution.

6/11/2010 6:41:47 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow