Hemant Malpote
Hemant Malpote

Reputation: 891

How to update table with SqlAdapter.Update which doesn't have primary key

I am having SqlDataAdapter in which I am updating DataRow.

I got exception

Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information

The error goes if I put primary key in table.

But I don't want to put primary key in it.

var dataAdapter = new SqlDataAdapter();
var updatedRows = Getting some row ...
dataAdapter.Update(updatedRows);

Upvotes: 4

Views: 3379

Answers (1)

Moe Sisko
Moe Sisko

Reputation: 12005

Without a primary key, you won't be able to rely on the SqlCommandBuilder to generate the insert/update/delete commands for you.

If you still want to use the SqlDataAdapter to modify your data, you'll need to explicitly set the adapter's InsertCommand, UpdateCommand, DeleteCommand properties.

This may help to get you started : https://msdn.microsoft.com/en-us/library/ms971491.aspx

EDIT: without a primary key, you're going to have to be very careful when modifying data. e.g. when the UPDATE sql is executed, it will be much easier to accidently update more than 1 row of the table for the DataRow. Having a primary key will make things easier and more robust.

Upvotes: 1

Related Questions