简体   繁体   English

尝试打开和OLE DB连接时获取“ System.InvalidOperationException”

[英]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 VFPVFP实体框架ADO.Net的VFP客户端

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试foreach遍历列表时获取System.InvalidOperationException吗? - Getting System.InvalidOperationException when Trying to foreach loop over a List? 尝试保存在数据库中时 System.InvalidOperationException - System.InvalidOperationException when trying to save in database 使用ApiController获取列表时出现System.InvalidOperationException - System.InvalidOperationException when getting a List with an ApiController c#System.InvalidOperationException:'连接已经打开。 - c# System.InvalidOperationException: 'The connection is already open.' System.InvalidOperationException: '连接必须有效且打开。' - System.InvalidOperationException: 'Connection must be valid and open.' 我收到错误消息:System.InvalidOperationException:“连接已打开。” - I get an error: System.InvalidOperationException: 'The connection is already open.' 更改用于连接数据库C#的连接字符串时获取“ System.InvalidOperationException” - Getting a 'System.InvalidOperationException' when changing the connection string for connecting to a database C# 尝试通过xpath查找元素时出错 - System.InvalidOperationException:String无法强制转换为WebElement? - Getting error when trying to find element by xpath - System.InvalidOperationException: String cannot be cast to WebElement? 尝试运行 web 应用程序时出现 System.InvalidOperationException - I get System.InvalidOperationException when trying to run the web app System.InvalidOperationException:ExecuteReader 需要打开且可用的连接。 连接的电流 state 已打开 - System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is open
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM