G
Greg
I have done this with some success. Updates work generically, deletes work
generically, all using DataAdapter and passing DataSet Changes to one
method.
My problem is getting Inserts to work generically. The issue here in Output
parameters. How do I flag a datatable or something else coming in with the
DataSet that when the Datatable contains an Added Row and its looping
through the insert code, building input parameters it also needs to build an
output param and here are all the values needed for the new param
constructor?
Currently, I build all the Insert\Update command parameters in the
RowUpdating event like this:
Dim col As DataColumn
Dim drw As DataRow = e.Row
Dim myParam As SqlParameter
Select Case e.StatementType
Case StatementType.Update
For Each col In drw.Table.Columns
Try
' check the original row against the current row
to see if they are different
If Not (drw(col,
DataRowVersion.Current).Equals(drw(col, DataRowVersion.Original))) Then
' build a parameter for the changed column
myParam = New SqlParameter("@" &
col.ColumnName, drw.Item(col).ToString)
e.Command.Parameters.Add(myParam)
End If
Catch ex As Exception
Throw New Exception("Exception occurred in " &
className & ".OnRowUpdating Event. Column: " & col.ColumnName & ". " _
& ex.message)
End Try
Next
Case StatementType.Delete
For Each colName As String In PrimaryKeys
myParam = New SqlParameter("@" & colName, drw.Item(col,
DataRowVersion.Original).ToString)
e.Command.Parameters.Add(myParam)
Next
Case StatementType.Insert
For Each col In drw.Table.Columns
Try
' build output params
If col.ColumnName = "names_id" Then
myParam = New SqlParameter("@newid",
SqlDbType.VarChar, _
6,
ParameterDirection.Output, _
False, CType(0,
Byte), CType(0, Byte), _
"names_id",
DataRowVersion.Current, _
Nothing)
e.Command.Parameters.Add(myParam)
ElseIf col.ColumnName = "control" Then
myParam = New SqlParameter("@ncontrol",
SqlDbType.VarChar, _
6,
ParameterDirection.Output, _
False, CType(0,
Byte), CType(0, Byte), _
"control",
DataRowVersion.Current, _
Nothing)
e.Command.Parameters.Add(myParam)
Else
myParam = New SqlParameter("@" &
col.ColumnName.Remove(0, col.ColumnName.IndexOf("_") + 1),
drw.Item(col).ToString)
e.Command.Parameters.Add(myParam)
End If
Catch ex As Exception
End Try
Next
End Select
So, as you can see, the Insert has hard code for the output params. I want
to get rid of this so I can send any DataTable insert through this
eventhandler. Currently, it only supports 2 of my Datatables, one with
'control' as the PK and one with 'names_id' as the PK.
generically, all using DataAdapter and passing DataSet Changes to one
method.
My problem is getting Inserts to work generically. The issue here in Output
parameters. How do I flag a datatable or something else coming in with the
DataSet that when the Datatable contains an Added Row and its looping
through the insert code, building input parameters it also needs to build an
output param and here are all the values needed for the new param
constructor?
Currently, I build all the Insert\Update command parameters in the
RowUpdating event like this:
Dim col As DataColumn
Dim drw As DataRow = e.Row
Dim myParam As SqlParameter
Select Case e.StatementType
Case StatementType.Update
For Each col In drw.Table.Columns
Try
' check the original row against the current row
to see if they are different
If Not (drw(col,
DataRowVersion.Current).Equals(drw(col, DataRowVersion.Original))) Then
' build a parameter for the changed column
myParam = New SqlParameter("@" &
col.ColumnName, drw.Item(col).ToString)
e.Command.Parameters.Add(myParam)
End If
Catch ex As Exception
Throw New Exception("Exception occurred in " &
className & ".OnRowUpdating Event. Column: " & col.ColumnName & ". " _
& ex.message)
End Try
Next
Case StatementType.Delete
For Each colName As String In PrimaryKeys
myParam = New SqlParameter("@" & colName, drw.Item(col,
DataRowVersion.Original).ToString)
e.Command.Parameters.Add(myParam)
Next
Case StatementType.Insert
For Each col In drw.Table.Columns
Try
' build output params
If col.ColumnName = "names_id" Then
myParam = New SqlParameter("@newid",
SqlDbType.VarChar, _
6,
ParameterDirection.Output, _
False, CType(0,
Byte), CType(0, Byte), _
"names_id",
DataRowVersion.Current, _
Nothing)
e.Command.Parameters.Add(myParam)
ElseIf col.ColumnName = "control" Then
myParam = New SqlParameter("@ncontrol",
SqlDbType.VarChar, _
6,
ParameterDirection.Output, _
False, CType(0,
Byte), CType(0, Byte), _
"control",
DataRowVersion.Current, _
Nothing)
e.Command.Parameters.Add(myParam)
Else
myParam = New SqlParameter("@" &
col.ColumnName.Remove(0, col.ColumnName.IndexOf("_") + 1),
drw.Item(col).ToString)
e.Command.Parameters.Add(myParam)
End If
Catch ex As Exception
End Try
Next
End Select
So, as you can see, the Insert has hard code for the output params. I want
to get rid of this so I can send any DataTable insert through this
eventhandler. Currently, it only supports 2 of my Datatables, one with
'control' as the PK and one with 'names_id' as the PK.