View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Jamie Collins Jamie Collins is offline
external usenet poster
 
Posts: 593
Default Overwriting existing data in an Excel worksheet using ADO.Net

"Joel Gordon" wrote ...

The first time the code is run (and
the excel file does not exist) it runs fine. If it is run again then
the drop table leaves the named range alone and hence the create table
fails.


I don't think that is correct. I think your code is failing to call
its DeleteTable method on the second pass because it wrongly assumes
the table doesn't exist.

You are using GetOleDbSchemaTable to detect whether the table exists
and only try to drop the table if TableExists returns true. However, a
'named range' that contains no data rows does not show up as a table
in the schema (non-.NET readers should note that the same behaviour
results when using GetOpenSchema in ADO 'classic'). Regardless, the
table may still be used and so, of course, may be dropped.

The approach I usually take is to try to DROP the table anyhow: if it
exists then it gets dropped; if it doesn't exist I ignore the error.

Jamie.

--