tags:

views:

156

answers:

2

How can I connect mssql with Qt ?

Thank a lot.

+3  A: 

QT seem to have an ODBC driver which you can use to connect to mssql.
Here's the relevant docs:
http://doc.trolltech.com/4.6/sql-driver.html

shoosh
+1  A: 

Qt supports ODBC, to connect to an odbc database using a QSqlDatabase you can use the following code

QString connectionTemplate = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";

QString connectionString = connectionTemplate.arg(server).arg(dbName);
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", connectionName);

db.setDatabaseName(connectionString);
db.setUserName(user);
db.setPassword(password);

if (db.open())
{

}
else
{

}

Most or all of the QSql... classes return an error, it is a very good habit to always check that error.

If you built Qt from scratch you might have to enable the building of the odbc plugin

Harald Scheirich