PDA

View Full Version : QSqldatabase open() failed to connect SQL server on other than default 1433 port



helen
5th December 2011, 15:59
SQL server uses other than default 1433 port (e.g. 5555), system DSN created using the port 5555 and tested completed successfully.

However, QSqlDatabase failed to open database.

The code snippets:


QSqlDatabase *db = QSqlDatabase :: addDatabase("QODBC");
db->setDatabaseName("test");
db->setUserName(userName);
db->setPassword(passWord);

if ( db->open() ){
///Database open successfully.....
}
else
{
///Database open failed
}

It's greatly appreciated for any help to solve this problem.

Lykurg
5th December 2011, 17:11
What is
QSqlDatabase::lastError().text() telling you?

helen
5th December 2011, 17:36
Thank you for your reply. The lastError().test() prints out as follow.

"[Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). QODBC3: Unable to connect"

Is any possible it still uses default 1433 port instead of specified port 5555?

Lykurg
5th December 2011, 17:45
Check that with QSqlDatabase::port(). Because in your example code you do not set the port.

helen
5th December 2011, 17:54
QSqlDatabase.port() return -1.