Connecting to an mdb file using OdbcConnection in VS2013 - c#

I am trying to connect to an mdb file from C# 2013 using OdbcConnection. I used to have Office 2000 on my Windows 7 and Windows 8 machines, but I have upgraded to Office 2013 on both and it now longer works on either. My database is still in mdb format.
Here's the code:
const string myConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Path\Mydb.mdb";
OdbcConnection connRL = new OdbcConnection();
connRL.Open();
Whatever I have tried I get the following error:
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not
found and no default driver specified
I have Installed the "Microsoft Access Database Engine 2010 Redistributable" and the "2007 Office System Driver: Data Connectivity Components" but neither of them have made any difference. I can create a new Data Source from the VS 2013 Project menu and that all works at design time.
When I create a blank udl file and double click on it and go to the provider tab there's nothing listed about Access or Jet, but I can see the providers in the registry at HKEY_CURRENT_USER\Software\Microsoft\VWDExpress\12.0_Config\DataProviders. Why aren't they there?
What else can I do to get this to work? I have tried everything I can think of.

I assume you can't connect to the mdb through OdbcConnection. Anyway, to connect to the DB you must specify the connection string on your connection object which is not specified in your code.
To access mdb use code like this:
const string myConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Path\Mydb.mdb";
using(var connRL = new OleDbConnection(myConnectionString))
{
//use your connection
}

Related

How to run .mdf database from LAN?

I have a C# (.NET Framework 4.5 - MVS 2015) project which has a Service-based Database with a local generated .mdf file. My Microsoft SQL Server that I use has this version: 13.0.1601.5.
Everything works fine on the server PC. I share in LAN the application with the .mdf files, but the clients from the same LAN can't open the application. It seems that the connection with the DB is bad.
My connection string is formed like this:
public static string attachedDbFile = "AttachDbFilename=" + currentWorkinglocation + "NCI_DB.MDF;";
public SqlConnection mySqlConnection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;" + attachedDbFile + "Integrated Security=True");
What am I missing, please?
Later Edit:
After following the steps provided by you guys:
My Data Source is Microsoft SQL Server now (and not MSQL Server Db file). I have attached the .mdf file to a database from my server (in MSQL Server Management Studio), the "Allow remote connection option" is checked and I have changed the connection string to:
public SqlConnection mySqlConnection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=myDbName;Integrated Security=True");
Now, the application works on my local host (the PC that has the sql server installed) without the need of the .mdf file. It's ok.
But if the clients try to access the application from LAN, from my shared location, the app .exe crashes once again with this error message:
Did I missed something?
SOLUTION:
After following some pointers from our colleagues and after investigating further the problem the solution is:
- install sql server 2017 (full version ! - not express edition)
- install microsoft sql server management studio
- attach mdf to a database
- configure an account with rights for that database
- update the connection string according to this:
public SqlConnection mySqlConnection = new SqlConnection(#"Data Source=ip,port(1433-default);Initial Catalog=db_name;User ID=user;Password=pwd");
SQL server only supports database files on local disks (or more exotic links than LANs, such as iSCSI, etc). If you want to share access to a database across a local network, it's time to stand up a SQL Server instance that will own the file and then connect to that SQL Server client/server. Stop trying to access it as a file across the network.

Asp.net error on trying to connect with database

I am trying to connect to a database which is an .mdb file from my web application on asp.net. This error comes up 'Microsoft.ACE.OLEDB.12.0 Data Source =C:\Users\KIKI\Desktop\ASP.net labs\Erg8\Erg8\ebookstoredb.mdb' provider is not registered on the local machine. The thing is that on my connection string I don't use version 12 but 4. I tried installing Microsoft Access Database Engine 2010 Redistributable which gives me the option to select version 12 when I set it the connection from the "Server Explorer" window but the error is still there. Any ideas? I have spent hours looking for a solution...
Here is my connection string:
OleDbConnection constring = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\KIKI\\Desktop\\ASP.net labs\\Erg8\\Erg8\\ebookstoredb.mdb");
Also, I tested the connection when I set it and a message "Connection Succeeded" came up. The problem seems to be when I run the application on the browser.

Connecting C# Application to MS Access 2013 DB

I'm using VS 2012 & Office 2013 64 bits, and i changed the target platform to x86, but I still got this weard error
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local
machine".
You need to download two components:
1) microsoft access database engine
2) Office System Driver: Data Connectivity Components
After these installations your application will start to work. Furthermore, following link is better to use on access to database:
void test()
{
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Database1.accdb;Persist Security Info=False;";
OleDbConnection conn = new OleDbConnection(baglantiCumlesi);
conn.Open();
conn.Close();
}

Postgresql odbc driver error c# [IM002] [Microsoft][ODBC Driver Manager] Data source name not found

I am setting up an database application to be database agnostic, and when testing with postgresql I get the standard dsn error:
[IM002] [Microsoft][ODBC Driver Manager] Data source name not found
I usually use SQL server and MySQL so I'm new to postgres, I tried the standard recommended Connection string:
"Driver = {PostgreSQL}; Server = localhost; Database = postgres; Port = 5432; Uid = postgres; Pwd = XXXXXX;"
I also tried the name of the odbc driver that I installed after installing postrgesql:
"Driver = {PostgreSQL ODBC Driver(UNICODE)}; Server = localhost; Database = postgres; Port = 5432; Uid = postgres; Pwd = XXXXXX;"
Setting up a DSN in odbc manager also works perfectly using the unicode driver, so I cant understand why i cant connect in my application, is there an error in the driver name that i am using in the connection string?
Your error message looks very strange. It tells about DSN not found. Are you sure you use connect string with Driver=...?
You can use ODBC connect string in several forms. At first you have created DSN, so you can use it:
DSN=mn_test; Uid=postgres; Pwd=postgres;
Then you can use other form of connect string:
Driver={PostgreSQL UNICODE};Server=127.0.0.1; Port=5493; Database=mn_test; Uid=postgres; Pwd=postgres;
Both work on my old 32 bit Windows environment. I test them with simple Python script (I use ActiveState Python in which there is simple odbc module):
import odbc
def test_odbc(connect_string):
print(connect_string)
db = odbc.odbc(connect_string)
c = db.cursor()
rs = c.execute("SELECT version()")
for txt in c.fetchall():
print('%s' % (txt[0]))
print('-----')
test_odbc('Driver={PostgreSQL UNICODE};Server=127.0.0.1; Port=5493; Database=mn_test; Uid=postgres; Pwd=postgres;')
test_odbc('DSN=mn_test; Uid=postgres; Pwd=postgres;')
When you created the DSN, did you create it with the correct odbcad tool? With the 64bit version found in C:\Windows\System32 if your application is 64bit and with the 32bit version found in C:\Windows\SysWOW64 if your application is 32bit?

MYOB ODBC connection error

I recently got a prebuild application which is uses a MYOB ODBC connection to a MYOB file.
The ODBC connection works perfectly in that application.
I'm using the same ODBC connection string in another application but it failed to open in that application. The connection string is perfectly identical but it won't work there.
The error I'm getting is:
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
The server explorer in Visual Studio 2008 connects without a problem with the same connection string.
Is it a trusted application issue because my new application is not digitally signed at the moment?
OdbcConnection odbc = new OdbcConnection("Driver=MYOAU0901;TYPE=MYOB; UID=Administrator; PWD=; DATABASE=C:\\Premier125\\Clearwtr.MYO; NETWORK_PROTOCOL=NONET; DRIVER_COMPLETION=DRIVER_NOPROMPT;;KEY=****");
odbc.Open();
The key used in the connection string is definitely valid.
kindly help me.
MYOB ODBC does not require your application to be signed. The company file must be registered for ODBC to allow reads, and you must install a licence key to allow writes. I am assuming your new application is running on the same machine as the company file and MYOB executable, and that your company file is properly registered.
Try setting the KEY parameter to the key itself, not a path to a key file.
Replace ;; with ;
Include the key HOST_EXE_PATH in your connection string. This is the path to the MYOB executable (in your case this will be myobp.exe).
If you still have no luck, try setting up a User DSN. This will allow you to test whether everything is installed correctly. If the DSN works, you can try replacing your connection string with a reference to the DSN, so "DSN=YourDsnName".

Categories

Resources