C
Carl Mercier
Hi,
I need to commit the changes to the database of a dataset containing
many (MANY) datatables. A few of them have an auto-increment column,
but I need to insert a specific value in the database (which is MS-SQL,
by the way).
My code is fine and I send a "SET IDENTITY_INSERT ON" to the database
when needed.
Since all the operations I need to perform are very basic and that I
have many different tables, I am using the CommandBuilder object. The
problem is that the Insert command does NOT include the auto-increment
field.
Is there a way to make the extra parameter is added to my InsertCommand?
MSDN and the books I have were not of any help.
Thank you very much!
Carl
PS: Here's the code...
Public Sub CommitTable(ByVal table As DataTable, ByVal transaction
As SqlTransaction, Optional ByVal allowIdentityInsert As Boolean = False)
Dim da As New SqlDataAdapter("SELECT * FROM " +
table.TableName, transaction.Connection)
da.SelectCommand.Transaction = transaction
Dim cmdBuilder As New SqlCommandBuilder(da)
Dim cmd As SqlCommand
cmdBuilder.QuotePrefix = "["
cmdBuilder.QuoteSuffix = "]"
da.InsertCommand = cmdBuilder.GetInsertCommand
da.UpdateCommand = cmdBuilder.GetUpdateCommand
da.DeleteCommand = cmdBuilder.GetDeleteCommand
cmd = New SqlCommand("DELETE FROM " + table.TableName,
transaction.Connection, transaction)
cmd.ExecuteNonQuery()
If allowIdentityInsert Then
cmd.CommandText = "SET IDENTITY_INSERT " + table.TableName
+ " ON"
cmd.ExecuteNonQuery()
End If
da.Update(table)
table.AcceptChanges()
If allowIdentityInsert Then
cmd.CommandText = "SET IDENTITY_INSERT " + table.TableName
+ " OFF"
cmd.ExecuteNonQuery()
End If
End Sub
I need to commit the changes to the database of a dataset containing
many (MANY) datatables. A few of them have an auto-increment column,
but I need to insert a specific value in the database (which is MS-SQL,
by the way).
My code is fine and I send a "SET IDENTITY_INSERT ON" to the database
when needed.
Since all the operations I need to perform are very basic and that I
have many different tables, I am using the CommandBuilder object. The
problem is that the Insert command does NOT include the auto-increment
field.
Is there a way to make the extra parameter is added to my InsertCommand?
MSDN and the books I have were not of any help.
Thank you very much!
Carl
PS: Here's the code...
Public Sub CommitTable(ByVal table As DataTable, ByVal transaction
As SqlTransaction, Optional ByVal allowIdentityInsert As Boolean = False)
Dim da As New SqlDataAdapter("SELECT * FROM " +
table.TableName, transaction.Connection)
da.SelectCommand.Transaction = transaction
Dim cmdBuilder As New SqlCommandBuilder(da)
Dim cmd As SqlCommand
cmdBuilder.QuotePrefix = "["
cmdBuilder.QuoteSuffix = "]"
da.InsertCommand = cmdBuilder.GetInsertCommand
da.UpdateCommand = cmdBuilder.GetUpdateCommand
da.DeleteCommand = cmdBuilder.GetDeleteCommand
cmd = New SqlCommand("DELETE FROM " + table.TableName,
transaction.Connection, transaction)
cmd.ExecuteNonQuery()
If allowIdentityInsert Then
cmd.CommandText = "SET IDENTITY_INSERT " + table.TableName
+ " ON"
cmd.ExecuteNonQuery()
End If
da.Update(table)
table.AcceptChanges()
If allowIdentityInsert Then
cmd.CommandText = "SET IDENTITY_INSERT " + table.TableName
+ " OFF"
cmd.ExecuteNonQuery()
End If
End Sub