Updating the database with the dataset data????

  • Thread starter Jon S via DotNetMonster.com
  • Start date
J

Jon S via DotNetMonster.com

Hi all,
I'm trying to update the target database with the data thats in the target
dataset. I've included the snippit of code that is populating the target
dataset. When I loop round I see the source dataset give data to the target
dataset. I just don't know how to update the target database with the target
dataset.
Any help will be much appreciated???
Thanks, Dan.

if( tbl_Target.Rows.Count > -1 )
{
foreach(DataColumn col_Source in tbl_Source.Columns) //Create column object
to go through each column in table (source).
{
DataRow row_Target = tbl_Target.Rows[0]; //Create row object to state which
row to work on.
string str_ColNameSource; //Variable to hold the name of each column.
row_Target.BeginEdit();
str_ColNameSource = col_Source.ColumnName;
row_Target[str_ColNameSource] = row_Source[str_ColNameSource];
row_Target.EndEdit();
da_Target.Update(ds_Target, "Layouts");
}
}
 
C

Christiaan van Bergen

Hi Jon,

Though I don't have enough info, my first rewrite of your code is

if( tbl_Target.Rows.Count > 0 )
{
DataRow row_Target = tbl_Target.Rows[0];//Create row object to state
which row to work on.
row_Target.bBeginEdit();
foreach(DataColumn col_Source in tbl_Source.Columns) //Create column
object to go through each column in table (source).
{
if (row_Target.Tables.Columns.Contains(col_Source.ColumnName))
row_Target[col_Source.ColumnName] =
row_Source[col_Source.ColumnName];
}
row_Target.EndEdit();
da_Target.Update(ds_Target);
}

In this I assume that
tbl_Target is a DataTable drawn from ds_Target
you only want to UPDATE one row (the first) in tbl_Target with data from
one (the first) row in tbl_Source
there are no auto-incremental fields present

HTH
Christiaan
 
J

Jon S via DotNetMonster.com

Hey Christiaan,
Lovely piece of code, thanks very much.

Hi Jon,

Though I don't have enough info, my first rewrite of your code is

if( tbl_Target.Rows.Count > 0 )
{
DataRow row_Target = tbl_Target.Rows[0];//Create row object to state
which row to work on.
row_Target.bBeginEdit();
foreach(DataColumn col_Source in tbl_Source.Columns) //Create column
object to go through each column in table (source).
{
if (row_Target.Tables.Columns.Contains(col_Source.ColumnName))
row_Target[col_Source.ColumnName] =
row_Source[col_Source.ColumnName];
}
row_Target.EndEdit();
da_Target.Update(ds_Target);
}

In this I assume that
tbl_Target is a DataTable drawn from ds_Target
you only want to UPDATE one row (the first) in tbl_Target with data from
one (the first) row in tbl_Source
there are no auto-incremental fields present

HTH
Christiaan
Hi all,
I'm trying to update the target database with the data thats in the target
[quoted text clipped - 24 lines]
 

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