What is the best way to load a table to database?

  • Thread starter Thread starter chris
  • Start date Start date
C

chris

Hi,

I have a table in the database. I have a method which will
return a table with the same structure as the DB one and
with all data I need.
What I want to do is to remove all data in the DB table
and reload it with the table I created.
I tried to use SqlDBAdaptor and it seems I cannot do
something like
ds.Tables[0]=myTable
It does not work.
So what would be the best way for me to achieve it in
ADO.NET?
Thanks a lot

Chris
 
Thanks Miha. Copy works, but I still have not find the
right way to update my DB in one shot. Here is my codes.
The purpose is to load the table from the DB, delete all,
then load what I have and reload it to the DB. Did I miss
something?
Thanks for the help

//dt is the poluated table I used

SqlConnection sconn = new SqlConnection(connectString);
SqlDataAdapter da = new SqlDataAdapter(
"select * from [mytable]",connectString);
DataSet ds = new DataSet();
da.Fill(ds);

DataTable dbTable=ds.Tables[0];
dbTable.Clear();
dbTable=dt.Copy();
dbTable.AcceptChanges();
da.Update(ds);
-----Original Message-----
Hi Chris,

Try using DataTable.Copy method instead.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Hi,

I have a table in the database. I have a method which will
return a table with the same structure as the DB one and
with all data I need.
What I want to do is to remove all data in the DB table
and reload it with the table I created.
I tried to use SqlDBAdaptor and it seems I cannot do
something like
ds.Tables[0]=myTable
It does not work.
So what would be the best way for me to achieve it in
ADO.NET?
Thanks a lot

Chris


.
 
Hi Chris,

Calling AcceptChanges before Update is classical error :)
You should invert the order.
However, you don't neet do copy rows to dataset's table.
Just call da.Update(dt); instead.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Chris said:
Thanks Miha. Copy works, but I still have not find the
right way to update my DB in one shot. Here is my codes.
The purpose is to load the table from the DB, delete all,
then load what I have and reload it to the DB. Did I miss
something?
Thanks for the help

//dt is the poluated table I used

SqlConnection sconn = new SqlConnection(connectString);
SqlDataAdapter da = new SqlDataAdapter(
"select * from [mytable]",connectString);
DataSet ds = new DataSet();
da.Fill(ds);

DataTable dbTable=ds.Tables[0];
dbTable.Clear();
dbTable=dt.Copy();
dbTable.AcceptChanges();
da.Update(ds);
-----Original Message-----
Hi Chris,

Try using DataTable.Copy method instead.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Hi,

I have a table in the database. I have a method which will
return a table with the same structure as the DB one and
with all data I need.
What I want to do is to remove all data in the DB table
and reload it with the table I created.
I tried to use SqlDBAdaptor and it seems I cannot do
something like
ds.Tables[0]=myTable
It does not work.
So what would be the best way for me to achieve it in
ADO.NET?
Thanks a lot

Chris


.
 
Hi Miha,

Following your direction, after correcting a bunch of
other mistakes, I finally get my things work as what I
want.
Thank you very much for your help.

Chris
-----Original Message-----
Hi Chris,

Calling AcceptChanges before Update is classical error :)
You should invert the order.
However, you don't neet do copy rows to dataset's table.
Just call da.Update(dt); instead.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Thanks Miha. Copy works, but I still have not find the
right way to update my DB in one shot. Here is my codes.
The purpose is to load the table from the DB, delete all,
then load what I have and reload it to the DB. Did I miss
something?
Thanks for the help

//dt is the poluated table I used

SqlConnection sconn = new SqlConnection(connectString);
SqlDataAdapter da = new SqlDataAdapter(
"select * from [mytable]",connectString);
DataSet ds = new DataSet();
da.Fill(ds);

DataTable dbTable=ds.Tables[0];
dbTable.Clear();
dbTable=dt.Copy();
dbTable.AcceptChanges();
da.Update(ds);
-----Original Message-----
Hi Chris,

Try using DataTable.Copy method instead.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Hi,

I have a table in the database. I have a method which will
return a table with the same structure as the DB one and
with all data I need.
What I want to do is to remove all data in the DB table
and reload it with the table I created.
I tried to use SqlDBAdaptor and it seems I cannot do
something like
ds.Tables[0]=myTable
It does not work.
So what would be the best way for me to achieve it in
ADO.NET?
Thanks a lot

Chris


.


.
 
When do you think it is a good idea to call AcceptChanges and RejectChanges
at all? For a simple scenario like Chris's example how about just leaving
out the AcceptChanges call altogether?

Brad Williams


Miha Markic said:
Hi Chris,

Calling AcceptChanges before Update is classical error :)
You should invert the order.
However, you don't neet do copy rows to dataset's table.
Just call da.Update(dt); instead.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Chris said:
Thanks Miha. Copy works, but I still have not find the
right way to update my DB in one shot. Here is my codes.
The purpose is to load the table from the DB, delete all,
then load what I have and reload it to the DB. Did I miss
something?
Thanks for the help

//dt is the poluated table I used

SqlConnection sconn = new SqlConnection(connectString);
SqlDataAdapter da = new SqlDataAdapter(
"select * from [mytable]",connectString);
DataSet ds = new DataSet();
da.Fill(ds);

DataTable dbTable=ds.Tables[0];
dbTable.Clear();
dbTable=dt.Copy();
dbTable.AcceptChanges();
da.Update(ds);
-----Original Message-----
Hi Chris,

Try using DataTable.Copy method instead.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Hi,

I have a table in the database. I have a method which will
return a table with the same structure as the DB one and
with all data I need.
What I want to do is to remove all data in the DB table
and reload it with the table I created.
I tried to use SqlDBAdaptor and it seems I cannot do
something like
ds.Tables[0]=myTable
It does not work.
So what would be the best way for me to achieve it in
ADO.NET?
Thanks a lot

Chris


.
 
Hi Brad,

Thanks for the suggestions. So at what senario should I
use acceptchanges and what senario shouldn't I use it?
What are pros and cons for using/(not using it)?
Thanks

Chris
-----Original Message-----
When do you think it is a good idea to call AcceptChanges and RejectChanges
at all? For a simple scenario like Chris's example how about just leaving
out the AcceptChanges call altogether?

Brad Williams


Miha Markic said:
Hi Chris,

Calling AcceptChanges before Update is classical error :)
You should invert the order.
However, you don't neet do copy rows to dataset's table.
Just call da.Update(dt); instead.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Thanks Miha. Copy works, but I still have not find the
right way to update my DB in one shot. Here is my codes.
The purpose is to load the table from the DB, delete all,
then load what I have and reload it to the DB. Did I miss
something?
Thanks for the help

//dt is the poluated table I used

SqlConnection sconn = new SqlConnection (connectString);
SqlDataAdapter da = new SqlDataAdapter(
"select * from [mytable]",connectString);
DataSet ds = new DataSet();
da.Fill(ds);

DataTable dbTable=ds.Tables[0];
dbTable.Clear();
dbTable=dt.Copy();
dbTable.AcceptChanges();
da.Update(ds);

-----Original Message-----
Hi Chris,

Try using DataTable.Copy method instead.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

message
Hi,

I have a table in the database. I have a method which
will
return a table with the same structure as the DB one and
with all data I need.
What I want to do is to remove all data in the DB table
and reload it with the table I created.
I tried to use SqlDBAdaptor and it seems I cannot do
something like
ds.Tables[0]=myTable
It does not work.
So what would be the best way for me to achieve it in
ADO.NET?
Thanks a lot

Chris


.


.
 
Back
Top