Clear a datatable before inserting new records

J

JamesT

Is there an easy way of clearing one datatable in a dataset and hence the
database table to no records?

I have a large dataset that contains many datatables. Most of the datatables
are updated using linked forms but a couple are not updated using forms as
the data they contain is too variable. I would like to clear the datatable
and then insert new records from scratch from classes I have defined. I
cannot find a method to clear the datatable except row by row. Inserting the
new data and updating the database I can do OK. Have I missed something? If
you supply any code could it be in VB.NET please.
 
J

JamesT

Thanks I will try that. I was looking at the tableadapter commands - no
wonder I was lost.
--
JamesT


Miha Markic said:
table.Rows.Clear() perhaps?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

JamesT said:
Is there an easy way of clearing one datatable in a dataset and hence the
database table to no records?

I have a large dataset that contains many datatables. Most of the
datatables
are updated using linked forms but a couple are not updated using forms as
the data they contain is too variable. I would like to clear the datatable
and then insert new records from scratch from classes I have defined. I
cannot find a method to clear the datatable except row by row. Inserting
the
new data and updating the database I can do OK. Have I missed something?
If
you supply any code could it be in VB.NET please.
 
M

Miha Markic

Hi James,

Perhaps a clarification to avoid misunderstandings.
table.Rows.Clear() will clear datatable in memory. It won't affect database
in any way.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

JamesT said:
Thanks I will try that. I was looking at the tableadapter commands - no
wonder I was lost.
--
JamesT


Miha Markic said:
table.Rows.Clear() perhaps?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

JamesT said:
Is there an easy way of clearing one datatable in a dataset and hence
the
database table to no records?

I have a large dataset that contains many datatables. Most of the
datatables
are updated using linked forms but a couple are not updated using forms
as
the data they contain is too variable. I would like to clear the
datatable
and then insert new records from scratch from classes I have defined. I
cannot find a method to clear the datatable except row by row.
Inserting
the
new data and updating the database I can do OK. Have I missed
something?
If
you supply any code could it be in VB.NET please.
 
J

JamesT

Do I need to do an update to the database with the cleared datatable before I
add new rows and then update again after adding the rows?

I am only just starting to use ADO.NET after many years of using ADODB even
with .NET. I thought it was time I started using the 'new' technology so I am
still on a learning curve. So please be gentle with me.

--
JamesT


Miha Markic said:
Hi James,

Perhaps a clarification to avoid misunderstandings.
table.Rows.Clear() will clear datatable in memory. It won't affect database
in any way.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

JamesT said:
Thanks I will try that. I was looking at the tableadapter commands - no
wonder I was lost.
--
JamesT


Miha Markic said:
table.Rows.Clear() perhaps?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Is there an easy way of clearing one datatable in a dataset and hence
the
database table to no records?

I have a large dataset that contains many datatables. Most of the
datatables
are updated using linked forms but a couple are not updated using forms
as
the data they contain is too variable. I would like to clear the
datatable
and then insert new records from scratch from classes I have defined. I
cannot find a method to clear the datatable except row by row.
Inserting
the
new data and updating the database I can do OK. Have I missed
something?
If
you supply any code could it be in VB.NET please.
 
W

William \(Bill\) Vaughn

Well, you might have waited too long. ;)
LINQ to SQL is being ... well, Microsoft is not that thrilled with it and
don't plan to give it much more attention. By some, it's not seen as a
viable approach. Count me among those who don't endorse it.

--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker’s Guide to Visual Studio and SQL Server (7th Edition)
____________________________________________________________________________________________



JamesT said:
Do I need to do an update to the database with the cleared datatable
before I
add new rows and then update again after adding the rows?

I am only just starting to use ADO.NET after many years of using ADODB
even
with .NET. I thought it was time I started using the 'new' technology so I
am
still on a learning curve. So please be gentle with me.

--
JamesT


Miha Markic said:
Hi James,

Perhaps a clarification to avoid misunderstandings.
table.Rows.Clear() will clear datatable in memory. It won't affect
database
in any way.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

JamesT said:
Thanks I will try that. I was looking at the tableadapter commands - no
wonder I was lost.
--
JamesT


:

table.Rows.Clear() perhaps?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Is there an easy way of clearing one datatable in a dataset and
hence
the
database table to no records?

I have a large dataset that contains many datatables. Most of the
datatables
are updated using linked forms but a couple are not updated using
forms
as
the data they contain is too variable. I would like to clear the
datatable
and then insert new records from scratch from classes I have
defined. I
cannot find a method to clear the datatable except row by row.
Inserting
the
new data and updating the database I can do OK. Have I missed
something?
If
you supply any code could it be in VB.NET please.
 
M

Miha Markic

Hi James,
JamesT said:
Do I need to do an update to the database with the cleared datatable
before I
add new rows and then update again after adding the rows?

If you clear rows from datatable then ado.net won't know nothing about them
(it will ignore them because they are not there anymore).
You should clear, add new rows and use adapter.Update to commit changes to
database. The new rows will be marked as Unchanged afterwards (row's
RowState will change from Added to Unchanged)
I am only just starting to use ADO.NET after many years of using ADODB
even
with .NET. I thought it was time I started using the 'new' technology so I
am
still on a learning curve. So please be gentle with me.

Sure, no problem. Perhaps you should read a topic about ado.net in .net help
files...
 

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