简体   繁体   中英

How to close a table with the VFP OLE DB Provider

I'm writing some software in C# that will perform queries on Visual FoxPro datafiles over time. I need to be able to close tables, especially those that are opened exclusively ("Mode=Share Exclusive" in the Connection String), but the only way I can seem to do that is by closing the entire OleDbConnection.

The VFP OLE DB Provider supports some syntax from VFP itself, but commands that would close a table in standard VFP, such as USE, either do not work, or throw an exception (I can't recall which command threw an exception at the moment).

Currently, each instance of a class has its own OleDbConnection property, so that if I need to, I'm able to close it and release the table it works on. While this works, it's not optimal, and I'd prefer to have 1 connection instance.

// Elsewhere, ideally one connection for the entire process,
// if I'm able to release locks
public OleDbConnection Connection { get; } = new OleDbConnect();



// ...


if(Connection.State == ConnectionState.Closed) {
    Connection.ConnectionString = "Provider=vfpoledb.dll;Data Source=J:\\epdata\\;Mode=Share Exclusive"
    Connection.Open();
}
OleDbCommand cmd = new OleDbCommand("SET DELETED OFF", Connection);
cmd.ExecuteNonQuery();

cmd.CommandText = "SELECT * FROM tran";
OleDbDataAdapter adap = new OleDbDataAdapter(cmd);
DataTable table = new DataTable();
adap.Fill(table);


// Something here to release the lock on the tran table

This works to the point where it will select the data and lock the table for exclusive use by the current OleDbConnection, but I cannot find any way to release that exclusive lock without closing the entire connection, which I'm trying to avoid having to do.

I don't see a point opening a connection exclusively and then using an adapter to fill a table, after which you want to release lock. You don't need a connection in the first place, adapter opens and closes the connection as needed:

DataTable table = new DataTable();
new OleDbDataAdapter("SELECT * FROM tran", 
      @"Provider=vfpoledb;Data Source=J:\epdata;Deleted=off")
  .Fill(table);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM