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:
#include <QtCore/QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtDebug>
#include <QtSql/QSqlError>
#include <QtSql/QSqlDriver>
#include <QtSql/QtSql>
int main(int argc, char *argv[])
{
QString servername
= "DAN\\SQLEXPRESS";
// my servername which I can see in SQL Manangement Studio QString dbname
= "myDatabase";
// this is my dabase created in SQL Management Studio
db.setConnectOptions();
QString dsn
= QString("DRIVE ={ SQL Native Client}; SERVER=%1; DATABASE=%2;Trusted_Connection=Yes;").
arg(servername
).
arg((dbname
));
db.setDatabaseName(dsn);
if(db.open())
{
qDebug() << "Openned" << db.lastError().text();
db.close();
}
else
{
qDebug() << "Error = ";
}
return a.exec();
}
#include <QtCore/QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtDebug>
#include <QtSql/QSqlError>
#include <QtSql/QSqlDriver>
#include <QtSql/QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString servername = "DAN\\SQLEXPRESS"; // my servername which I can see in SQL Manangement Studio
QString dbname = "myDatabase"; // this is my dabase created in SQL Management Studio
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
QString dsn = QString("DRIVE ={ SQL Native Client}; SERVER=%1; DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg((dbname));
db.setDatabaseName(dsn);
if(db.open())
{
qDebug() << "Openned" << db.lastError().text();
db.close();
}
else
{
qDebug() << "Error = ";
}
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Also, I have added in the .pro file the following statement:
QT += sql
QT += sql
To copy to clipboard, switch view to plain text mode
However, when I compile, I get the following errors:
1>ClCompile:
1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase@@QAE@XZ) referenced in function _main
1>main.
obj : error LNK2019
: unresolved external symbol
"__declspec(dllimport) public: void __thiscall QSqlDatabase::close(void)" (__imp_?close@
QSqlDatabase@@QAEXXZ
) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlError::~QSqlError(void)" (__imp_??1QSqlError@@QAE@XZ) referenced in function _main
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
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
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
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
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
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
1>main.
obj : error LNK2001
: unresolved external symbol
"__declspec(dllimport) public: static char * QSqlDatabase::defaultConnection" (__imp_?defaultConnection@
QSqlDatabase@@2PADA
)
1>ClCompile:
1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QSqlDatabase::close(void)" (__imp_?close@QSqlDatabase@@QAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlError::~QSqlError(void)" (__imp_??1QSqlError@@QAE@XZ) referenced in function _main
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
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
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
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
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
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
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
Bookmarks