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:
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