shweta
shweta

Reputation: 91

Connection String of MS Access database

I have developed a Windows application in C#, and its database is in Access 2010.

I have the database connection string, but it gives an error: OleDbException Was Unhandled

Please suggest me what is the problem & its solution.

con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="\\dtinaurdsna02\\LE-IN\\Admin\Quality Rating\\Quality_Rating_Tool\\Quality_Rating_Tool.accdb";Persist Security Info=True;");

Upvotes: 1

Views: 33837

Answers (4)

Berkay Yalçın
Berkay Yalçın

Reputation: 11

var connect = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source= C:\Users\User1\Desktop\test.mdb";

using(var conn = new OleDbConnection(connect)) { codes here }

use like this

Upvotes: 1

Sachu
Sachu

Reputation: 7766

If you know the path exactly you can use

con = new OleDbConnection
       (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source
             =\dtinaurdsna02\LE-IN\Admin\Quality
                Rating\Quality_Rating_Tool\Quality_Rating_Tool.accdb;
                Jet OLEDB:Database Password=xxxxxxx;
                 Persist Security Info=True;");

If database is within the app folder and you can use below

string path = Environment.CurrentDirectory;
      path = path + "\\Quality_Rating_Tool.accdb;";
   con = new OleDbConnection
           (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source" + path);

Upvotes: 4

Patrik
Patrik

Reputation: 1372

1) \\ is an escaped \

2) You end your string after Source= because of the quotation mark. You might use ' instead of ".

3) @ disables escaping

You need to read something about Escaping!

con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;DataSource='\\dtinaurdsna02\\\LE-IN\\Admin\Quality Rating\\Quality_Rating_Tool\\Quality_Rating_Tool.accdb';Persist SecurityInfo=True;");

Upvotes: 1

Brendon Vdm
Brendon Vdm

Reputation: 974

Your "Data Source="\dtinaurdsna02\LE-IN\Admin\Quality Rating\Quality_Rating_Tool\Quality_Rating_Tool.accdb" is in Inverted commas eg.("")which is canceling out the original inverted commas... change it to this

con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='\\dtinaurdsna02\\LE-IN\\Admin\Quality Rating\\Quality_Rating_Tool\\Quality_Rating_Tool.accdb';Persist Security Info=True;");

Upvotes: 0

Related Questions