|
-
Mar 27th, 2004, 07:16 AM
#1
Thread Starter
Member
Mass Insert from a datatable to a database
I have a typed dataset set that I fill by scrapping an HTML page, and getting the information I need.
I fill my datatable with 600 odd rows, then call my dataadapter to update the Database. The problem is that it adds the first row, but not any other of the rows. I get this error message
"Column 'PriceID' is constrained to be unique. Value '6' is already present." The row that is added to the database is given the PriceID of 6.
The PriceID field is the primary key, Autoincrement field on a SQL Server database. My typed dataset is auto generated by VB.Net and I've not had problems with the mass inserts of data in the passed.
VB Code:
Dim dt As ds.PricesDataTable = ds.Ds1.Prices
For Each cp In HS.Prices
Dim dr As ds.PricesRow = dt.NewPricesRow
dr.ProductID = cp.ProductID
dr.CurrentPrice = cp.Price
dr.UpdateTime = CDate(Format(Now, "dd/MM/yyyy"))
dt.Rows.Add(dr)
Next
Try
ds.PricesDA.Update(ds.Ds1.Prices)
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
-
Mar 27th, 2004, 10:57 AM
#2
Thread Starter
Member
Also.........
When I alter the code to update to the database one row at a time as below.
VB Code:
Dim dt As ds.PricesDataTable = ds.Ds1.Prices
For Each cp In HS.Prices
Dim dr As ds.PricesRow = dt.NewPricesRow
dr.ProductID = cp.ProductID
dr.CurrentPrice = cp.Price
dr.UpdateTime = CDate(Format(Now, "dd/MM/yyyy"))
dt.Rows.Add(dr)
Try
ds.PricesDA.Update(ds.Ds1.Prices)
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Next
it works. This is not acceptable as this is inefficient. ADO.NET is supposed to allow bulk inserts.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|