Updating FoxPro DBF From C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to Delete records from Fox Pro Free Tables (.DBF) using
C#. I am able to insert and view data with no problems but when I try
to delete - no luck. It appears that I need to run a "Pack" command
but I receive the error "File must be opened exclusively." on the Pack
Command. The code is below and the first command "Delete From
Progress" marks all rows for deletion but does not remove them. I've
tried Exclusive=On but still get the same error.


public FoxProConnector(string tableName){
this.DBFTableName = tableName;
OleDbConnection oConn = new OleDbConnection(@"Provider=VFPOLEDB.1;Data
Source=c:\\" + tableName +";Mode=ReadWrite;");
oConn.Open();
//OleDbCommand oCom = new OleDbCommand("Insert into
Progress(nProgress) values(990)");
OleDbCommand oCom = new OleDbCommand("Delete From Progress");
oCom.Connection = oConn;
oCom.ExecuteNonQuery();
oCom = new OleDbCommand("PACK");
oCom.Connection = oConn;
oCom.ExecuteNonQuery();
}

Unhandled Exception: System.Data.OleDb.OleDbException: File must be
opened exclusively.
**Removed Several Lines**
at OrderHeader.Form1.Main() in c:\documents and settings\scottm\my
documents\visual studio projects\orderheader\form1.cs:line 126
 
aaa,

Technically, when you issue a delete, the rows are flagged with a delete
flag. You aren't going to see those rows on subsequent selects unless the
runtime for foxpro has "SET DELETE ON". If you want to pack the tables, you
shouldn't do this after every database operation, as it can be costly. I
would recommend issuing a pack as part of a regular maintinence and not on
every database operation.

Hope this helps.
 
In
Nicholas Paldino said:
Technically, when you issue a delete, the rows are flagged with a
delete flag. You aren't going to see those rows on subsequent
selects unless the runtime for foxpro has "SET DELETE ON".

Hi Nicholas,

The FoxPro command SET DELETED ON hides deleted records. It corresponds to
the Deleted checkbox in the ODBC driver setup - in other words, when the box
is checked, DELETED is ON and deleted records are hidden. A little
counterintuitive.
 
In
aaa said:
I am trying to Delete records from Fox Pro Free Tables (.DBF) using
C#. I am able to insert and view data with no problems but when I try
to delete - no luck. It appears that I need to run a "Pack" command
but I receive the error "File must be opened exclusively." on the Pack
Command.

When a FoxPro record is marked for deletion you can consider it to be
deleted, as long as you're running with Deleted set ON. The reasons to
permanently remove records are few - if your tables are becoming impossibly
large, or if you need to re-use a primary key. IMHO a key value that's
reused isn't really a PK. Is there some specific reason you need to PACK the
table? Be aware that having exclusive use of the table can only be done when
the users are out of the system.
The code is below and the first command "Delete From
Progress" marks all rows for deletion but does not remove them. I've
tried Exclusive=On but still get the same error.

Um - which error - do you get an error from the Delete command, an error
when you try to PACK, or an error when you try some operation that requires
the records to be permanently removed?
....
oCom = new OleDbCommand("PACK");
Unhandled Exception: System.Data.OleDb.OleDbException: File must be
opened exclusively.

The following article describes how to PACK a table via ODBC. It should help
you get started doing it through OLE DB:

HOWTO: Pack a Table Through the Visual FoxPro ODBC Driver (VFPODBC.dll)
http://support.microsoft.com/default.aspx?scid=kb;en-us;234756
 
Back
Top