chirag pathak chirag pathak - 3 months ago 23
C# Question

How to execute foxpro 2.6(DOS) commands from C# that needs exclusive access to table?

This is my code, and I want to delete all the data of the FoxPro tables named as SMS_DATA, I tried ZAP command, as well as also tried DELETE ALL and PACK command with the USE -table-name, but it doesn't working, and gives the exception that:

System.Data.OleDb.OleDbException (0x80004005): File must be opened exclusively.


This is my code:

string USETBL ="EXECSCRIPT([USE SMS_DATA && Open SMS_DATA table])";
string DELETE="EXECSCRIPT([DELETE ALL])";
string PACK="EXECSCRIPT([PACK])";

OleDbConnection con = new OleDbConnection(conStr);
try
{

OleDbCommand cmd = new OleDbCommand(USETBL, con);
OleDbCommand cmd1 = new OleDbCommand(DELETE, con);
OleDbCommand cmd2 = new OleDbCommand(PACK, con);

con.Open();
err.log("connection opened");
cmd.ExecuteNonQuery();
err.log("table in use");
cmd1.ExecuteNonQuery();
err.log("delete executed.");
cmd2.ExecuteNonQuery();
err.log("pack executed.");
con.Close();
}
catch (Exception e)
{
err.log("Exception:-" + e);
}

Answer

Your first line should be:

EXECSCRIPT([USE SMS_DATA EXCLUSIVE && Open SMS_DATA table]);

for safety I would also change the next two lines to:

string DELETE="EXECSCRIPT([DELETE ALL IN SMS_DATA])";       
string PACK="EXECSCRIPT([PACK IN SMS_DATA])";
Comments