i have two datatables that i am inserting into sql server 2008 using SqlBulkCopy
here they are:
QuickLabDump = new DataTable();
QuickLabDump.Columns.Add("Specimen ID", typeof(string));
QuickLabDump.Columns.Add("Client Key", typeof(int));
QuickLabDump.Columns.Add("Outcome", typeof(string));
QuickLabDump.Columns.Add("Medications", typeof(string));
QuickLabDump.Columns.Add("Date Collected", typeof(DateTime));
QuickLabDump.Columns.Add("Time Collected", typeof(TimeSpan));
QuickLabDump.Columns.Add("Date Entered", typeof(DateTime));
QuickLabDump.Columns.Add("Time Entered", typeof(TimeSpan));
QuickLabDump.Columns.Add("Date Completed", typeof(DateTime));
QuickLabDump.Columns.Add("Time Completed", typeof(TimeSpan));
QuickLabDump.Columns.Add("Test Date", typeof(DateTime));
QuickLabDump.Columns.Add("Test Time", typeof(TimeSpan));
QuickLabDump.Columns.Add("Practice Name", typeof(string));
QuickLabDump.Columns.Add("Practice Code", typeof(string));
QuickLabDump.Columns.Add("Client ID", typeof(string));
QuickLabDump.Columns.Add("Requesting Physician", typeof(string));
QuickLabDump.Columns.Add("Other Medications", typeof(string));
QuickLabDump.Columns.Add("Order Comments", typeof(string));
QuickLabDump.Columns.Add("Reference Number", typeof(string));
QuickLabDump.Columns.Add("Order Count", typeof(int));
TestResults = new DataTable();
TestResults.Columns.Add("TestName", typeof(String));
TestResults.Columns.Add("Result", typeof(Decimal));
TestResults.Columns.Add("NonNumericResult", typeof(String));
TestResults.Columns.Add("QuickLabDumpid", typeof(int));
on the database they are:
[Specimen ID] [varchar](50) NOT NULL,
[Client Key] [int] NOT NULL,
[Outcome] [varchar](50) NOT NULL,
[Medications] [varchar](max) NULL,
[Date Collected] [date] NOT NULL,
[Time Collected] [time](0) NOT NULL,
[Date Entered] [date] NOT NULL,
[Time Entered] [time](0) NOT NULL,
[Date Completed] [date] NOT NULL,
[Time Completed] [time](0) NOT NULL,
[Test Date] [date] NULL,
[Test Time] [time](0) NULL,
[Practice Name] [varchar](500) NOT NULL,
[Practice Code] [varchar](500) NOT NULL,
[Client ID] [varchar](500) NULL,
[Requesting Physician] [varchar](500) NULL,
[Other Medications] [varchar](max) NULL,
[Order Comments] [varchar](max) NULL,
[Reference Number] [varchar](500) NULL,
[Order Count] [int] NOT NULL,
[QuickLabDumpID] [int] IDENTITY(1,1) NOT NULL,
and
[TestName] [varchar](500) NOT NULL,
[Result] [decimal](18, 4) NULL,
[NonNumericResult] [varchar](100) NULL,
[QuickLabDumpID] [int] NOT NULL,
[TestResultsID] [int] IDENTITY(1,1) NOT NULL,
[QuickLabDumpID] [int] IDENTITY(1,1) NOT NULL
this one is autoincrement. each time a row is inserted it increments. it is also a foreign key for the same column on the other table.
question i have no problem inserting records into either table; however, i need to preserve the referential integrity so that the QuickLabDumpID
on the first table matches the QuickLabDumpID
on the other table.
i do NOT want to use @@identity
because i am inserting 100million rows and this will take days!! i have tried it.
I think that your best option is to SET IDENTITY_INSERT QuickLabDump ON
before the bulk copy and then turn it off afterwards.
You will then need to generate ids for each of the rows to be inserted unless they are already present in the incoming data set.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.