Reputation: 145
I'm pretty new to .NET so this might be basic stuff.
Here is my code:
adapter1 = New SqlDataAdapter("SELECT top 0 * FROM [aTable]", connection)
cb = New SqlCommandBuilder(adapter1)
adapter1.InsertCommand = cb.GetInsertCommand
adapter1.Update(dataSet1, "[aTable]")
Basically, what I want to do is an Insert into this table. However, some of the values that I want to insert can already exist in the table aTable on other rows except for 1 column that differs. However I do NOT want to do an update any rows but INSERT a new row, always. But as of now I always get an UPDATE on old rows that has similar values.
How do I decide which column should be in there WHERE-clause in the .update()-function? Or is there any better method to do this?
Thanks!
Upvotes: 0
Views: 35
Reputation: 17377
Yes, you can specify the look of the update command, using SqlDataAdapter.UpdateCommand. Look at the example:
' Create the UpdateCommand.'
Dim command As SqlCommand command = New SqlCommand( _
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " & _
"WHERE CustomerID = @oldCustomerID", connection)
' Add the parameters for the UpdateCommand.'
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
Dim parameter As SqlParameter = command.Parameters.Add( _
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID")
parameter.SourceVersion = DataRowVersion.Original
adapter.UpdateCommand = command
Upvotes: 1