PC Review


Reply
Thread Tools Rate Thread

delete all rows in a datatable

 
 
jeff
Guest
Posts: n/a
 
      16th Nov 2004
i am using ms access as my database

i can successfully delete a single record from a table using :

targetRow.Delete()
Try
reportDataAdapter.Update(reportDataSet, "table1" )
reportDataSet.AcceptChanges()
Catch ex As OleDbException
reportCompanySitesDataSet.RejectChanges()
MessageBox.Show(ex.Message)
End Try


however, i can't delete all records in a table at once using some simple and
fast way.

what i try is :
reportTable.Rows.Clear()

Try
reportDataAdapter.Update(reportDataSet, "table1" )
reportDataSet.AcceptChanges()
Catch ex As OleDbException
reportCompanySitesDataSet.RejectChanges()
MessageBox.Show(ex.Message)
End Try

it fails (without any runtime error)

if you have an example / suggestion, pls let me know.

thanks.


 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      16th Nov 2004
Jeff,

You would have to use a for each loop

for each targetRow as datarow in reportdataset.tables("table1").rows
targetRow.delete
next

Your acceptchanges is not needed, because that is a part of the dataadapter,
and read as well this page for the rollback

http://support.microsoft.com/default...b;en-us;310351.

When you clear your datatables the rows are removed and therefore not
updated with a rowstate remove.

I hope this helps?

Cor

"jeff" <(E-Mail Removed)>

>i am using ms access as my database
>
> i can successfully delete a single record from a table using :
>
> targetRow.Delete()
> Try
> reportDataAdapter.Update(reportDataSet, "table1" )
> reportDataSet.AcceptChanges()
> Catch ex As OleDbException
> reportCompanySitesDataSet.RejectChanges()
> MessageBox.Show(ex.Message)
> End Try
>
>
> however, i can't delete all records in a table at once using some simple
> and
> fast way.
>
> what i try is :
> reportTable.Rows.Clear()
>
> Try
> reportDataAdapter.Update(reportDataSet, "table1" )
> reportDataSet.AcceptChanges()
> Catch ex As OleDbException
> reportCompanySitesDataSet.RejectChanges()
> MessageBox.Show(ex.Message)
> End Try
>
> it fails (without any runtime error)
>
> if you have an example / suggestion, pls let me know.
>
> thanks.
>
>



 
Reply With Quote
 
jeff
Guest
Posts: n/a
 
      16th Nov 2004
thanks mate for the suggestion.

actually i am using the loop method to delete all records in a table.
is there any other more effective way to do it as looping may not be so good
if the number of record in a table is huge.

many thanks.

"Cor Ligthert" <(E-Mail Removed)> 在郵件
news:%(E-Mail Removed) 中撰寫...
> Jeff,
>
> You would have to use a for each loop
>
> for each targetRow as datarow in reportdataset.tables("table1").rows
> targetRow.delete
> next
>
> Your acceptchanges is not needed, because that is a part of the

dataadapter,
> and read as well this page for the rollback
>
> http://support.microsoft.com/default...b;en-us;310351.
>
> When you clear your datatables the rows are removed and therefore not
> updated with a rowstate remove.
>
> I hope this helps?
>
> Cor
>
> "jeff" <(E-Mail Removed)>
>
> >i am using ms access as my database
> >
> > i can successfully delete a single record from a table using :
> >
> > targetRow.Delete()
> > Try
> > reportDataAdapter.Update(reportDataSet, "table1" )
> > reportDataSet.AcceptChanges()
> > Catch ex As OleDbException
> > reportCompanySitesDataSet.RejectChanges()
> > MessageBox.Show(ex.Message)
> > End Try
> >
> >
> > however, i can't delete all records in a table at once using some simple
> > and
> > fast way.
> >
> > what i try is :
> > reportTable.Rows.Clear()
> >
> > Try
> > reportDataAdapter.Update(reportDataSet, "table1" )
> > reportDataSet.AcceptChanges()
> > Catch ex As OleDbException
> > reportCompanySitesDataSet.RejectChanges()
> > MessageBox.Show(ex.Message)
> > End Try
> >
> > it fails (without any runtime error)
> >
> > if you have an example / suggestion, pls let me know.
> >
> > thanks.
> >
> >

>
>



 
Reply With Quote
 
=?Utf-8?B?TmlnZWwgQXJtc3Ryb25n?=
Guest
Posts: n/a
 
      16th Nov 2004
Hi jeff

Why don't you just use a Command against your database and pass through a
DELETE query? It will certainly execute much faster...

Nigel Armstrong

"jeff" wrote:

> thanks mate for the suggestion.
>
> actually i am using the loop method to delete all records in a table.
> is there any other more effective way to do it as looping may not be so good
> if the number of record in a table is huge.
>
> many thanks.
>
> "Cor Ligthert" <(E-Mail Removed)> 礎b繞l瞼籀
> news:%(E-Mail Removed) 瞻瞻翹繞翹g...
> > Jeff,
> >
> > You would have to use a for each loop
> >
> > for each targetRow as datarow in reportdataset.tables("table1").rows
> > targetRow.delete
> > next
> >
> > Your acceptchanges is not needed, because that is a part of the

> dataadapter,
> > and read as well this page for the rollback
> >
> > http://support.microsoft.com/default...b;en-us;310351.
> >
> > When you clear your datatables the rows are removed and therefore not
> > updated with a rowstate remove.
> >
> > I hope this helps?
> >
> > Cor
> >
> > "jeff" <(E-Mail Removed)>
> >
> > >i am using ms access as my database
> > >
> > > i can successfully delete a single record from a table using :
> > >
> > > targetRow.Delete()
> > > Try
> > > reportDataAdapter.Update(reportDataSet, "table1" )
> > > reportDataSet.AcceptChanges()
> > > Catch ex As OleDbException
> > > reportCompanySitesDataSet.RejectChanges()
> > > MessageBox.Show(ex.Message)
> > > End Try
> > >
> > >
> > > however, i can't delete all records in a table at once using some simple
> > > and
> > > fast way.
> > >
> > > what i try is :
> > > reportTable.Rows.Clear()
> > >
> > > Try
> > > reportDataAdapter.Update(reportDataSet, "table1" )
> > > reportDataSet.AcceptChanges()
> > > Catch ex As OleDbException
> > > reportCompanySitesDataSet.RejectChanges()
> > > MessageBox.Show(ex.Message)
> > > End Try
> > >
> > > it fails (without any runtime error)
> > >
> > > if you have an example / suggestion, pls let me know.
> > >
> > > thanks.
> > >
> > >

> >
> >

>
>
>

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      16th Nov 2004
Jeff,

> actually i am using the loop method to delete all records in a table.
> is there any other more effective way to do it as looping may not be so
> good
> if the number of record in a table is huge.


Did you test how much time it took, a for loop is a very fast command.

I did give you a wrong command you can take this instead however although
that this is fast I would take the other option that I typed beneath it..
\\\\
For i As Integer = dt.Rows.Count - 1 To 0 Step -1
dt.Rows(i).Delete()
Next
///

When you want to delete all rows from a database table than you can use the
SQL delete command for that with a command.executenonquery

http://msdn.microsoft.com/library/de...de-dz_9lut.asp

http://msdn.microsoft.com/library/de...querytopic.asp

I hope this helps?

Cor


 
Reply With Quote
 
jeff
Guest
Posts: n/a
 
      17th Nov 2004
very useful. i now use the executenonquery method.

thanks all.

"Cor Ligthert" <(E-Mail Removed)> 在郵件
news:OXb$(E-Mail Removed) 中撰寫...
> Jeff,
>
> > actually i am using the loop method to delete all records in a table.
> > is there any other more effective way to do it as looping may not be so
> > good
> > if the number of record in a table is huge.

>
> Did you test how much time it took, a for loop is a very fast command.
>
> I did give you a wrong command you can take this instead however although
> that this is fast I would take the other option that I typed beneath it..
> \\\\
> For i As Integer = dt.Rows.Count - 1 To 0 Step -1
> dt.Rows(i).Delete()
> Next
> ///
>
> When you want to delete all rows from a database table than you can use

the
> SQL delete command for that with a command.executenonquery
>
>

http://msdn.microsoft.com/library/de...de-dz_9lut.asp
>
>

http://msdn.microsoft.com/library/de...querytopic.asp
>
> I hope this helps?
>
> Cor
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Rows in DataTable Sreppohcdoow Microsoft ADO .NET 0 26th Mar 2006 08:28 PM
DataTable DataRow Delete Duplicate Rows =?Utf-8?B?VmlqYXkgUG90ZQ==?= Microsoft C# .NET 4 9th Sep 2005 09:59 PM
Delete rows from DataTable =?Utf-8?B?TmVvIFRoZSBPbmU=?= Microsoft ADO .NET 2 22nd Jul 2005 03:10 PM
Delete DataView rows from DataTable (should be simple) MattB Microsoft ADO .NET 1 21st Jun 2005 07:15 PM
How can I use real SQL on a DataTable? i.e. not array of rows using a filter... as in DataTable.Select Dan V. Microsoft C# .NET 3 1st Jul 2004 03:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:55 PM.