D
D
DeepX2016-12-07 10:16:25
Database
DeepX, 2016-12-07 10:16:25

How to enter password to MS Access database from non-printable characters from C# code?

Good day, dear colleagues!
Help me please. I ran into a problem I didn't know before.
There is quite a working MS Access database , *.mdb format , working with a desktop client. The password for access to the database is set with non-printable characters like \u0127 \u0027 \u0087 , and the like. Accordingly, it is impossible to enter them from the keyboard - but you can only get access programmatically by setting the appropriate password in the connection parameters. Yes, and in any case, the password cannot be changed.
The old client was written in Delphi , written a long time ago, but I know of a working function that collects a password to connect to the database:

//получение пароля
function TDBAccess.GetDBPass: string;
begin
  Result := Chr(27) + Chr(8) + Chr(30) + Chr(23) +
 Chr(31) + Chr(22) + Chr(2) + Chr(24) + Chr(127);
end;

Now the client is written in C# , there is no Chr() function in principle, and, accordingly, we do character conversion and password string assembly manually. We tried a bunch of options - it does not work in any way!
Here are the main options:
// строка подключения
DBConnection = new OleDbConnection();
DBConnection.ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Database Password={1};", _path, _pass);

Option 1.
string _pass = @"\u0027\u0008\u0030\u0023\u0031\u0022\u0002\u0024\u0127";

Option #2.
var _pass_char = Convert.ToChar(27) + Convert.ToChar(8) + Convert.ToChar(30) + Convert.ToChar(23) + Convert.ToChar(31) + Convert.ToChar(22) + Convert.ToChar(2) + Convert.ToChar(24) + Convert.ToChar(127);
String _pass= Encoding.Default.GetString(_pass_char);

Option #3.
var _pass_char =(char)27 + (char)8 + (char)30 + (char)23 + (char)31 + (char)22 + (char)2 + (char)24 + (char)127
String _pass= Encoding.Default.GetString(_pass_char);

Option #4.
byte[] _hex_code = new Byte[] { 0x1B, 0x08, 0x1E, 0x17, 0x1F, 0x16, 0x02, 0x18, 0x7F };
String _pass= Encoding.Unicode.GetString(_hex_code);

Accordingly, we played around with all variants of code pages
var encoding = Encoding.GetEncoding(...)...
. But when opening a connection:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Wrong password.
Or, with some CodePage, it immediately swears at an erroneous connection string.
Tell me, please, who knows! Maybe it's in the little things, but we "steam".
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DeepX, 2016-12-08
@DeepX

I asked the question myself - I answer it myself :)
You need it like this:
Everything, as always, is in the details...
Moreover, I thought about this method, but rejected it as too simple and obvious :) It turns out that it is also suitable for non-printable characters.

M
Mikhail, 2016-12-07
@dmitrievMV

The Chr function converts an integer IntValue to either AnsiChar or WideChar

string _pass = Encoding.ASCII.GetString(new byte[] { 27, 8, 30, 23, 31, 22, 2, 24, 127 });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question