0

currently the system is creating multiples OledbConnections and the process is taking a long time to finish, it sums up to this:

C# code:

  1. Select all person from table A.
  2. FOR EACH person: Do a SELECT to see if ID 'x' from table A exists in table B.
  3. FOR EACH person: INSERT with the ID from table A or a new ID (if that ID already exists in table B)

All these INSERTS are creating a new OledbConnection for each person, and for example 3k persons, it is taking too long.

It would be easier if I didn't have to deal with the IDs, but I'm not finding a good way to do this with VFP.

is that a way to Bulk INSERT this, or improve the performance?

2 Answers2

0

if id in table2 is autonumber:

if exists(select * from table2 where id = idtable1)

    insert into table2(field1,field2...

else

    insert into table2(ID,field1,field2...
Ibra
  • 192
  • 11
0

I don't understand why you need to use multiple OleDbConnections for this. A single connection would do:

string sql = @"insert into tableB (id, f1, f2, f3)
select getNextId('tableB'), f1, f2, f3
from tableA
where exists (select * from tableA where tableA.id = tableB.id)";

using (OleDbConnection cn = new OleDbConnection(@"Provider=VFPOLEDB;Data Source=c:\MyDataFolder"))
using (OleDbCommand cmd = new OleDbCommand(sql, cn))
{
  cn.Open();
  cmd.ExecuteNonQuery();
  cmd.CommandText = @"insert into tableB (id, f1, f2, f3)
select id, f1, f2, f3
from tableA
where not exists (select * from tableA where tableA.id = tableB.id)";
  cmd.ExecuteNonQuery();
  cn.Close();
}

The SQLs used may not be right, because from your definition, it is not clear what you are really trying to do.

And BTW, you are not telling about the version, if Foxpro tables mean VFP tables then there is autoinc int field (but not necessary you can always have your own getNextId function as in the sample).

Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39