I have an Excel .odc file which defines an OLEDB connection like so:
Provider=MSOLAP.5;Integrated Security=SSPI;Persist Security Info=True;User ID="";Initial Catalog="";Data Source=xxx.xxx.com\tabular;Location=xxx.xxx.com\tabular;MDX Compatibility=1;Safety Options=2;MDX Missing Member Mode=Error
Is it possible for me to open the database using SqlConnection in C# using this?
There is nothing built into .Net outside of Office Interop (actually opening an instance of Excel), an Office Add-In (Excel is already opened, and the code runs as a macro), or Reporting Services + Sharepoint to use *.odc files to connect to a database. You will have to write your own code to open the file, parse the values out for a connection string, and use that string to connect to a database.
When you go to write this code, be careful: ODC is not-quite valid XML.
Related
From within an VSTO Excel Add-In I tried to connect to an encrypted Access Database (accdb) using OleDbConnection. (I tried on Excel 2010 and 2016 with different VSTO templates)
I get the Error: System.Data.OleDb.OleDbErrorCollection: 'Not a valid password.'
The Source of the Exception is: "Microsoft Access Database Engine".
The Exception does not contain any inner exception.
The code runs without problems:
from within the Add-In if the targeted database is NOT encrypted.
from a console app even if the accdb is encrypted.
So there is no problem with the connection string, the path or the password.
What is different if the code is run in the context of an Add-In?
I found that some people faced a similar problem (the same error message), when trying to connect to an encrypted accdb from ASP.NET and the permissions for the web process were not correct. Could this be a hint? IMHO the Add-In should run with user privileges, so this should not be the cause (and as described above, the connection to the database can be established, therefore the file opened, as long as the accdb is not encrypted)...
public void TestAccessConnect()
{
string connString = #"Provider=Microsoft.ACE.OLEDB.12.0;" +
#"Data Source=C:\temp\encryptedDb.accdb;" +
#"Jet OLEDB:Database Password=1234;";
using (var oleDbConn = new OleDbConnection(connString))
{
oleDbConn.Open(); // <---- triggers the following error
// Error: { System.Data.OleDb.OleDbErrorCollection}
// Source: "Microsoft Access Database Engine"
// Message: "Not a valid password"
}
}
I did not find anything concerning my problem on the internet and I am running out of ideas. Is this a (known) bug on the MS side?
Any advice on where to look is highly appreciated.
Update 2019/05/17:
I found that if I encrypted the accdb in "legacy encryption", the connection works! :-(
To reproduce:
In Access:
Decrypt the database (DB must be opened exclusively)
Go to File -> Options -> Client-Settings: There scroll to the "Advanced" section and tick "Legacy encryption" (instead of "Standard" - which is set by default)
Close the database (might be optional)
Reopen the database exclusively
Encrypt the database.
Et voilĂ - now the database can be opened using the password from the Add-In.
Dear MS-guys - what the heck! The non working "default" encryption is there at since Office 2010 at least.
In addition: I did tried to connect directly to the accdb from Excel 365. Data -> Get Data.
-> Database ->Access. Non encrypted (OK), Encrypted (Fail)
-> Other Sources -> OLEDB: (here you can provide a connection string). Non encrypted (OK), Encrypted (Fail)
-> Other Sources -> ODBC: (Set up a new ODBC-Connection, here you get to see some really cool dialog boxes from the last century): Non encrypted (OK), Encrypted (OK)
Well, it seems that one has to accept that Microsoft can't properly integrate their own database to their office suite. And hey, it's only been about 10 years than this is not working...
Just to make sure that it's not something Excel specific, I also created an Add-In for Word. Same experience.
So basically I can connect either by changing the encryption model (not very practical because there are many instances out there), or by using and ODBC-Connection (no good fit because I want to use EF Core - in this case for Access - (i know, i know, ... Access is about to be phased out)) or by just directly using ADODB or by remoting Access.
But, I still have the feeling that the .NET class SHOULD behave like a .NET class - even if it's hosted in an Office Application.
Does anybody have an idea what could stop this simple command from working?
Update No2 2019/05/17:
I put the connection code in a new DLL project. Then I have an Add-In project and a Console-Application. Both contain basically the same two lines of production code:
var ac = new AccessConnectTest(); // <- class in own assembly
ac.TestAccessLogin();
Console works.
Add-In does not.
How is this possible? Does office fiddle with Assembly resolution?
I'm currently working on a wpf application where I tried to create a database.
I used data sources > add new datasource > dataset and copied the query string for its properties, but it is giving me the following exception:
What might be the problem? This is a local database... and when I click on the test connection button it writes "test connection succeeded"
Thanks
You are connecting to an SDF file. This means that you are using Sql Server Compact, not the full fledged Sql Server.
The classes to be used are named
SqlCeConnection
SqlCeCommand
The one you are using (SqlConnection) cannot understand the connection string used for a Sql Server Compact
Of course you need to add the reference to the assembly and the appropriate using directives
Assembly: System.Data.SqlServerCe.dll
using System.Data.SqlServerCe;
....
You are using a SqlConnection rather than the SqlCeConnection that you require. SqlConnection is for connecting directly to a "real" sql server.
Take a look at the MSDN for more information.
hey,
i am new at connecting to dataBases and for some reason each time i use those following lines my program collapse:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\Company.accdb"
OleDbConnection con = new OleDbConnection(connectionString);
inside my debug folder i got Company.accdb access file
edit:
i am getting 'Microsoft.Ace.OLEDB12.0' provider is not registered on the local machine any idea how to solve it?
thanks in advance for your help
Two things -
This connection string rely on ACE OLEDB provider (typically comes with Office 2007 - your machine need to have this provider)
Connection string is requesting data dictionary. You probably need to use below form:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;
For password protected files, form would be Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Jet OLEDB:Database Password=MyDbPassword;
I will also suggest trying different Provider (ODBC perhaps) instead. For various connection strings for Access 2007, refer http://www.connectionstrings.com/access-2007
How can I connect a Crystal Report (VS 2008 basic) to a MySQL DB without using a DSN or a preload DataSet using C#?
I need install the program on several places, so I must change the connection parameters. I don't want to create a DSN on every place, nor do I want to preload a DataSet and pass it to the report engine. I use nhibernate to access the database, so to create and fill the additional DS would take twice the work and additional maintenance later. I think the best option would be to let the crystal reports engine to connect to MySQL server by itself using ODBC.
I managed to create the connection in the report designer (VS2008) using the Database Expert, creating an ODBC(RDO) connection and entering this connection string
"DRIVER={MySQL ODBC 5.1 Driver};SERVER=myserver.mydomain"
and in the "Next" page filling the "User ID", "Password" and "Database" parameters. I didn't fill the "Server" parameter. It worked. As a matter of fact, if you use the former connection string, it doesn't matter what you put on the "Server" parameter, it seems the parameter is unused. On the other hand, if you use "DRIVER={MySQL ODBC 5.1 Driver}" as a connection string and later fill the "Server" parameter with the FQDN of the server, the connection doesn't work.
How can I do that by code? All the examples I've seen till now, use a DSN or the DataSet method. I saw the same question posted but for PostgreSQL and tried to adapt it to mysql, but so far, no success. The first method:
Rp.Load();
Rp.DataSourceConnections[0].SetConnection("DRIVER={MySQL ODBC 5.1 Driver};SERVER=myserver.mydomain", "database", "user", "pass");
Rp.ExportToDisk(ExportFormatType.PortableDocFormat, "report.pdf");
raise an CrystalDecisions.CrystalReports.Engine.LogOnException during ExportToDisk
Message="Logon failed.\nDetails: IM002:[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified.\rError in File temporal file path.rpt:\nUnable to connect: incorrect log on parameters.
the InnerException is an System.Runtime.InteropServices.COMException with the same message and no InnerException
The "no default driver specified" makes me wonder if the server parameter is unused here too (see above). In that case: How can I specify the connection string?
I haven't tried the second method because it doesn't apply.
Does anybody know the solution?
I think it'll likely be quicker to generate the Dataset via nHibernate, or do a direct ADO.NET query, then trying to solve the issue.
I'm trying to create an OleDb connection to an Excel file that is located on a SharePoint server. The example code I'm playing with at the moment throws an OleDb exception "Invalid internet address":
public static void ConnectToRemoteExcelFile()
{
string connectionString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;\";", "http://horde.servername.co.uk/Docs/Documents/Sales.xlsx");
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
}
}
Anyone know how to connect to an excel spread sheet on a web server?
Update
Microsoft support have come back to me and agree that is not possible to connect to Excel files located on a web server. Both Jet .40 and the news ACE (Access Connectivity Engine) do not support this mode of operation. They cite reference to the KB Article "The Import/Link Data on an FTP or HTTP Server Help topic is not correct in Access 2000, Access 2002, Access 2003 and Access 2007"
As far as I know that is not possible, you would have to download the file first, then access it. The reason being is that the file could not be modified given its location.