Pierce
Pierce

Reputation: 153

Using non-ascii characters in connection string password

I'm writing a utility to work with a bunch of legacy Access '97 .MDB files. I need to connect to them programmatically, and I am not allowed to convert the files to a newer version of Access. So far, no a big deal. A majority of them are password protected, but the password is really odd:

Password File

I found out the hard way that making this string the default text for a text box will break my code. I am able to read the password and load it into a text box on my form with the following code:

FileStream Reader = new FileStream(openFileDialog2.FileName, FileMode.Open);
byte[] StringData = new byte[Reader.Length];
Reader.Read(StringData, 0, StringData.Length);
String strPassword = Encoding.Default.GetString(StringData);

txtReadPassword.Text = strPassword;
strDatabasePassword = strPassword;

If I write the string out to the console at this point the last character does not appear, but the capitol E with the accent mark does. I am generating the connection string using the read password via the following code:

public static string GetConnectionString(string strDataSource, string strPassword)
        {
            return String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Mode=Share Deny None;data source={0};password={1};", strDataSource, strPassword,"Jet OLEDB:Database ");
        }

And the connection string returned does contain the special characters:

enter image description here

The password is correct, as I can copy/paste it into access and it opens the database. When I try to use that connection string via this code:

private void btnConnectToDatabase_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection();

                try
                {
                    connection.ConnectionString = strConnectionString;
                    connection.Open();
                    Console.WriteLine("Connected!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to connect to data source" + ex.ToString());
                }
        }

I get this result:

enter image description here

Line 78 is "connection.ConnectionString = strConnectionString;" from above. Index 137, I believe, is the last two characters in the connection string that are the weird characters in the password.

The "no-longer-supported" application that generated and opens these .MDB files is connecting to them programmatically, so there has to be a way to do so. The solution has eluded me for three days now, so I'm looking for help.

Upvotes: 2

Views: 1702

Answers (2)

Sale
Sale

Reputation: 1

For those that still have this problem despite proper password length (up to 14/20 chars), encapsulate the password value inside either apostrophes or escaped quotation marks:

"...;Password=\"passwordÈ┘\""

or

"..;Password='passwordÈ┘'"

Upvotes: 0

Malibu
Malibu

Reputation: 48

@Pierce There are these most awesome specifications by Microsoft for the number of characters in a password depending on the version of Access. So like the rad thing is that Access 2003 and below are 14 characters and Access 2007+ is 20 characters. Give the 20 characters a shot since you're using 22 characters in your example. SWEET!

Upvotes: 2

Related Questions