[英]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.我正在 C# 中编写一些软件,这些软件将随着时间的推移对 Visual FoxPro 数据文件执行查询。 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.
我需要能够关闭表,尤其是那些以独占方式打开的表(连接字符串中的“模式=共享独占”),但我似乎可以这样做的唯一方法是关闭整个 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). VFP OLE DB Provider 支持 VFP 本身的一些语法,但是会在标准 VFP 中关闭表的命令,例如 USE,要么不起作用,要么抛出异常(我现在不记得哪个命令抛出了异常)。
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.目前,class 的每个实例都有自己的 OleDbConnection 属性,因此如果需要,我可以关闭它并释放它工作的表。 While this works, it's not optimal, and I'd prefer to have 1 connection instance.
虽然这可行,但它不是最优的,我希望有 1 个连接实例。
// 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.这工作到它将 select 数据并锁定表以供当前 OleDbConnection 独占使用,但我找不到任何方法来释放该独占锁而不关闭整个连接,我试图避免这样做.
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.