Sql connection



  • hello! i m using Qt 5.1. i want to connect my sql data with qt..but shows problem
    my is here..please help me..
    @
    #include <QtCore/QCoreApplication>
    #include <QtSql>
    #include <QDebug>
    #include <QString>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QString servername="BARUN//BARUN";
    QString dbname= "testDB";

    QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    db.setConnectOptions();
    QString dsn=QString("DRIVER{SQL NATIVE CLIENT};SERVER").arg(servername).arg(dbname);
    
    db.setDatabaseName(dsn);
    if(db.open())
    {
        qDebug()<<"opened";
        db.close();
    }
    else
    {
        qDebug()<<"error"<<db.lastError().text();
    }
    
    return a.exec&#40;&#41;;
    

    }
    @

    after compiling it shows ..enable to connect
    my sql server name is=BARUN\BARUN

    edit: please use @ tags around your code sections; Andre


  • Lifetime Qt Champion

    Hi,

    Please enclose your code in coding tags, without them it's really hard to read.

    Are you sure that the server name is correct ?



  • my server name is=BARUN/BARUN

    CODE:
    @
    int main(int argc, char *argv[])
    { QCoreApplication a(argc, argv);
    QString servername=“BARUN//BARUN”;
    QString dbname= “testDB”;

    QSqlDatabase db=QSqlDatabase::addDatabase(“QODBC”); db.setConnectOptions(); QString dsn=QString(“DRIVER{SQL NATIVE CLIENT};SERVER”).arg(servername).arg(dbname); db.setDatabaseName(dsn);
    if(db.open())
    { qDebug()<<“opened”; db.close(); }
    else
    { qDebug()<<“error”<<db.lastError().text();
    }
    return a.exec();
    }
    @

    Again: use @ tags around code sections please; Andre


  • Lifetime Qt Champion

    Are you sure your dsn variable contains the correct data ? Since your not using coding tags (one @ at the beginning and one at the end) I'm not sure your are not missing a %1 and a %2 to get your servername and dbname in the string.



  • my dns :
    @QString dsn=QString("DRIVER{Sql native client};server=%1;database=%2;trusted_connectivity=yes;").arg(servername).arg(dbname);@


Log in to reply
 

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