Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
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
Updated 17-Sep-12 2:14am
v2

Hey there,

Take a look at SqlConnectionStringBuilder[^].
C#
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
 
Share this answer
 
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:

C#
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
 
Share this answer
 
the question is not clear.
 
Share this answer
 
Comments
Rockstar_ 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.
[no name] 17-Sep-12 9:05am    
And this is not a solution
Rockstar_ 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900