QSqlDatabase problem (QODBC)



  • Hello,

    I am trying to connect at a database in SQL Server 2008. This last seems to be correctly installed (because I can create database and tables), and in addition, my C# program can connect in a database with success.

    So, I've wrote the following lines in Visual C++ Express :
    @
    QSqlDatabase sql_test = QSqlDatabase::addDatabase("QODBC");
    sql_test.setHostName("SYLRA-PC\SQLEXPRESS");
    sql_test.setDatabaseName("quiz_games");
    bool ok = sql_test.open();
    if(ok)
    {
    QMessageBox::information(this, "Connexion SQL", "Connexion SQL réussite !");
    }
    else
    {
    QMessageBox::warning(this, "Connexion SQL", "Connexion SQL échoué !");
    }
    QSqlError *checkError= new QSqlError(sql_test.lastError());
    QMessageBox::information(this, "", checkError->text());
    @

    There is the result in french :
    !http://uploads.siteduzero.com/files/297001_298000/297821.png(Here)!
    In english, it is : "data source name not found and no default driver specified"

    For me, it could be wrong specification that I should give to QSqlDatabase object, but I've tried other solutions like
    @
    db.setUserName("");
    db.setPassword("");
    @
    I didn't have any username and password :s

    Here is the following lines to connect in C#, just to show that I just needed to specificate Server and database to make it work.
    @
    Global.cn.ConnectionString = "Server=SYLRA-PC\SQLEXPRESS;DataBase=quiz_games;Trusted_Connection=yes";
    @

    Double antislash is to escape the first. I tried with only one and the slash, same errors.

    Here is how I am connected in SQL Management :
    !http://uploads.siteduzero.com/files/297001_298000/297822.png(Here)!

    Thanks you.
    Sylra





  • It looks like the ODBC driver is loaded (if I understand the message in French correctly), but the connection string is incorrect. From the documentation:
    [quote]Be aware that when connecting to an ODBC datasource you must pass in the name of the ODBC datasource to the QSqlDatabase::setDatabaseName() function rather than the actual database name.[/quote]

    The documentation is a bit too well-hidden unfortunately. Read all about using QODBC "here":http://doc.qt.nokia.com/4.7/sql-driver.html.


Log in to reply
 

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