[英]Getting “System.InvalidOperationException” When trying to open and OLE DB connection
I'm trying to connect to a Visual FoxPro Database that I downloaded for testing purposees using OLE DB, this is my code: 我正在尝试使用OLE DB连接到为测试目的而下载的Visual FoxPro数据库,这是我的代码:
private void ReadMyData() {
String dbProvider = "Provider=.NET Framework Data Provider for OLE DB;";
String dbSource = "Data Source=VFPOLEDB.C:\\USERS\\X\\DESKTOP\\LOG;";
String connectionString = dbProvider + dbSource;
OleDbConnection FPDBConn = new OleDbConnection(connectionString);
OleDbCommand FPDBCmd;
string sql = null;
sql = "Select * from clogbook";
try {
FPDBConn.Open();
FPDBCmd = new OleDbCommand(sql, FPDBConn);
OleDbDataReader FPDBReader = FPDBCmd.ExecuteReader();
while (FPDBReader.Read()) {
Debug.Write(FPDBReader.GetInt32(0) + ", " + FPDBReader.GetString(1));
}
FPDBReader.Close();
FPDBCmd.Dispose();
FPDBConn.Close();
} catch (Exception ex)
{
Debug.Write("Can not open connection ! " + ex);
}
}
The problem is I'm getting "System.InvalidOperationException: The '.NET Framework Data Provider for OLE DB' provider is not registered on the local machine." 问题是我收到“ System.InvalidOperationException:“用于OLE DB的.NET Framework数据提供程序”提供程序未在本地计算机上注册。”
I was googling about it and came across this msdn page that says that the exception "InvalidOperationException" has the condition "The connection is already open." 我在搜索它时遇到了该msdn 页面 ,该页面显示异常“ InvalidOperationException”的条件为“连接已打开”。 Which I don't think is happenning in this case.
在这种情况下,我认为这没有发生。
How can I get this to work? 我该如何工作?
PS: Accepting different suggestions to make this connection. PS:接受不同的建议以建立此连接。
For connecting to VFP, I would strongly suggest and download the Visual Foxpro OleDb Provider . 为了连接到VFP,我强烈建议并下载Visual Foxpro OleDb Provider 。
Then, the connection string would be formatted something like... 然后,连接字符串将被格式化为...
string connectionString = @"Provider=VFPOLEDB.1;Data Source=C:\YourDirectory\";
The connection should point to the PATH where the tables are located as you currently have... many times people try to connect to the specific TABLE, but once the path is established, you can query from ANY table that is in that folder (or child folder if so available). 连接应该指向表的当前位置所在的PATH ...很多人尝试连接到特定的TABLE,但是一旦建立了路径,就可以从该文件夹中的ANY表中查询(或子文件夹(如果有)。
Should get you a bit further... 应该让您更进一步...
You should download and use VFPOLEDB as DRapp suggested. 您应该按照DRapp的建议下载并使用VFPOLEDB。 Here is a sample using the sample database Northwind data:
这是使用示例数据库Northwind数据的示例:
private void ReadMyData()
{
var builder = new OleDbConnectionStringBuilder();
builder.Provider = "VFPOLEDB";
builder.DataSource = @"C:\Program Files (x86)\Microsoft Visual FoxPro 9\Samples\Northwind";
string sql = "Select * from Customers where Country like ?";
using (OleDbConnection con = new OleDbConnection(builder.ConnectionString))
using (OleDbCommand cmd = new OleDbCommand(sql, con))
{
cmd.Parameters.AddWithValue("@country", "USA");
try
{
con.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
Debug.WriteLine("{0}, {1}",
reader["CustomerId"],
reader["CompanyName"]);
}
con.Close();
}
catch (Exception ex)
{
Debug.Write("Can not open connection ! " + ex);
}
}
}
Note that parameter is not named but positional. 请注意,参数不是命名的,而是位置的。
PS: Also check Tom Brothers' Linq To VFP , VFP Entity Framework and VFP Client for ADO.Net . PS:还请检查Tom Brothers的Linq To VFP , VFP实体框架和ADO.Net的VFP客户端 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.