如何从需要独占访问表的C#执行foxpro 2.6(DOS)命令?

时间:2016-08-31 04:46:00

标签: c# oledb foxpro

这是我的代码,我想删除名为SMS_DATA的FoxPro表的所有数据,我尝试了 ZAP 命令,还尝试了全部删除和带有 USE -table-name的 PACK 命令,但它不起作用,并提供以下异常:

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

这是我的代码:

     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);
                       }

1 个答案:

答案 0 :(得分:2)

你的第一行应该是:

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

为了安全起见,我还将接下来的两行更改为:

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