📜 ⬆️ ⬇️

MS SQL (Win1251) - (?) -> Qt (Unicode)

A small hack to convert windows-1251 encoding to MSSQL to Unicode for Qt5.

There are still systems that use mssql in the win encoding. To one such system, I wrote an extension to Qt4 and solved the problem with Russian letters simply: by changing the encoding
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("Windows-1251")); QTextCodec::setCodecForTr(QTextCodec::codecForName("Windows-1251")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("Windows-1251")); 

Time passes and I translate my projects to Qt5 if possible. However, the codec change commands were removed there. Everything should be in unicode, which is correct. And the solution did not always work and not on every version of the OS win.

In my program, I do not directly display the result of the SQL query in the model, so I have solved the problem so far:

 //   QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); db.setUserName("<username>"); db.setPassword("<userpass>"); db.setDatabaseName("DRIVER={SQL Server};Server=<servername>;Database=<databasename>;Regional=Yes"); // SQL-,       query->exec( "select cast(<fieldname> as varbinary) from <tablename>"); //   QTextCodec *codec = QTextCodec::codecForName("Windows-1251"); //          Unicode.      Qt ... = codec->toUnicode(queryl->value( 0 ).toByteArray()); 

')
Interestingly, if the field is not converted to a varbinary , then the .toByteArray() method will .toByteArray() already corrupted data.

And maybe someone knows the connection string for data in Unicode?

Source: https://habr.com/ru/post/234089/


All Articles