Using dataadpater.Update(datatable) when the da is from a file

G

Guest

I want to update records from a csv file.

I do this in a button click:

//Create Adapters
da = new OdbcDataAdapter("Select * FROM test.csv", conn);

//Fill a data table
da.Fill(dt);

//Populate grid with file data
dataGrid1.DataSource = dt;

//Builder
csvBuilder = new OdbcCommandBuilder(da);

// Close the connection
conn.Close();

In another button click I delete some rows:

I loop thru the datarow and call dr.Delete() on the appropiate row and then
call dataGrid1.Refresh() which updates the grid with the remaining rows.

In another button click I want to update the test.csv file I pulled the data
from by doing this:

da.Update(dt);

But it throws this exception:

Additional information: Dynamic SQL generation for the DeleteCommand is not
supported against a SelectCommand that does not return any key column
information.

Does this mean my file needs to have a primary key. It does not, so how do I
update the file.

Thanks
 
W

W.G. Ryan eMVP

Yes, you're going to need a key in order to run an Update w/ CommandBUilder-
regardless of the source.
 
G

Guest

Is there any other way for me to update the csv file without having a key?

or is there a way for me to add a key to the csv file, and then use the
da.update(dt). The key can't be a unique key.

Paul
 
K

Kevin Yu [MSFT]

Hi Paul,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to update a csv file. If there
is any misunderstanding, please feel free to let me know.

Generally, when we need to update a database, we need a primary key column
to let the command builder generate update commands for you.

However, as far as I know, the Text driver for accessing .csv files is
readonly. It doesn't support updating, which means whether we have a
primary key or not, we cannot update the data source.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top