ADO.NET Question

R

Rohit

I have a function here which takes a dataset as an argument and attempts to
update a SQL CE table with the data in the dataset. However it is not
working. When I check the database, there are no records in the table. Can
somebody help? Here is the code:

public void sync_db(DataSet d, string table_name, bool open)
{
if (open) con.Open();
SqlCeDataAdapter da = new SqlCeDataAdapter("SELECT * FROM " +
table_name, con);
SqlCeCommandBuilder cmdb = new SqlCeCommandBuilder(da);
da.InsertCommand = cmdb.GetInsertCommand();
da.DeleteCommand = cmdb.GetDeleteCommand();
da.UpdateCommand = cmdb.GetUpdateCommand();
da.Update(d);
if (open) con.Close();
}
 
C

Cor Ligthert[MVP]

Rohit,

A DBDataAdapter updates data in datasets, datatables or datarows as long as
the rows in those have rowstates that are changed, inserted or updated.

As long as that is not the case, nothing is done.

(The setting of the Delete command from the commandbuilder is without sense,
that is not the way the commandbuilder is working, you can leave that it has
no effect).

Cor
 
R

Rohit

Thanks.

I got it to work by using a DataTableReader and writing an explicit loop to
iterate through the reader and explicitly insert the rows into the SQL CE
database.

Is there a way to take a given dataset and populate a database table with
the data in the dataset without writing an explicit loop in C#? Also, I want
the function sync_db to be generic so that it can be used for a variety of
different database tables.
 

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