MS SQL Server 2008 with Qt



  • Hi,
    I'm trying to connect Qt to MS SQL Server 2008 R2. I already have compiled the ODBC driver and I think it is ok.
    This is the code I'm using:
    @
    qDebug() << "Drivers: "<<QSqlDatabase::drivers();
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setHostName("ze-w7\SQLExpress");
    db.setDatabaseName("tempdb");
    db.setUserName("sa");
    db.setPassword("123456");
    if(!db.open()){
    qDebug()<<"ERROR: "<<QSqlError(db.lastError()).text();
    } else {
    qDebug()<<"Ok";
    }@

    But I always getting this error:
    Drivers: ("QSQLITE", "QODBC3", "QODBC")
    ERROR: "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect"

    What should I do?



  • Maybe instead of
    @db.setHostName("ze-w7\SQLExpress");@

    you must write

    @ db.setHostName("ze-w7\SQLExpress");@

    Or try write this code:

    @QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("Driver={SQL Server};Server=ze-w7\SQLEXPRESS;Database=tempdb;");
    db.setUserName("sa");
    db.setPassword("123456");
    if(!db.open()){
    qDebug()<<"ERROR: "<<QSqlError(db.lastError()).text();
    } else {
    qDebug()<<"Ok";
    } @



  • really thanks :)
    Used the code you suggest and works :D


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.