Hi everyone,


I'm trying to set up my environment to create a project with a connection to a databse.

I'm using Qt Version 5.2 (x86) and the VS Addin for Visual Studio 2010 .

I'm trying to test the connection to a local database created in Microsoft SQL Server Management Studio. Is a sample database.

Then, I've created a QtConsoleApplication and use the following code:
Qt Code:
  1. #include <QtCore/QCoreApplication>
  2. #include <QtSql/QSqlDatabase>
  3. #include <QtDebug>
  4. #include <QtSql/QSqlError>
  5. #include <QtSql/QSqlDriver>
  6. #include <QtSql/QtSql>
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. QCoreApplication a(argc, argv);
  11.  
  12. QString servername = "DAN\\SQLEXPRESS"; // my servername which I can see in SQL Manangement Studio
  13. QString dbname = "myDatabase"; // this is my dabase created in SQL Management Studio
  14.  
  15. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
  16.  
  17. db.setConnectOptions();
  18.  
  19. QString dsn = QString("DRIVE ={ SQL Native Client}; SERVER=%1; DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg((dbname));
  20.  
  21. db.setDatabaseName(dsn);
  22.  
  23. if(db.open())
  24. {
  25. qDebug() << "Openned" << db.lastError().text();
  26. db.close();
  27. }
  28. else
  29. {
  30. qDebug() << "Error = ";
  31. }
  32. return a.exec();
  33. }
To copy to clipboard, switch view to plain text mode 

Also, I have added in the .pro file the following statement:
Qt Code:
  1. QT += sql
To copy to clipboard, switch view to plain text mode 
However, when I compile, I get the following errors:
Qt Code:
  1. 1>ClCompile:
  2. 1> main.cpp
  3. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase@@QAE@XZ) referenced in function _main
  4. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QSqlDatabase::close(void)" (__imp_?close@QSqlDatabase@@QAEXXZ) referenced in function _main
  5. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlError::~QSqlError(void)" (__imp_??1QSqlError@@QAE@XZ) referenced in function _main
  6. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class QString __thiscall QSqlError::text(void)const " (__imp_?text@QSqlError@@QBE?AVQString@@XZ) referenced in function _main
  7. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class QSqlError __thiscall QSqlDatabase::lastError(void)const " (__imp_?lastError@QSqlDatabase@@QBE?AVQSqlError@@XZ) referenced in function _main
  8. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall QSqlDatabase::open(void)" (__imp_?open@QSqlDatabase@@QAE_NXZ) referenced in function _main
  9. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QSqlDatabase::setDatabaseName(class QString const &)" (__imp_?setDatabaseName@QSqlDatabase@@QAEXABVQString@@@Z) referenced in function _main
  10. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QSqlDatabase::setConnectOptions(class QString const &)" (__imp_?setConnectOptions@QSqlDatabase@@QAEXABVQString@@@Z) referenced in function _main
  11. 1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QSqlDatabase __cdecl QSqlDatabase::addDatabase(class QString const &,class QString const &)" (__imp_?addDatabase@QSqlDatabase@@SA?AV1@ABVQString@@0@Z) referenced in function _main
  12. 1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static char * QSqlDatabase::defaultConnection" (__imp_?defaultConnection@QSqlDatabase@@2PADA)
To copy to clipboard, switch view to plain text mode 

I've read that I need to set an ODBC Data Source but I never used something like that and I don't even know what to do or how to do or even why I need to do.

Can you please help me with this?

Thank you,
Dan