This question already has answers here:
Proper way of getting a data from an Access Database
(4 answers)
Closed 1 year ago.
I got a very old mdb-database.
I want to load all tables from the database into a C# DataTable.
If possible the final tool should be stand-alone and don't need to install additional software.
Environment
.NET 5.0
Visual Studio 2019
Windows 10 - 64 bit
Office 365 - 32 bit
Questions:
How does the code for this looks like?
Which packages do I have to add/install?
Are there other dependencies?
Code:
string dbLocation = "DatabaseName.mdb";
string tableName = "TableName";
string connString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={dbLocation}";
DataTable results = new DataTable();
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand cmd = new OleDbCommand($"SELECT * FROM {tableName}", conn);
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(results);
}
Conditions:
In order to the Office-Version that is installed (32/64 bit) you have to install Microsoft Access Database Engine 2010/2016 Redistributable (for me it was 2010).
Most office installations are 32 bit.
Microsoft Access Database Engine 2010 Redistributable
Microsoft Access Database Engine 2016 Redistributable
If you choose the right version an still get an error trying to install in silent mode
accessdatabaseengine.exe /quiet
Add package System.Data.OleDb
Target plattform of the application has to be set to x86/x64 according to the other packages that are used
Still not solved:
Actually you need to have Microsoft Access Database Engine Redistributable installed. If there exist any other methode without this, please let me know.
Related
I have a C# exe, called 'start.exe' that is when run will check a SQL database and copy a file to a folder if certain criteria are met.
This works on most computers.
But on some computers when I try to run it I get the following error message.
System.invalidOperationException: The 'Microsoft.ACE.OLEDB.12.0' is not registered on the local machine
To fix this I created a setup project that should add the missing .dll.
I created the set up project, went to the applications folder, added the primary output of the project.
I then ran a batch build on all the files and copied the msi and setup .exe files to a problem computer and ran it.
It adds System.Net.Http.dll and 'start.exe' to the computer.
When I run the exe, it still gives me the same error.
I believe the message box comes from these lines of code.
try
{
using (var conn = new OleDbConnection(#"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=" + CurFeSource + ";"))
{
conn.Open();
using (var cmdBE = new OleDbCommand("SELECT value FROM Tab1", conn))
{
OleDbDataReader rdrBE = cmdBE.ExecuteReader();
rdrBE.Read();
be_version = (rdrBE.GetString(0));
rdrBE.Close();
}
using (var cmdFE = new OleDbCommand("SELECT Col FROM Links", conn))
{
OleDbDataReader rdrFE = cmdFE.ExecuteReader();
rdrFE.Read();
fe_version = (rdrFE.GetString(0));
rdrFE.Close();
}
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return false;
}
Edit:
As SQLGeorge said, I need to install Microsoft.ACE.OLEDB.12.0.
But how do I add it to a setup project.
The Microsoft.ACE.OLEDB.12.0 must be downloaded from Microsoft:
https://www.microsoft.com/en-US/download/details.aspx?id=13255
Excerpt From Microsoft:
This download will install a set of components that facilitate the transfer of data between existing Microsoft Office files such as Microsoft Office Access 2010 (*.mdb and .accdb) files and Microsoft Office Excel 2010 (.xls, *.xlsx, and *.xlsb) files to other data sources such as Microsoft SQL Server. Connectivity to existing text files is also supported. ODBC and OLEDB drivers are installed for application developers to use in developing their applications with connectivity to Office file formats.
As noted by Microsoft:
As a general replacement for Jet (If you need a general replacement for Jet you should use SQL Server Express Edition).
This question already has answers here:
'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine
(39 answers)
Closed 7 years ago.
I'm getting the following error
"The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine."
while reading the data from Excel file using OLEDB Data Reader.
Its not working in both 32 and 64 bit machine. Currently my machine having MS office 2013 and VS 2010.
string Connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;MAXSCANROWS=15;READONLY=FALSE\"";
OleDbConnection con = new OleDbConnection(Connection);
OleDbCommand command = new OleDbCommand();
DataSet ds = new DataSet();
command.CommandText = sql;
command.CommandType = CommandType.Text;
command.Connection = con;
command.Connection.Open();
OleDbDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
You need to change the Solution Platform from "Any CPU" to "x86". The steps are given below:
Right click on the Solution File in Solution Explorer
Click on the Configuration Manager.
Click on the Active Platform Drop down, if x86 is already there then select that, else click on New.
Select x86 from the new platform dropdown:
Compile and run your application.
If you are still facing the issue, then try installing Office System Driver. from follow site
http://www.microsoft.com/en-us/download/details.aspx?id=23734
Please refer to the following link for a possible solution:
On 64-bit Windows and 64-bit Office (2010, 2013) environments, there are many reports on this error...
...try using 2007 Office System Driver: Data Connectivity Components
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
I know the common fix for this which is to install:
Microsoft Access Database Engine 2010 Redistributable
Or
2007 Office System Driver: Data Connectivity Components
Both are installed on my local PC. This is the code which I have
OleDbConnection conn = new OleDbConnection();
string fileName = "test.xlsx";
try
{
string connectString = String.Format(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR={1};'",
fileName, "YES");
conn.ConnectionString = connectString;
conn.Open(); //exception is thrown here !!!
OleDbCommand comm = new OleDbCommand();
comm.CommandText =
string.Format("CREATE TABLE [{0}] ", "Test");
comm.Connection = conn;
comm.ExecuteNonQuery();
OleDbDataAdapter ad = new OleDbDataAdapter(
string.Format("SELECT * FROM [{0}]", "Test"), conn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(ad);
}
catch(Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
I try this code in other projects on my local machine and everything is working. I have project which creates excel exports and I don't have this problem.
The problem is in the project I don't understand how to fix it. Also on the current project I create one new .aspx page and in Page_Load put only this code, same exception.
Additional information: this project was written on vs 2008, convert to 2010 and after that used in vs 2012. Everything was working in the project till now.
Also same thing for JET connection string !
EDIT
After the answer of Joe I see that this project is run on 64bitProcess:
bool test = Environment.Is64BitProcess; //return true.
My driver is for 32bit. What I can do now, can I change the environment process. I can't install the driver for 64bit process because I have installed Office 2010 x32.
The OLEDB drivers for 32-bit and 64-bit applications are different.
If you only have the 32-bit driver installed, then 64-bit applications that attempt to use it will get this error. Similarly, if you have only the 64-bit version installed, then 32-bit applications that attempt to use it will get this error.
You say:
I try this code in other projects on my local machine and everything is working
Ergo, at least one of the two must be correctly installed.
To understand what's happening you could examine Environment.Is64BitProcess in both the application that works, and the one that doesn't. That will tell you which version is missing.
Then download and install the 32-bit or 64-bit version that's missing from:
http://www.microsoft.com/en-us/download/details.aspx?id=13255
You need AccessDatabaseEngine.exe (32-bit) or AccessDatabaseEngine_64.exe (64-bit)
Note that you may need to specify the provider as 'Microsoft.ACE.OLEDB.14.0' for the Office 2010 version (12.0 was for Office 2007).
After some hours I found a solution:
What were the steps, first I try to run the project on x86 - Properties/Build/Platform Target. Exception was thrown that I can't rebuild, because a registry was false. I create the registry. How to do it:
In notepad file paste this code and save it like reg file. Make the file name something to remember why you have it in the future(Fusion.reg).
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion]
"EnableLog"=dword:00000001
After that I had a problem because not all of my assemblies was readable by the application pool. Solution about that was going to IIS/Application Pools/Application Pool 4.0/General/Enable 32- Bit Applications. After that restart the IIS, close the project and open it again and everything was working with 32bit version.
Can someone help me out please, as google is not providing the answers.
I've got a SharePoint 2007 setup which uses SQL Server 2008 R2 SSAS OLAP cubes via some web parts.
As a C# developer, Sharepoint is a nightmare, so I decided I needed to try to get to grips with just C# and OLAP interaction. My cubes all exist, and are working, so all I needed to do was create a simple C# App to get it all straight in my mind.
I've downloaded Microsoft.AnalysisServices v10.0.0.0 and I can see it sitting happily in my GAC, but I can't add a reference from within my Visual Studio 2010 C# 4.0 project. It's just notappearing. I've tried setting the app to use 3.5, but still no joy.
Any clues?
Have you added the reference for Microsoft.AnalysisServices.AdomdClient.dll located in C:\Program Files\Microsoft.NET\ADOMD.NET\100
You could also use the nuget package manager. Type this in the console
Deprecated version (does not exist anymore):
install-package Microsoft.AnalysisServices.AdomdClient
New version:
Install-Package Microsoft.AnalysisServices.AdomdClient.retail.amd64
I think you need to reference the file directly, rather than through the GAC. It should be located in C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies
AdomdConnection steps
AdomdConnection con = new AdomdConnection("connectionstring"); // connect DB
con.Open();
AdomdCommand cmd = new AdomdCommand("MDX query", con); //query
AdomdDataReader reader = cmd.ExecuteReader(); //Execute query
while (reader.Read()) // read
{
Data dt = new Data(); // custom class
dt.Gender = reader[0].ToString();
dt.Eid = reader[1].ToString();
dt.salary = reader[2].ToString();
data.Add(dt);
}
I am using OLEDB to connect .mdb/.accdb file.
Here is what I have so far:
using System.data.OLEDB;
class dbconne
{
.....
.........
.......
.
.
.
.
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\db1.mdb");
OleDbCommand aCommand = new OleDbCommand("select * from emp_test", aConnection);
aConnection.Open();
....
....
.
.
.
}
When I compile this code I don't get any compile errors. But it gives me a runtime error:
"Microsoft.Jet.OLEDB.4.0" provider is not registered on the local machine
How can I fix this?
See if you have Microsoft Data Access Components installed.
Jet Oledb provider is part of the above components & should be present for you to connect to the database using the appropriate provider.
On a side note, this should be a part of the operating system (I guess).
Is this your development machine, with .net framework installed?
Microsoft.Jet OLEDB 4.0 is part of Microsoft Access or the Microsoft Access Runtime. You could try installing Jet Service Pack 8, but if that doesn't work, you will need Access or the Access Runtime. Jet 4.0 is supposed to come with Windows XP.
Starting with Microsoft Data Access Components (MDAC) version 2.6, MDAC no longer contains the Jet components, so installing MDAC won't work.