Access OleDb Connection string without Data Source Path C#.Net

OleDbConnection String without Data Source Path:
    In .Net application one of the main problem is Data Provider Path. We actually write the connection string in access OleDb Database as
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\WORK AREA\Thalassemia\Data\thalsemia.accdb");
 This way only we are using the connection to execute the application.But the problem with this connection string is the database must be in the same path as we write in the Data Source as "D:\WORK AREA\Thalassemia\Data\thalsemia.accdb".
  Problem Is if the client system does not have the D Drive then your application will not work there.
To overcome this problem you can use Application.StartupPath property. It can show the path that where the your application contains in the system.

Connection string without database Path:
To use this code your database should be in the path urApplicatio/bin/Release. Keep your database in Data Folder

string strng = Application.StartupPath;
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+strng+"\Data\thalsemia.accdb");
cn.Open();

I hope this code will help you.

No comments:

Post a Comment