S
Sean Finkel
Hello all,
I am currently tasked with developing a home-grown replication system for our product.
I have it completed and am working out the various bugs. One of the issues we are having deals with inserting data into tables that have identity columns.
Currently, I am reseeding the autonumber on each insert. As you can imagine, this results in a ton of context switches over to the database.
I have tried to modify the code to take the insert command that SqlCommandBuilder generates and modify it to include the identity column in both the column list and the values list.
I then also add a new parameter and set the name/source column.
I also set identity_insert to on for the table. No matter what I do, I always get the error from SQL Server that I must explicitly provide the value for the identity column.
Am I missing a line of code to associate the new parameter with the insert command?
The parameter is defined, and the insert command's commandText is correct.
Any help would be much appreciated!
Here is a sample app I was using, the test table has two columns -
insertID - int - identity - primary key - not null
insertText - varchar(50)
-------------------( snip )-----------------
Dim lconTest As New SqlConnection
Try
lconTest.ConnectionString = "<removed>"
lconTest.Open()
' now attempt to insert a new record
Dim lcmdInsert As New SqlCommand
lcmdInsert.CommandText = "set identity_insert insertTest on"
lcmdInsert.Connection = lconTest
lcmdInsert.ExecuteScalar()
Dim ldtTest As New DataTable("insertTest")
Dim ldaTest As New SqlDataAdapter("select insertID, insertText from insertTest", lconTest)
ldaTest.Fill(ldtTest)
Dim ldrNewRow As DataRow = ldtTest.NewRow
ldrNewRow("insertID") = 1
ldrNewRow("insertText") = "test"
ldtTest.Rows.Add(ldrNewRow)
Dim lcbdTest As New SqlCommandBuilder(ldaTest)
ldaTest.InsertCommand = lcbdTest.GetInsertCommand(True)
' manually modify the insert command to include the identity column
Dim lparmIdentity As New SqlParameter
lparmIdentity.SourceColumn = "insertID"
lparmIdentity.ParameterName = "@insertID"
lparmIdentity.SqlDbType = SqlDbType.Int
ldaTest.InsertCommand.CommandText = ldaTest.InsertCommand.CommandText.Replace("[insertTest] (", "[insertTest] ([insertID], ").Replace("VALUES (", "VALUES (@insertID, ")
ldaTest.InsertCommand.Parameters.Add(lparmIdentity)
ldaTest.Update(ldtTest)
lcmdInsert.CommandText = "set identity_insert insertTest off"
lcmdInsert.ExecuteScalar()
TextBox1.Text = "Success"
Catch ex As Exception
TextBox1.Text = "=========================" & vbNewLine & ex.Message & vbNewLine & "=========================" & vbNewLine & ex.StackTrace
End Try
-------------------( /snip )------------------
I am currently tasked with developing a home-grown replication system for our product.
I have it completed and am working out the various bugs. One of the issues we are having deals with inserting data into tables that have identity columns.
Currently, I am reseeding the autonumber on each insert. As you can imagine, this results in a ton of context switches over to the database.
I have tried to modify the code to take the insert command that SqlCommandBuilder generates and modify it to include the identity column in both the column list and the values list.
I then also add a new parameter and set the name/source column.
I also set identity_insert to on for the table. No matter what I do, I always get the error from SQL Server that I must explicitly provide the value for the identity column.
Am I missing a line of code to associate the new parameter with the insert command?
The parameter is defined, and the insert command's commandText is correct.
Any help would be much appreciated!
Here is a sample app I was using, the test table has two columns -
insertID - int - identity - primary key - not null
insertText - varchar(50)
-------------------( snip )-----------------
Dim lconTest As New SqlConnection
Try
lconTest.ConnectionString = "<removed>"
lconTest.Open()
' now attempt to insert a new record
Dim lcmdInsert As New SqlCommand
lcmdInsert.CommandText = "set identity_insert insertTest on"
lcmdInsert.Connection = lconTest
lcmdInsert.ExecuteScalar()
Dim ldtTest As New DataTable("insertTest")
Dim ldaTest As New SqlDataAdapter("select insertID, insertText from insertTest", lconTest)
ldaTest.Fill(ldtTest)
Dim ldrNewRow As DataRow = ldtTest.NewRow
ldrNewRow("insertID") = 1
ldrNewRow("insertText") = "test"
ldtTest.Rows.Add(ldrNewRow)
Dim lcbdTest As New SqlCommandBuilder(ldaTest)
ldaTest.InsertCommand = lcbdTest.GetInsertCommand(True)
' manually modify the insert command to include the identity column
Dim lparmIdentity As New SqlParameter
lparmIdentity.SourceColumn = "insertID"
lparmIdentity.ParameterName = "@insertID"
lparmIdentity.SqlDbType = SqlDbType.Int
ldaTest.InsertCommand.CommandText = ldaTest.InsertCommand.CommandText.Replace("[insertTest] (", "[insertTest] ([insertID], ").Replace("VALUES (", "VALUES (@insertID, ")
ldaTest.InsertCommand.Parameters.Add(lparmIdentity)
ldaTest.Update(ldtTest)
lcmdInsert.CommandText = "set identity_insert insertTest off"
lcmdInsert.ExecuteScalar()
TextBox1.Text = "Success"
Catch ex As Exception
TextBox1.Text = "=========================" & vbNewLine & ex.Message & vbNewLine & "=========================" & vbNewLine & ex.StackTrace
End Try
-------------------( /snip )------------------