DataTable.HasChanges?

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

Hi,

I have a dataset with about 9 tables, some of which will have changes.

I'm trying to determine if the table has changes that need to be written to
the server.

I cannot find a property to do this;

I suppose I could use something like the following;


Dim aRows As DataRow()
aRows = tData.Select("*", "", DataViewRowState.Added)

If aRows.Length, > 0 Then
'Has New Rows
Return True
End If

etc for deleted and Modified

Is there a better way?

thanks

e
 
Eric,

Just check the RowState of all rows in the table. If all of them are
Unchanged, you do not have any changes.
This is how DataSet.HasChanges works.

If you're using DataAdapter with SqlServerCe or SqlClient, you do not need
to check if you have changes or not.
Just execute Update() for each table. It won't do anything if you have no
changes.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
Just check the RowState of all rows in the table. If all of them are
Unchanged, you do not have any changes.
This is how DataSet.HasChanges works.

Thanks for the response.

If you're using DataAdapter with SqlServerCe or SqlClient, you do not need
to check if you have changes or not.
Just execute Update() for each table. It won't do anything if you have no
changes.

I'm doing something that I am not sure if it is the correct way -

I have a stored procedure that returns a dataset with 9 datatables. Some of
these will be updated. To save time, for now, I am creating a dataadapter
and a command builder using the SQL for each of the 9 datatables. I thought
the overhead for that would be high, especially since each table has less
than 50 records, so I would just test the table for changes and only update
those tables that need updating.

Other suggestions? With stored procedures, to use the dataadapter and
command builder, I think this my only way. Eventually I may convert
entirely to stored procedures.

Here is my .HasChanges code:

Dim Dirty As Boolean = False

Dim R as DataRow

For Each R in tData.Rows
If R.RowState <> DataRowState.Unchanged Then

Dirty = True

Exit For

End If

Next

Return Dirty
 
If you do not have any changes, DataAdapter won't actually update anything.
It will loop through all the records, make sure all of them are unchanged
and terminate.
This is exactly what you would have to do to check if you have changes.
So, there's no point to duplicate DataAdapter's work and check for changes
before calling Update.

That means you're doing the right thing already, no need to complicate
things.

Best regards,

Ilya

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

--------------------
 
Eric:

No can do with Stored Procs and CommandBuilders. The Comm Builder infers
its update logic from the Select statement so this isn't going to work.
CommandBuilders are pretty lame anyway and usually end up showing their
limitations only when you are in production. Also, Most importantly, you
don't need a CommandBuilder and Configured DataAdapter. The Configuration
wizard will get you where you need to go.

While Ilya is correct that calling update if there aren't any changes won't
hurt anything, I wish he wouldn't say stuff like that b/c toooo many people
call .Update when they haven't determined if they have changes or not and
expect it to do something either way.
In this instance though his point is a very good one b/c you have 9
different tables, and iterating through the tables in the dataset calling
update won't hurt and you obviously get the whole HasChanges issue.
Moreoever, iterating through each row on each table is totally wasteful, and
in a resource constrained environment like the PPC, that's really not good.

You can create a DataView on each table, very little overhead here. You can
specify in the Contructor that you want to RowState filter to
Add/Modified/Deleted all of the above. Or you can just use
DataViewWhatever.RowStateFilter = DataViewRowState.Added
and then see if dataView.Count > 0

However, this is also more work than just calling update
 
Ilya:

Quick question b/c I've heard two different things. Does the adapter loop
through the table no matter what checking the rowstate when you call update?
Someone had told me the HasChanges property is set and it this is set, it
circumvents iterating the dataset. It makes sense but I wasn't sure and I
haven't been able to find any documentation eitehr way. Whatever it is the
cost is trivial, but on a PPC resources are scarce it'd be worth noting.

Thanks,

Bill

www.devbuzz.com
www.knowdotnet.com

"Ilya Tumanov [MS]" said:
If you do not have any changes, DataAdapter won't actually update anything.
It will loop through all the records, make sure all of them are unchanged
and terminate.
This is exactly what you would have to do to check if you have changes.
So, there's no point to duplicate DataAdapter's work and check for changes
before calling Update.

That means you're doing the right thing already, no need to complicate
things.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
Subject: Re: DataTable.HasChanges?
Date: Fri, 7 May 2004 14:53:07 -0500
Lines: 45
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: osh-197-nat-54.onshore.net 66.146.197.54
Path:
cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11
phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:52653
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework


Thanks for the response.



I'm doing something that I am not sure if it is the correct way -

I have a stored procedure that returns a dataset with 9 datatables.
Some
of
these will be updated. To save time, for now, I am creating a dataadapter
and a command builder using the SQL for each of the 9 datatables. I thought
the overhead for that would be high, especially since each table has less
than 50 records, so I would just test the table for changes and only update
those tables that need updating.

Other suggestions? With stored procedures, to use the dataadapter and
command builder, I think this my only way. Eventually I may convert
entirely to stored procedures.

Here is my .HasChanges code:

Dim Dirty As Boolean = False

Dim R as DataRow

For Each R in tData.Rows
If R.RowState <> DataRowState.Unchanged Then

Dirty = True

Exit For

End If

Next

Return Dirty
 
Inline, please...

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Reply-To: "William Ryan eMVP" <[email protected]>
From: "William Ryan eMVP" <[email protected]>
References: <[email protected]>
<[email protected]>
Subject: Re: DataTable.HasChanges?
Date: Fri, 7 May 2004 16:53:41 -0400
Lines: 115
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: 65.23.106.65
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11
phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:52657
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

Ilya:

Quick question b/c I've heard two different things. Does the adapter loop
through the table no matter what checking the rowstate when you call
update?

DataAdapter internals are quite complicated, but the simple answer would be
"yes".
Someone had told me the HasChanges property is set and it this is set, it
circumvents iterating the dataset.

This seems to be a smart idea...
Too bad, HasChanges() is not just a bit value to return.
It actually loops through all the records to determine if you have changes
in the DataSet.
Thus, HasChanges() will do exactly what you're trying to avoid - looping
through all the records.
And should you have changes, it will be done again in Update().
It makes sense but I wasn't sure and I
haven't been able to find any documentation eitehr way. Whatever it is the
cost is trivial, but on a PPC resources are scarce it'd be worth noting.

Unfortunately, it's unlikely you can save same time with HasChanges().
Let me demonstrate with this pseudo code:

if (.HasChanges) { -> foreach(DataRow) { if (RowState != Unchanged )
return true} return false;
.Update() -> foreach(DataRow) { if (RowState != Unchanged )
UpdateRow()};
}

There's no reason to check all the rows twice, right?
Thanks,

Bill

www.devbuzz.com
www.knowdotnet.com

"Ilya Tumanov [MS]" said:
If you do not have any changes, DataAdapter won't actually update anything.
It will loop through all the records, make sure all of them are unchanged
and terminate.
This is exactly what you would have to do to check if you have changes.
So, there's no point to duplicate DataAdapter's work and check for changes
before calling Update.

That means you're doing the right thing already, no need to complicate
things.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
Subject: Re: DataTable.HasChanges?
Date: Fri, 7 May 2004 14:53:07 -0500
Lines: 45
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: osh-197-nat-54.onshore.net 66.146.197.54
Path:
cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11
not
need have
no Some
 
Thank You!
"Ilya Tumanov [MS]" said:
Inline, please...

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.<[email protected]>
Subject: Re: DataTable.HasChanges?
Date: Fri, 7 May 2004 16:53:41 -0400
Lines: 115
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: 65.23.106.65
Path:
cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11
phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:52657
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

Ilya:

Quick question b/c I've heard two different things. Does the adapter loop
through the table no matter what checking the rowstate when you call
update?

DataAdapter internals are quite complicated, but the simple answer would be
"yes".

Someone had told me the HasChanges property is set and it this is set, it
circumvents iterating the dataset.

This seems to be a smart idea...
Too bad, HasChanges() is not just a bit value to return.
It actually loops through all the records to determine if you have changes
in the DataSet.
Thus, HasChanges() will do exactly what you're trying to avoid - looping
through all the records.
And should you have changes, it will be done again in Update().
It makes sense but I wasn't sure and I
haven't been able to find any documentation eitehr way. Whatever it is the
cost is trivial, but on a PPC resources are scarce it'd be worth noting.

Unfortunately, it's unlikely you can save same time with HasChanges().
Let me demonstrate with this pseudo code:

if (.HasChanges) { -> foreach(DataRow) { if (RowState != Unchanged )
return true} return false;
.Update() -> foreach(DataRow) { if (RowState != Unchanged )
UpdateRow()};
}

There's no reason to check all the rows twice, right?
Thanks,

Bill

www.devbuzz.com
www.knowdotnet.com

"Ilya Tumanov [MS]" said:
If you do not have any changes, DataAdapter won't actually update anything.
It will loop through all the records, make sure all of them are unchanged
and terminate.
This is exactly what you would have to do to check if you have changes.
So, there's no point to duplicate DataAdapter's work and check for changes
before calling Update.

That means you're doing the right thing already, no need to complicate
things.

Best regards,

Ilya

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

--------------------
From: "Eric" <[email protected]>
References: <[email protected]>
<[email protected]>
Subject: Re: DataTable.HasChanges?
Date: Fri, 7 May 2004 14:53:07 -0500
Lines: 45
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: osh-197-nat-54.onshore.net 66.146.197.54
Path:
cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11
 
No can do with Stored Procs and CommandBuilders. The Comm Builder infers
its update logic from the Select statement so this isn't going to work.

how about this scenario:

Fill the dataset with a stored procedure. Close connection.

...edit records..

open new connection and create a dataadapter for each table to update using
a sql statement such as SELECT * FROM table; create command builder, and
update the dataadapter.

This seems to be working, at least for the several tables I have tried so
far.
CommandBuilders are pretty lame anyway and usually end up showing their
limitations only when you are in production. Also, Most importantly, you
don't need a CommandBuilder and Configured DataAdapter. The Configuration
wizard will get you where you need to go.

I'll probably eventually go to store procedures. . .

While Ilya is correct that calling update if there aren't any changes won't
hurt anything, I wish he wouldn't say stuff like that b/c toooo many people
call .Update when they haven't determined if they have changes or not and
expect it to do something either way.
In this instance though his point is a very good one b/c you have 9
different tables, and iterating through the tables in the dataset calling
update won't hurt and you obviously get the whole HasChanges issue.
Moreoever, iterating through each row on each table is totally wasteful, and
in a resource constrained environment like the PPC, that's really not
good.

Each table has between 0 and 20 records, with most haveing about 5. . .so is
the overhead of doing all the above command builder stuff really greater on
the PPC? Not sure. On the desktop that's another story.
However, this is also more work than just calling update

I may do some tests, should be easy to do, and I have built in benchmarking
code.

If I get to it, will let you know.

Thanks for the comments.

e
 
Just a small point - late in the discussion. The datatable has a
.GetChanges method which might be useful to you. It creates a copy of
the datatable which only contains changed rows, if any.
 
Back
Top