S
SqlJunkies User
Hi,
I should be able to use the following code to load data from a datatable via ado.net into sql server. I want to keep the code generic so I can run it against any table. The error I get relates to primary key not being set. But I know it is because when the row is first inserted I get error msg that primary key is violated
Dim myconnection As SqlConnection
Dim myda As SqlDataAdapter
Dim DS2 As New DataSet()
myconnection = New SqlConnection("server=blah;uid=;pwd=;database=")
myda = New SqlDataAdapter("Select * from FTVORGN2 ", myconnection)
Dim cmdBuilder As SqlCommandBuilder = _
New SqlCommandBuilder(myda)
myda.FillSchema(DS2, SchemaType.Mapped) 'this loads PK and field structure ok
myda.Fill(DS2) 'this table is actually empty so no data is brought in
DS2.Tables(0).PrimaryKey = New DataColumn() {DS2.Tables(0).Columns("FTVORGN_ORGN_CODE"), _
DS2.Tables(0).Columns("FTVORGN_EFF_DATE"), _
DS2.Tables(0).Columns("FTVORGN_NCHG_DATE")}
Dim I As Integer
Dim FieldCount As Integer
Dim x As Integer
Dim dr As DataRow
FieldCount = dsFTVORGN.Tables(0).Columns.Count
For I = 0 To dsFTVORGN.Tables(0).Rows.Count - 1
On Error Resume Next 'need this cause can't insert null into not null fields
'first add empty row
dr = DS2.Tables(0).NewRow
DS2.Tables(0).Rows.Add(dr)
'now populate
For x = 0 To FieldCount - 1
DS2.Tables(0).Rows(I).Item(x) = dsFTVORGN.Tables(0).Rows(I).Item(x)
Next
'lets try to write data from other dataset into this ds2
Next
' Update database with modified data
On Error GoTo 0
'at this point I have confirmed data is loaded into dataset just need to write it to datasource error received is:
"Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information."
myda.UpdateCommand = cmdBuilder.GetUpdateCommand()
myda.Update(DS2.Tables(0))
End Sub
Any ideas how to make this work???
I should be able to use the following code to load data from a datatable via ado.net into sql server. I want to keep the code generic so I can run it against any table. The error I get relates to primary key not being set. But I know it is because when the row is first inserted I get error msg that primary key is violated
Dim myconnection As SqlConnection
Dim myda As SqlDataAdapter
Dim DS2 As New DataSet()
myconnection = New SqlConnection("server=blah;uid=;pwd=;database=")
myda = New SqlDataAdapter("Select * from FTVORGN2 ", myconnection)
Dim cmdBuilder As SqlCommandBuilder = _
New SqlCommandBuilder(myda)
myda.FillSchema(DS2, SchemaType.Mapped) 'this loads PK and field structure ok
myda.Fill(DS2) 'this table is actually empty so no data is brought in
DS2.Tables(0).PrimaryKey = New DataColumn() {DS2.Tables(0).Columns("FTVORGN_ORGN_CODE"), _
DS2.Tables(0).Columns("FTVORGN_EFF_DATE"), _
DS2.Tables(0).Columns("FTVORGN_NCHG_DATE")}
Dim I As Integer
Dim FieldCount As Integer
Dim x As Integer
Dim dr As DataRow
FieldCount = dsFTVORGN.Tables(0).Columns.Count
For I = 0 To dsFTVORGN.Tables(0).Rows.Count - 1
On Error Resume Next 'need this cause can't insert null into not null fields
'first add empty row
dr = DS2.Tables(0).NewRow
DS2.Tables(0).Rows.Add(dr)
'now populate
For x = 0 To FieldCount - 1
DS2.Tables(0).Rows(I).Item(x) = dsFTVORGN.Tables(0).Rows(I).Item(x)
Next
'lets try to write data from other dataset into this ds2
Next
' Update database with modified data
On Error GoTo 0
'at this point I have confirmed data is loaded into dataset just need to write it to datasource error received is:
"Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information."
myda.UpdateCommand = cmdBuilder.GetUpdateCommand()
myda.Update(DS2.Tables(0))
End Sub
Any ideas how to make this work???