简体   繁体   中英

The 'VFPOLEDB' provider is not registered on the local machine exceptions persists

Please read remark below:

I'm trying to connect to a fox pro database and it is very frustrating.

Following some of the answers in StackOverflow, I did the following:

  • Installed the vfpoledb from Microsoft
  • I sat the platform target to x86 in the project properties.
  • Ran Visual Studio/Application in Administrator mode
  • Database file is unblocked.

It still throws an exception that the provider is not registered on this machine.

I'm running Windows 10 Pro X64 and Visual Studio Community 2017.

 public class FoxProHandler
    {
        static OleDbConnection connector = default(OleDbConnection);
        static bool isConnected = false;
        public static string ConnectionString { get; private set; }
        private FoxProHandler()
        {

        }

        public static void SetConnectionString(string Value)
        {
            ConnectionString = Value;
        }
        public Dictionary<string, string> GetValues(string PartNumberValue, Dictionary<string, string> Mapper)
        {
            throw new Exception();
        }
        public static void Connect()
        {
            if (string.IsNullOrWhiteSpace(ConnectionString))
            {
                throw new Exception("Connection string is empty");
            }
            if (isConnected == false)
            {
                try
                {
                    Console.WriteLine($"CREATING DB CONNECTOR");
                    connector = new OleDbConnection(ConnectionString);
                    Console.WriteLine($"CREATING DB CONNECTOR OBJECT");                 
                    connector.Open();
                    Console.WriteLine($"OPENED CONNECTED SUCCESSFULLY");
                    isConnected = true;
                }
                catch (Exception e)
                {

                    Console.WriteLine($"{e.Message} {e.StackTrace}"); 
                }

            }

        }
    }
    class Program
    {

        static void Main(string[] args)
        {
           string fileName = System.IO.Path.Combine(@"C:\Users\Amen\Downloads", "ADMQH20X.DBC"); 
           string connectionString = $"Provider=\"VFPOLEDB\";Data Source=\"{fileName}\";Collate=Machine;"; 
           FoxProHandler.SetConnectionString(connectionString);
           FoxProHandler.Connect();
           Console.ReadKey(); 

        }
    }

Any help would be highly appreciated.

Remark: After procuring the necessary files (.dbf), the application still throws an exception during a debug session but not when running the application from explorer.

Can you please test this code (provided you have or created c:\\temp):

void Main()
{
    if (IntPtr.Size == 8)
    {
        Console.WriteLine("Sorry this is not going to work in 64 bits");
    }
    else
    {
        DataTable tbl=new DataTable();
        using (OleDbConnection con = new OleDbConnection(@"Provider=VFPOLEDB;Data Source=c:\Temp"))
        {
            con.Open();
            new OleDbCommand("create table myTest (id int, dummy c(10))",con).ExecuteNonQuery();
            var cmd = new OleDbCommand(@"insert into myTest (id, dummy) values (?,?)",con);
            cmd.Parameters.Add("id", OleDbType.Integer);
            cmd.Parameters.Add("dum", OleDbType.VarChar,10);

            for (int i = 0; i < 10; i++)
            {
                cmd.Parameters["id"].Value = i + 1;
                cmd.Parameters["dum"].Value = $"Dummy#{i+1}";
                cmd.ExecuteNonQuery();
            }

            tbl.Load(new OleDbCommand("select * from myTest",con).ExecuteReader());
        }
        foreach (DataRow row in tbl.Rows)
        {
            Console.WriteLine($"{(int)row["id"]} : {(string)row["Dummy"]}");
        }
    }
    Console.ReadLine();
}

Or if you edit in Notepad and compile with csc:

using System;
using System.Data;
using System.Data.OleDb;
namespace Test
{
  class Test
  {

static void Main()
{
    if (IntPtr.Size == 8)
    {
        Console.WriteLine("Sorry this is not going to work in 64 bits");
    }
    else
    {
        DataTable tbl=new DataTable();
        using (OleDbConnection con = new OleDbConnection(@"Provider=VFPOLEDB;Data Source=c:\Temp"))
        {
            con.Open();
            new OleDbCommand("create table myTest (id int, dummy c(10))",con).ExecuteNonQuery();
            var cmd = new OleDbCommand(@"insert into myTest (id, dummy) values (?,?)",con);
            cmd.Parameters.Add("id", OleDbType.Integer);
            cmd.Parameters.Add("dum", OleDbType.VarChar,10);

            for (int i = 0; i < 10; i++)
            {
                cmd.Parameters["id"].Value = i + 1;
                cmd.Parameters["dum"].Value = $"Dummy#{i+1}";
                cmd.ExecuteNonQuery();
            }

            tbl.Load(new OleDbCommand("select * from myTest",con).ExecuteReader());
        }
        foreach (DataRow row in tbl.Rows)
        {
            Console.WriteLine($"{(int)row["id"]} : {(string)row["Dummy"]}");
        }
    }
    Console.ReadLine();
}

}
}

Save it, say VFPOLEDBTest.cs and compile with:

csc VFPOLEDBTest.cs /platform:x86

and run:

VFPOLEDBTest.exe

Output:

d:\Academy>VFPOLEDBTest.exe
1 : Dummy#1
2 : Dummy#2
3 : Dummy#3
4 : Dummy#4
5 : Dummy#5
6 : Dummy#6
7 : Dummy#7
8 : Dummy#8
9 : Dummy#9
10 : Dummy#10

您尝试使用: build as x86 ,在 cmd regsvr32 "C:\\Program Files (x86)\\Common Files\\System\\Ole DB\\vfpoledb.dll" (安装的 vpf 驱动程序)中运行命令

It is very simple indeed, you had extra quotes in connection string that is causing the problem:

 static void Main(string[] args)
        {
           string fileName = System.IO.Path.Combine(@"C:\Users\Amen\Downloads", "ADMQH20X.DBC"); 
           string connectionString = $"Provider=VFPOLEDB;Data Source={fileName}";
           DataTable tbl = new DataTable(); 
           using(OleDbConnection con = new OleDbConnection(connectionString))
           using(OleDbCommand cmd = new OleDbCommand("select * from myTable", con))
           {
              con.Open();
              tbl.Load(cmd.ExecuteReader());
           }

           // do Something with datatable
        }

From another answer on VFP and OleDb

A few things... The provider is "VFPOLEDB.1" Second, the connection string should point to a PATH, not the specific table. Finally, once you have a PATH connection, you can query from any SUB-PATH/FOLDER within that.

var connectionString = @"Provider=VFPOLEDB.1;Data Source=c:\\YourDataPath\\SomeSubFolder;";

Then your queries can be as simple as

select YT.* from YourTable YT where...

and if you have sub-folders, you can do...

select YT.* 
    from 
       YourTable YT
         JOIN AnotherSubFolder\SomeOtherTable SOT
            on YT.SomeKey = SOT.SomeKey
    where...

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