DataTable creating question?

R

Rich

Hello,

I have a large table in Sql Server that I want to populate
from a VB.Net app. I have 2 data sources that are exactly
the same structure but data from different geographic
locations. I want to populate 2 separate dataTables in my
VB.Net app (simultaneously on different threads) with the
respective source data and then I want to merge the data
from these 2 separate dataTables into a dataTable that I
create from a Sql DataAdapter. I know how to use a Sql
Adapater (sort of)

Dim SqlDA As SqlDataAdapter = New SqlDataAdapter
....
sqlDA.SelectCommand = New SqlCommand("Select * From
tbl1",conn)
....
sqlDA.Fill(dataSet1, "tbl1") - creates a dataTable with
the correct schema.

I tried doing the following with my 2 separate datatables
Dim dt1 As DataTable = ds1.Tables("tbl1")
Dim dt2 As DataTable = ds1.Tables("tbl1")

But dt1 and dt2 turned out to be the same table. If I
place from 1 to 1000 records in dt1 and 1001 to 2000 recs
in dt2 (simultaneously using multithreading), tbl1
contains 4000 recs, not 2000. I would like to create dt1
with the same schema as tbl1 and dt2 also. Then I want to
transfer the data from dt1 and dt2 into tbl1. In my
situation, simultaneously pick up data and place it into
tbl1 where I end up with 2000 records, not 4000. What is
the best way to do this? How do I transfer the data from
dt1 and dt2 into tbl1 (not even worried about updating the
Sql Server table for now - just end up with 2000 records
in tbl1 from dt1 and dt2).

TIA,
Rich
 
G

GVaught

Create a UNION query between the two data tables first and then use the
UNION query to fill your data table.
 
R

Rich

Thanks. I could give that a try (one more question - if
I may). But one other thing I just discovered that seems
to work is the .copy method:

Dim dt1 As DataTable = ds1.Tables("tbl1").Copy
Dim dt1 As DataTable = ds1.Tables("tbl1").Copy

This gives me 2 separate tables like I wanted. But when I
want to Update the Sql Server table ("tbl1") how do I go
about doing that?

sqlDA.Update(ds1, "tbl1")? how do I tell the adapter to
fill the sql Table ("tbl1") with data from dt1? and then
from dt2? pseudocode here:

ds1.Tables("tbl1").Copy.Rows(dt1)
sqlDA.Update(ds1, "tbl1")
ds1.Clear()
ds1.Tables("tbl1").Copy.Rows(dt2)
sqlDA.Update(ds1, "tbl1")

Is there something like this that I could do?

Thanks for your reply,
Rich
 
R

Rich

Silly silly me! The solution to my problem is the
problem. Instead of declaring a bunch of dataTables based
on a sqlDataAdapter table, I should declare a bunch of
sqldataAdapters on the same sql Server Table. Now, why
didn't I think of that? Oh wait, I did! But at least I
learned about the copy method of dataTables - so not a
complete waste of thinking.
 

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