read

In last post I’ve written a recipe how to connect from Linux to Microsoft SQL Server Express using ODBC. Now, since I’m using Qt framework for app portability, it is the time to do the same on Windows.

One can connect two ways to MS SQL: using ODBC or *[TDS]:”Tabular Data stream” - SQL Server’s native protocol. With new release of Qt SDK there is a QSqlOdbc4 driver bundled. No TDS driver is available for Microsoft’s native protocol. It is quite easy to connect to DB without having to define a *[DSN]:”Data Source Name” first, just use proper connection string:

DRIVER={SQL Native Client};SERVER=servername;DATABASE=databasename;UID=login;PWD=pass;

So, in case of default install of SQL Express we would have:

QString serverName = "localhost";
QString dbName = "master";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
QString dsn = QString("DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;UID=sa;PWD=;").arg(serverName).arg(dbName);
db.setDatabaseName(dsn);
db.open();

Note: If you have installed the server with named instance just use form of servername\namedinstance.

QString serverName = "localhost\\mssqlexpress";
Blog Logo

Marcin Gil


Published

Image

When grass was greener

Rants on programming, photography, life and everything

Back to Overview