PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Q: Copying part of a table

Reply

Q: Copying part of a table

 
Thread Tools Rate Thread
Old 21-09-2006, 08:14 AM   #1
G .Net
Guest
 
Posts: n/a
Default Q: Copying part of a table


Hi

I'm wondering if anybody can help me with the following problem?

I have a table with several rows. I want to create a new table, with exactly
the same structure, but has only a subset of the rows in the original table.

I have been able to do this using the following code but wonder if there is
a more efficient/fast way of doing it.

Dim subsetTable As DataTable = sourceTable.Clone()

For Each row As DataRow In sourceTable.Select("[Name] = 'John'")
Dim newRow As DataRow = subsetTable.NewRow()
For i As Integer = 0 To sourceTable.Columns.Count - 1
newRow(i) = row(i)
Next
subsetTable.Rows.Add(newRow)
Next

Using the code above I can a new table which has all the rows from the
source table with the field "Name" set to "John".

Can anybody suggest a more efficient/fast way of doing this?

Thanks in advance


  Reply With Quote
Old 21-09-2006, 11:13 AM   #2
Pritcham
Guest
 
Posts: n/a
Default Re: Q: Copying part of a table

Hi

You can actually do this via SQL. If you use the "INSERT INTO myTable"
statement then it will add rows to an existing table, if you use
"SELECT INTO myTable" it will create the table first, then add the
details. Both of these can have a WHERE clause so you can filter
what's going in to the table.

Hope that helps
Martin

G .Net wrote:
> Hi
>
> I'm wondering if anybody can help me with the following problem?
>
> I have a table with several rows. I want to create a new table, with exactly
> the same structure, but has only a subset of the rows in the original table.
>
> I have been able to do this using the following code but wonder if there is
> a more efficient/fast way of doing it.
>
> Dim subsetTable As DataTable = sourceTable.Clone()
>
> For Each row As DataRow In sourceTable.Select("[Name] = 'John'")
> Dim newRow As DataRow = subsetTable.NewRow()
> For i As Integer = 0 To sourceTable.Columns.Count - 1
> newRow(i) = row(i)
> Next
> subsetTable.Rows.Add(newRow)
> Next
>
> Using the code above I can a new table which has all the rows from the
> source table with the field "Name" set to "John".
>
> Can anybody suggest a more efficient/fast way of doing this?
>
> Thanks in advance


  Reply With Quote
Old 21-09-2006, 12:07 PM   #3
G .Net
Guest
 
Posts: n/a
Default Re: Q: Copying part of a table

Hi Martin

Good idea, but I don't think you can do this via DataSets only - which is my
intention.

"Pritcham" <dontwanttogivemyname@hotmail.com> wrote in message
news:1158833591.718890.97850@e3g2000cwe.googlegroups.com...
> Hi
>
> You can actually do this via SQL. If you use the "INSERT INTO myTable"
> statement then it will add rows to an existing table, if you use
> "SELECT INTO myTable" it will create the table first, then add the
> details. Both of these can have a WHERE clause so you can filter
> what's going in to the table.
>
> Hope that helps
> Martin
>
> G .Net wrote:
>> Hi
>>
>> I'm wondering if anybody can help me with the following problem?
>>
>> I have a table with several rows. I want to create a new table, with
>> exactly
>> the same structure, but has only a subset of the rows in the original
>> table.
>>
>> I have been able to do this using the following code but wonder if there
>> is
>> a more efficient/fast way of doing it.
>>
>> Dim subsetTable As DataTable = sourceTable.Clone()
>>
>> For Each row As DataRow In sourceTable.Select("[Name] = 'John'")
>> Dim newRow As DataRow = subsetTable.NewRow()
>> For i As Integer = 0 To sourceTable.Columns.Count - 1
>> newRow(i) = row(i)
>> Next
>> subsetTable.Rows.Add(newRow)
>> Next
>>
>> Using the code above I can a new table which has all the rows from the
>> source table with the field "Name" set to "John".
>>
>> Can anybody suggest a more efficient/fast way of doing this?
>>
>> Thanks in advance

>



  Reply With Quote
Old 21-09-2006, 12:41 PM   #4
=?Utf-8?B?S2VycnkgTW9vcm1hbg==?=
Guest
 
Posts: n/a
Default Re: Q: Copying part of a table

G.Net,

In .Net 2.0 you can create a dataview of the original datatable, filter it
to contain only the rows you want, and then use the dataview's ToTable method
to create a new datatable containing only the rows from the filtered dataview.

Kerry Moorman


"G .Net" wrote:

> Hi Martin
>
> Good idea, but I don't think you can do this via DataSets only - which is my
> intention.
>
> "Pritcham" <dontwanttogivemyname@hotmail.com> wrote in message
> news:1158833591.718890.97850@e3g2000cwe.googlegroups.com...
> > Hi
> >
> > You can actually do this via SQL. If you use the "INSERT INTO myTable"
> > statement then it will add rows to an existing table, if you use
> > "SELECT INTO myTable" it will create the table first, then add the
> > details. Both of these can have a WHERE clause so you can filter
> > what's going in to the table.
> >
> > Hope that helps
> > Martin
> >
> > G .Net wrote:
> >> Hi
> >>
> >> I'm wondering if anybody can help me with the following problem?
> >>
> >> I have a table with several rows. I want to create a new table, with
> >> exactly
> >> the same structure, but has only a subset of the rows in the original
> >> table.
> >>
> >> I have been able to do this using the following code but wonder if there
> >> is
> >> a more efficient/fast way of doing it.
> >>
> >> Dim subsetTable As DataTable = sourceTable.Clone()
> >>
> >> For Each row As DataRow In sourceTable.Select("[Name] = 'John'")
> >> Dim newRow As DataRow = subsetTable.NewRow()
> >> For i As Integer = 0 To sourceTable.Columns.Count - 1
> >> newRow(i) = row(i)
> >> Next
> >> subsetTable.Rows.Add(newRow)
> >> Next
> >>
> >> Using the code above I can a new table which has all the rows from the
> >> source table with the field "Name" set to "John".
> >>
> >> Can anybody suggest a more efficient/fast way of doing this?
> >>
> >> Thanks in advance

> >

>
>
>

  Reply With Quote
Old 21-09-2006, 02:10 PM   #5
G .Net
Guest
 
Posts: n/a
Default Re: Q: Copying part of a table

Thanks Kerry. Very interesting.

"Kerry Moorman" <KerryMoorman@discussions.microsoft.com> wrote in message
news:8B7C3A5E-521C-4544-AEB0-4250570A8678@microsoft.com...
> G.Net,
>
> In .Net 2.0 you can create a dataview of the original datatable, filter it
> to contain only the rows you want, and then use the dataview's ToTable
> method
> to create a new datatable containing only the rows from the filtered
> dataview.
>
> Kerry Moorman
>
>
> "G .Net" wrote:
>
>> Hi Martin
>>
>> Good idea, but I don't think you can do this via DataSets only - which is
>> my
>> intention.
>>
>> "Pritcham" <dontwanttogivemyname@hotmail.com> wrote in message
>> news:1158833591.718890.97850@e3g2000cwe.googlegroups.com...
>> > Hi
>> >
>> > You can actually do this via SQL. If you use the "INSERT INTO myTable"
>> > statement then it will add rows to an existing table, if you use
>> > "SELECT INTO myTable" it will create the table first, then add the
>> > details. Both of these can have a WHERE clause so you can filter
>> > what's going in to the table.
>> >
>> > Hope that helps
>> > Martin
>> >
>> > G .Net wrote:
>> >> Hi
>> >>
>> >> I'm wondering if anybody can help me with the following problem?
>> >>
>> >> I have a table with several rows. I want to create a new table, with
>> >> exactly
>> >> the same structure, but has only a subset of the rows in the original
>> >> table.
>> >>
>> >> I have been able to do this using the following code but wonder if
>> >> there
>> >> is
>> >> a more efficient/fast way of doing it.
>> >>
>> >> Dim subsetTable As DataTable = sourceTable.Clone()
>> >>
>> >> For Each row As DataRow In sourceTable.Select("[Name] = 'John'")
>> >> Dim newRow As DataRow = subsetTable.NewRow()
>> >> For i As Integer = 0 To sourceTable.Columns.Count - 1
>> >> newRow(i) = row(i)
>> >> Next
>> >> subsetTable.Rows.Add(newRow)
>> >> Next
>> >>
>> >> Using the code above I can a new table which has all the rows from the
>> >> source table with the field "Name" set to "John".
>> >>
>> >> Can anybody suggest a more efficient/fast way of doing this?
>> >>
>> >> Thanks in advance
>> >

>>
>>
>>



  Reply With Quote
Old 22-09-2006, 06:04 AM   #6
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Copying part of a table

G.

You can very simple use the overloaded new 2005 method from the dataview
ToTable for this.

I hope this helps,

Cor

"G .Net" <nodamnspam@email.com> schreef in bericht
news:zbidnU3ARoVapo_YnZ2dnUVZ8t2dnZ2d@pipex.net...
> Hi
>
> I'm wondering if anybody can help me with the following problem?
>
> I have a table with several rows. I want to create a new table, with
> exactly the same structure, but has only a subset of the rows in the
> original table.
>
> I have been able to do this using the following code but wonder if there
> is a more efficient/fast way of doing it.
>
> Dim subsetTable As DataTable = sourceTable.Clone()
>
> For Each row As DataRow In sourceTable.Select("[Name] = 'John'")
> Dim newRow As DataRow = subsetTable.NewRow()
> For i As Integer = 0 To sourceTable.Columns.Count - 1
> newRow(i) = row(i)
> Next
> subsetTable.Rows.Add(newRow)
> Next
>
> Using the code above I can a new table which has all the rows from the
> source table with the field "Name" set to "John".
>
> Can anybody suggest a more efficient/fast way of doing this?
>
> Thanks in advance
>



  Reply With Quote
Old 25-09-2006, 03:56 PM   #7
G .Net
Guest
 
Posts: n/a
Default Re: Copying part of a table

Thanks Cor

"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
news:ezEy0Rg3GHA.1796@TK2MSFTNGP06.phx.gbl...
> G.
>
> You can very simple use the overloaded new 2005 method from the dataview
> ToTable for this.
>
> I hope this helps,
>
> Cor
>
> "G .Net" <nodamnspam@email.com> schreef in bericht
> news:zbidnU3ARoVapo_YnZ2dnUVZ8t2dnZ2d@pipex.net...
>> Hi
>>
>> I'm wondering if anybody can help me with the following problem?
>>
>> I have a table with several rows. I want to create a new table, with
>> exactly the same structure, but has only a subset of the rows in the
>> original table.
>>
>> I have been able to do this using the following code but wonder if there
>> is a more efficient/fast way of doing it.
>>
>> Dim subsetTable As DataTable = sourceTable.Clone()
>>
>> For Each row As DataRow In sourceTable.Select("[Name] = 'John'")
>> Dim newRow As DataRow = subsetTable.NewRow()
>> For i As Integer = 0 To sourceTable.Columns.Count - 1
>> newRow(i) = row(i)
>> Next
>> subsetTable.Rows.Add(newRow)
>> Next
>>
>> Using the code above I can a new table which has all the rows from the
>> source table with the field "Name" set to "John".
>>
>> Can anybody suggest a more efficient/fast way of doing this?
>>
>> Thanks in advance
>>

>
>



  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off