copy datatable

G

Guest

Hi,

In my quest for performance, I come up with another question...
There is no DataTable.Copy() with CF and I need to do that. So I came up
with my own implementation which is as follow:

public static DataTable DataTableCopy(DataTable dtOriginal)
{
DataTable dtCopy = dtOriginal.Clone();
try
{
for (int j=0;j<dtOriginal.Rows.Count;j++)
{
dtCopy.ImportRow(dtOriginal.Rows[j]);
}
}
catch(Exception ex)
{
throw new System.Exception("Datatable copy error", ex);
}

return dtCopy;
}

I'm wondering if it is the fastest way to do it though. I use ImportRow
because I need to preserve the RowState values.

Any comment on this?

Thanks,
Sitar.
 
P

Peter Foot [MVP]

I'm not sure if it will improve performance in your example (depends on the
constraints you have set up), but you could call dtCopy.BeginLoadData and
EndLoadData around your insertions e.g.

dtCopy.BeginLoadData();

foreach(DataRow dr in dtOriginal.Rows)
{
dtCopy.ImportRow(dr);
}

dt.Copy.EndLoadData();

Peter
 
F

Fred Hirschfeld

It is also faster to save to XML and load into the new DataSet as ReadXML...

Peter Foot said:
I'm not sure if it will improve performance in your example (depends on
the constraints you have set up), but you could call dtCopy.BeginLoadData
and EndLoadData around your insertions e.g.

dtCopy.BeginLoadData();

foreach(DataRow dr in dtOriginal.Rows)
{
dtCopy.ImportRow(dr);
}

dt.Copy.EndLoadData();

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://blog.opennetcf.org/pfoot/

Sitar said:
Hi,

In my quest for performance, I come up with another question...
There is no DataTable.Copy() with CF and I need to do that. So I came up
with my own implementation which is as follow:

public static DataTable DataTableCopy(DataTable dtOriginal)
{
DataTable dtCopy = dtOriginal.Clone();
try
{
for (int j=0;j<dtOriginal.Rows.Count;j++)
{
dtCopy.ImportRow(dtOriginal.Rows[j]);
}
}
catch(Exception ex)
{
throw new System.Exception("Datatable copy error", ex);
}

return dtCopy;
}

I'm wondering if it is the fastest way to do it though. I use ImportRow
because I need to preserve the RowState values.

Any comment on this?

Thanks,
Sitar.
 
I

Ilya Tumanov [MS]

That's hardly true. The rule of thumb is: if it has something to do with
XML, it's the slowest possible way to do it.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Fred Hirschfeld" <[email protected]>
References: <[email protected]>
Subject: Re: copy datatable
Date: Fri, 18 Mar 2005 09:28:40 -0800
Lines: 62
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
X-RFC2646: Format=Flowed; Response
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework,microsoft.public.dotnet.f
ramework.performance
NNTP-Posting-Host: svvan340.sierrasys.com 192.251.30.171
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP1
0.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.performance:9737
microsoft.public.dotnet.framework.compactframework:73589
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

It is also faster to save to XML and load into the new DataSet as ReadXML...

Peter Foot said:
I'm not sure if it will improve performance in your example (depends on
the constraints you have set up), but you could call dtCopy.BeginLoadData
and EndLoadData around your insertions e.g.

dtCopy.BeginLoadData();

foreach(DataRow dr in dtOriginal.Rows)
{
dtCopy.ImportRow(dr);
}

dt.Copy.EndLoadData();

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://blog.opennetcf.org/pfoot/

Sitar said:
Hi,

In my quest for performance, I come up with another question...
There is no DataTable.Copy() with CF and I need to do that. So I came up
with my own implementation which is as follow:

public static DataTable DataTableCopy(DataTable dtOriginal)
{
DataTable dtCopy = dtOriginal.Clone();
try
{
for (int j=0;j<dtOriginal.Rows.Count;j++)
{
dtCopy.ImportRow(dtOriginal.Rows[j]);
}
}
catch(Exception ex)
{
throw new System.Exception("Datatable copy error", ex);
}

return dtCopy;
}

I'm wondering if it is the fastest way to do it though. I use ImportRow
because I need to preserve the RowState values.

Any comment on this?

Thanks,
Sitar.
 
R

Robbe Morris [C# MVP]

.. Having done both, the manual copy is definitely faster.

http://www.eggheadcafe.com/articles/copydatatable.asp

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx



"Ilya Tumanov [MS]" said:
That's hardly true. The rule of thumb is: if it has something to do with
XML, it's the slowest possible way to do it.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "Fred Hirschfeld" <[email protected]>
References: <[email protected]>
Subject: Re: copy datatable
Date: Fri, 18 Mar 2005 09:28:40 -0800
Lines: 62
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
X-RFC2646: Format=Flowed; Response
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework,microsoft.public.dotnet.f
ramework.performance
NNTP-Posting-Host: svvan340.sierrasys.com 192.251.30.171
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP1
0.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.performance:9737
microsoft.public.dotnet.framework.compactframework:73589
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

It is also faster to save to XML and load into the new DataSet as ReadXML...

Peter Foot said:
I'm not sure if it will improve performance in your example (depends on
the constraints you have set up), but you could call dtCopy.BeginLoadData
and EndLoadData around your insertions e.g.

dtCopy.BeginLoadData();

foreach(DataRow dr in dtOriginal.Rows)
{
dtCopy.ImportRow(dr);
}

dt.Copy.EndLoadData();

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://blog.opennetcf.org/pfoot/

Hi,

In my quest for performance, I come up with another question...
There is no DataTable.Copy() with CF and I need to do that. So I came up
with my own implementation which is as follow:

public static DataTable DataTableCopy(DataTable dtOriginal)
{
DataTable dtCopy = dtOriginal.Clone();
try
{
for (int j=0;j<dtOriginal.Rows.Count;j++)
{
dtCopy.ImportRow(dtOriginal.Rows[j]);
}
}
catch(Exception ex)
{
throw new System.Exception("Datatable copy error", ex);
}

return dtCopy;
}

I'm wondering if it is the fastest way to do it though. I use
ImportRow
because I need to preserve the RowState values.

Any comment on this?

Thanks,
Sitar.
 
G

Guest

Thanks everyone. Previous experience with Xml proved it's way too slow...

Robbe, I've checked out your code. You're using a foreach loop. I'm still
not clear whereas it is faster to use a for .... or a foreach .....
It is supposed to be the same but I've read for ... is faster as foreach was
not perfectly implemented in CF. But then in some articles people say foreach
is faster than for .....
What is the truth about this out there?

Cheers,
Sitar.
 
G

Guest

I don't know whether there would be any difference in performance but I often
use the following syntax (it's vb.net not c#)

Dim dtClone As DataTable
Try
dtClone = dtSource.Clone
For i = 0 To dtSource.Rows.Count - 1
dtClone.Rows.Add(dt.Rows(i).ItemArray)
Next
Catch ex As Exception
Throw New Exception(ex.Message)
End Try

Try this and let me know if there's any perf differences, but in general
that's the way to go (wrap either what you're doing or what I'm doing into a
class and use its copy method). Much more convenient, I'm surprised msoft
didn't provide that method by default.
 
G

Guest

Hi Alex,

The problem when you do Rows.Add() is that it sets the rowstate to added.
That's why I used ImportRow because I want an exact copy
(structure/data/state).

Sitar.
___
Alex said:
I don't know whether there would be any difference in performance but I often
use the following syntax (it's vb.net not c#)

Dim dtClone As DataTable
Try
dtClone = dtSource.Clone
For i = 0 To dtSource.Rows.Count - 1
dtClone.Rows.Add(dt.Rows(i).ItemArray)
Next
Catch ex As Exception
Throw New Exception(ex.Message)
End Try

Try this and let me know if there's any perf differences, but in general
that's the way to go (wrap either what you're doing or what I'm doing into a
class and use its copy method). Much more convenient, I'm surprised msoft
didn't provide that method by default.
Sitar said:
Hi,

In my quest for performance, I come up with another question...
There is no DataTable.Copy() with CF and I need to do that. So I came up
with my own implementation which is as follow:

public static DataTable DataTableCopy(DataTable dtOriginal)
{
DataTable dtCopy = dtOriginal.Clone();
try
{
for (int j=0;j<dtOriginal.Rows.Count;j++)
{
dtCopy.ImportRow(dtOriginal.Rows[j]);
}
}
catch(Exception ex)
{
throw new System.Exception("Datatable copy error", ex);
}

return dtCopy;
}

I'm wondering if it is the fastest way to do it though. I use ImportRow
because I need to preserve the RowState values.

Any comment on this?

Thanks,
Sitar.
 
F

Fred Hirschfeld

You can always indicate to the DataSet to accept changes once you have added
everything but this assumes that the DataSet before copy does not have and
change state it is managing.

Fred

Sitar said:
Hi Alex,

The problem when you do Rows.Add() is that it sets the rowstate to added.
That's why I used ImportRow because I want an exact copy
(structure/data/state).

Sitar.
___
Alex said:
I don't know whether there would be any difference in performance but I
often
use the following syntax (it's vb.net not c#)

Dim dtClone As DataTable
Try
dtClone = dtSource.Clone
For i = 0 To dtSource.Rows.Count - 1
dtClone.Rows.Add(dt.Rows(i).ItemArray)
Next
Catch ex As Exception
Throw New Exception(ex.Message)
End Try

Try this and let me know if there's any perf differences, but in general
that's the way to go (wrap either what you're doing or what I'm doing
into a
class and use its copy method). Much more convenient, I'm surprised msoft
didn't provide that method by default.
Sitar said:
Hi,

In my quest for performance, I come up with another question...
There is no DataTable.Copy() with CF and I need to do that. So I came
up
with my own implementation which is as follow:

public static DataTable DataTableCopy(DataTable dtOriginal)
{
DataTable dtCopy = dtOriginal.Clone();
try
{
for (int j=0;j<dtOriginal.Rows.Count;j++)
{
dtCopy.ImportRow(dtOriginal.Rows[j]);
}
}
catch(Exception ex)
{
throw new System.Exception("Datatable copy error", ex);
}

return dtCopy;
}

I'm wondering if it is the fastest way to do it though. I use ImportRow
because I need to preserve the RowState values.

Any comment on this?

Thanks,
Sitar.
 
G

Guest

Yes, but that's only a special case. It would not simulate the Copy() of the
full framework.

Sitar.
___
 

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