Click here to Skip to main content
13,045,566 members (71,358 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi friends,

I have a problem in MS Access connection string , i am reading Access files , those access files contains password,Each MS Access file contains one passwords from 2 randomnly, so i dont know which will be the password dynamically, so based on the password i want to change the connection string.
any one please guide me.
Posted 17-Sep-12 2:13am
Updated 17-Sep-12 2:14am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Hey there,

Take a look at SqlConnectionStringBuilder[^].
var conStringBuilder = new SqlConnectionStringBuilder
        UserID = @"something",
        Password = @"passphrase",
        DataSource = @".\SQLExpress",
        InitialCatalog = @"MyDatabase"
var connectionString = conStringBuilder.ConnectionString;

I know I've used .Net 4.0 syntax but this class is available from .Net 2.0. So it should help you to change the username password accordingly and generate the desired connection string.

Hope this helps, regards
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Imagine your connection string for MS Access would look like this:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;"

Now to make the password interchangeable by code you rewrite it like so:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=admin;Password={1};

You can then do something like this in your code:

String connectionStringPattern = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=admin;Password={1};";
foreach(currentFileName in listOfAccessFileNames)
    String connectionStringPatternWithFile = String.Format(connectionStringPattern, new String[] {currentFileName, "{0}");
    String passWord1 = "firstPassword";
    String passWord2 = "secondPassowrd";
    String connectionString1 = String.Format(connectionStringPatternWithFile, passWord1);
    String connectionString2 = String.Format(connectionStringPatternWithFile, passWord2);
    // Here place some code where you would try to open a connection to the current Access file 
    // using both variants of the password

Hope you get the idea!

— Manfred
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

the question is not clear.
VijayChauhan123 17-Sep-12 8:37am
MS Access files may contain different passwords, out of 2 passwords, dynamically we have to change the connection string if one gets failed to open , then use another connection string.
Wes Aday 17-Sep-12 9:05am
And this is not a solution
VijayChauhan123 20-Sep-12 0:10am
can u tell me the correct solution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 17 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100