How to open connection with Microsoft Access database in C# - c#

I'm using Microsoft Access to create my database. Here's my code:
static string Constr = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source = MyData.accdb";
OleDbConnection Conn = new OleDbConnection(Constr);
DataSet DataSet1 = new DataSet();
string SQLstr = "Select * from Tabel";
OleDbDataAdapter DataAdapter1;
Conn.Open();
I'm getting this exception:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Unrecognized database format

You are trying to open an Access database created with Access 2007/2010 (accdb) with an OleDb provider that can understand only MDB files created with Access 2003
The provider to use is Microsoft.ACE.OleDB.12.0 that should be already installed with the latest version of Office.
In alternative you could download the appropriate bits from the Microsoft Access Database Engine
Just pay attention to download the version appropriate for the platform (x86 or x64) you use.
And that's the connection string to use
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MyData.accdb";
NOTE: Keep in mind that if you have Office installed, the corresponding provider has the same bitness of Office, so with Office 64bit comes the ACE in 64bit version and the same for 32bit. This scenario poses a very difficult problem in deployment of your application. You need to have you application compiled with the same bitness or you will not be able to use the provider installed. (Or disinstall the incompatible Office version and reinstall the compatible one. Of course if you should be able to persuade your customer that Office is the incompatible app :-)

Related

unable to open Excel file from C# application on other device

We have a C# project that has an Excel file (Test.xls) included in the Resources folder. The Excel file properties (Build Action = Content & Copy to Output Directory = Copy always)
and the code to open it is:
string path = System.IO.Path.GetFullPath(#"Resources\Test.xls");
MyConnection = new System.Data.OleDb.OleDbConnection(#"provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + path + "';Extended Properties=Excel 8.0;");
When we publish this project the Excel file is published as (Test.xls.deploy) and when the new .exe file is installed on other device it gives error :
any idea how to fix this
thanks
You need to install Microsoft OLE DB Provider for Jet on the target computer:
TechNet:
The Microsoft OLE DB Provider for Jet provides an OLE DB interface to Microsoft Access databases, and allows SQL Server 2005 and later distributed queries to query Access databases and Excel spreadsheets.
Download from Microsoft

this is error ORA-12154: TNS:could not resolve the connect identifier specified?

I've this code :
OracleConnection con = new OracleConnection("data source=localhost;user id=fastecit;password=fastecit");
con.Open(); string sql="Select userId from tblusers";
OracleCommand cmd = new OracleCommand(sql, con);
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{ messageBox.Show(dr[0].Tostring()); }
Same code in both projects,
in project1 "WinForm" Is Working correctly
in project2 "Excel 2007 addins" Following error appears:
ORA-12154: TNS:could not resolve the connect identifier specified
I'm using C#.net 2010 ,office 2007 , windows8, oracle 10g.
When preparing a manual connection to the database, as shown in the picture
Visual Studio, open View menu + Server Explorer.
Right mouse click on Data Connection + Add Connection + Select Oracle Database
server Name : localhost or name of my machine, set username & password and click on Test Connection, test is no succeeds.
ORA-12154: TNS:could not resolve the connect identifier specified?
In case the TNS is not defined you can also try this one:
If you are using C#.net 2010 or other version of VS and oracle 10g express edition or lower version, and you make a connection string like this:
static string constr = #"Data Source=(DESCRIPTION=
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=yourhostname )(PORT=1521)))
(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));
User Id=system ;Password=yourpasswrd";
After that you get error message ORA-12154: TNS:could not resolve the connect identifier specified then first you have to do restart your system and run your project.
And if Your windows is 64 bit then you need to install oracle 11g 32 bit and if you installed 11g 64 bit then you need to Install Oracle 11g Oracle Data Access Components (ODAC) with Oracle Developer Tools for Visual Studio version 11.2.0.1.2 or later from OTN and check it in Oracle Universal Installer
Please be sure that the following are checked:
Oracle Data Provider for .NET 2.0
Oracle Providers for ASP.NET
Oracle Developer Tools for Visual Studio
Oracle Instant Client
And then restart your Visual Studio and then run your project ....
NOTE:- SYSTEM RESTART IS necessary TO SOLVE THIS TYPES OF ERROR.......
The database must have a name (example DB1), try this one:
OracleConnection con = new OracleConnection("data source=DB1;user id=fastecit;password=fastecit");
In case the TNS is not defined you can also try this one:
OracleConnection con = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=DB1)));
User Id=fastecit;Password=fastecit");
run the below command in command prompt
tnsping Datasource
This should give a response like below
C:>tnsping *******
TNS Ping Utility for *** Windows: Version *** - Production on *****
Copyright (c) 1997, 2014, Oracle. All rights reserved.
Used parameter files:
c:\oracle*****
Used **** to resolve the alias
Attempting to contact (description=(address_list=(address=(protocol=tcp)(host=)(port=)))(connect_data=(server=)(service_name=)(failover_mode=(type=)(method=)(retries=)(delay=))))**
OK (**** msec)
Add the text 'Datasource=' in beginning and credentials at the end.
the final string should be
Data Source=(description=(address_list=(address=(protocol=tcp)(host=)(port=)))(connect_data=(server=)(service_name=)(failover_mode=(type=)(method=)(retries=)(delay=))));User Id=;Password=;**
Use this as the connection string to connect to oracle db.

Import Excel 2013 to vs, Microsoft ACE.OLEDB.12.0 is not registered on the local Machine

Creating a form application in vs 2012. I have office 2013 installed, win 8 64 bit. I get the above error for the below piece of code.
public void SetConnection(string text1, string text2, string text3, string text4, string text5, string text6, string text7)
{
connectionString1 = "Initial Catalog=test; Data Source=work\\sqlexpress";
connectionString = "Data Source='c:\\Users\\test1.xlsx';Extended Properties=Excel 12.0 Xml;Readonly=False;";
database = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + connectionString);
database.Open();
database1 = new OleDbConnection("Provider=SQLOLEDB.1;" + connectionString1);
database1.Open();
}
I tried using jet drivers but would not work, so i turned to ace. First i got "Cannot find installable ISAM"; so installed AccessDatabaseEngine_x64 which leads me to this error. I then changed my build to x86 and same errors. So i'm out of options. I did try previous versions of AccessDatabaseEngine from 2007 and 2010 but no luck, any suggestions.
database = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\Users\\test.xls';Extended Properties= \"Excel 8.0;HDR=Yes;IMEX=1\";");
The above connection string works using jet although i have to leave the excel file open for the time being as i get "could not decrypt file" error otherwise. I also had to save the file as a 97-03 excel xls file.
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties=Excel 12.0 Xml;HDR=YES; Data Source=C:\Users\q\Desktop\Ug\Test.xlsx";
As said here, probably you have the 64-bit office installed which doesn't have the 32-bit compnents installed (links are provided there). You may also have to biuld your project targetint x86 platform.

Sharepoint 2010 with excel 2010 error

This is the first question for me on this site, and I wish my issue will be solved by you :)
I am creating a small utility which reads from a SharePoint List and from Excel sheet in order to map fields for doing some kind of migrating data from excel to SP list,
The utility is first populating SP lists from SharePoint depending on the URL provided, then loads columns for the selected List in a Datagrid, then it reads excel sheet and populates all columns in another datagrid:
the first problem I faced was that I was not able to add Microsoft.Sharepoint.dll this was solved by changing .Net Framework from .NET Framework 4 Client Profile to .NET Framework 3.5 since I got the below error:
Error 1 The type or namespace name 'SharePoint' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
So Far SharePoint is working fine, Now when attempting to read from Excel sheet, I get error the below Error:
I have used OLEDB and ODBC connections as below, but none worked under x64:
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.textBox3.Text + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine.
string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + this.textBox3.Text + ";Extended Properties=Excel 12.0;";
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
string sConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ="+ this.textBox3.Text +"";
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
After some search, I read that we need to build this application under 86x to solve excels issue, I did that and the problem was solved for excel, but now SharePoint is not working and giving the error message below:
The Web application at
http://localhost/ITO could not be
found. Verify that you have typed the
URL correctly. If the URL should be
serving existing content, the system
administrator may need to add a new
request URL mapping to the intended
application.
As you see, Excel Columns are populated here, but SharePoint is giving error!!
Any Idea in how to let both of them work in the same application??
Microsoft.SharePoint.dll is only for Applications running on the SharePoint server, not for Apps running on a client machine. For Client Applications, use the Client Object Model or the Web Services.
When using the SharePoint DLLs, the Application must be x64, not x86 (the default for Console Apps) or AnyCPU - SharePoint 2010 is 64-Bit only. (The above Client OM works on 32-Bit of course)
When using the SharePoint DLLs, use .net 3.5, but you already figured that out :)
Technically, using Microsoft.SharePoint.dll outside of SharePoint (either the website or a timer job) isn't supported either - it works in most cases though.
I would start by saying that Sharepoint 2010 need to be run in 64bit and on .net 3.5. Get that sorted, and your sharepoint will work. You might then have to install 64bit version of excel to get it working on the same platform.
WHat you have now is a hotch potch of 3.5 and 4.0, and 64 bit and 32 bit parts, which will never work together.
I have installed MS office for 64 bits and that solved my issue :)

How do I connect to different versions of Excel from C#?

I have a program that imports data from Excel into a dataset. To connect to Excel I use the following code...
return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +
fileName + "; Jet OLEDB:Engine Type=5;"+ "Extended Properties=\"Excel 8.0;\"");
I just got a new computer with Excel 2010 and now the connection attempt fails and throws an exception saying The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine
Is this because the new version of Excel needs a different connection string? Has anyone run into this?
EDIT:
Actually I just read somewhere that using the System.Data.OleDbClient class does not require Excel to even be installed on the computer. So my problem probably has nothing to do with Excel but rather with the Microsoft.Jet tool. My computer is running Windows7 64 bit. Shouldn't that be installed already?
Check out the Microsoft Access Database Engine 2010 Redistributable.
Redistributables for other Excel versions also exist.
You need to compile your program specifically for 32-bit so it will run in the WoW subsystem and invoke the 32-bit Jet provider. As mentioned above, there is no 64-bit version of the Jet provider and likely never will be.
In Visual Studio, you do this by setting your target CPU type in the project settings to x86 instead of Any or 64-bit.
If you don't have the source for your program, you can modify the .exe using the corflags.exe utility that comes with the .NET framework and the /32bit+ flag, but if it's a strongly signed assembly you'll need the .SNK to re-sign it once you've modified it.
Here is a reference on WoW64 if you need more detail: http://en.wikipedia.org/wiki/WoW64
I find this site useful when having connection string questions.
http://www.connectionstrings.com/excel
Also take a look at a previous SO question:
Diagnosing an OLEDB exception when Quering Excel 2010

Categories