connect to sql server



  • Hi
    I was attached sql server in Qt
    As long as I connected database in sql server
    The program works correctly, but when your database
    I cut not connected(deattach) in sql server
    Not connected to the database,Error Messages:QSqlQuery::exec: database not open
    My database directory is located in the folder debug program
    code:
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setConnectOptions();
    QString dbPath = QCoreApplication::applicationDirPath() +"/managetablo.mdf";
    db.setDatabaseName("Driver={SQL Server Native Client 10.0};Server=.;AttachDbFilename"+dbPath+";Database=db;Trusted_Connection=Yes");
    db.open();
    this->model = new QSqlQueryModel();
    model->setQuery("SELECT * FROM [managetablo].[dbo].[Vistor]");
    ui->tableView->setModel(model);



  • Hi. You should use query.lastError().text() (better is to implement try - catch) to have the error details.
    Maybe with Sql Server you can use QAxObject("ADODB.Connection").

    Regards.



  • @mrdebug
    I changed my code as follows:
    ////////
    if(db.open()){
    qDebug() <<"open";

       QSqlQuery qry;
       if(qry.exec("SELECT * FROM [managetablo].[dbo].[Vistor]"))
       {
          while(qry.next())
          {
               qDebug() <<qry.value(0).toString();
          }
       }
       else
       {
           qDebug()<< "error:"<<db.lastError().text();
    
       }
       qDebug() <<"close";
       db.close();
    

    }
    else
    {
    qDebug()<< "error:"<<db.lastError().text();
    }

    }
    /////////
    When I attach the database in sql server studio
    Information is read and true But when
    database is detach,Information from databse Not Read
    Following messages from qDebug statement:
    open
    error: " "
    close



  • Hi maybe your SQL Server Browser service is not running, try starting it, usually it's located in something like C:\Program Files (x86)\Microsoft SQL Server\90\Shared\

    sqlbrowser.exe -c
    


  • @hskoglund said:

    Hi maybe your SQL Server Browser service is not running, try starting it, usually it's located in something like C:\Program Files (x86)\Microsoft SQL Server\90\Shared\

    sqlbrowser.exe -c
    

    I told you to run commands,but I received the following error message:
    sql browser:failed starting OLAP redirection services with error 1



  • Hmmm I'm guessing but try to turn off your firewall and/or antivirus program.



    • Remove Database=db; from connecting string
    • Convert Trusted_Connection=Yes to Integrated Security=SSPI; (or Integrated Security=True;)
    • Test again.


  • @Hamed.Masafi said:

    • Remove Database=db; from connecting string
    • Convert Trusted_Connection=Yes to Integrated Security=SSPI; (or Integrated Security=True;)
    • Test again.

    Like talking to you
    I connecting string made
    The message error gives the following:

    error: "[Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user ''. [Microsoft][SQL Server Native Client 10.0]Invalid connection string attribute QODBC3: Unable to connect"



  • Please try to use this Qt Sql utility
    http://www.denisgottardello.it/ADOTest/ADOTestWin32.zip
    by clicking on "Open from datasource", following the wizard and at the end trying to write and run a query in the sql field.
    If this utility works on your machine than can I can help you.



  • @mrdebug
    I could
    Connect to database
    And could query run



  • You should use the QAxObject object
    QAXOConn= new QAxObject("ADODB.Connection");
    and Ado to manage databases.
    I normally use QSqlDatabase object to manage sqlite and PostgreSql databases but with the others (SqlServer, Oracle, DBII) I normally use Ado.
    After you have implemented Ado you can manage each type of databases because all databases have the Ado drivers (PostgreSql maybe not)



  • @mrdebug said:

    QAXOConn= new QAxObject("ADODB.Connection");

    Thanks for your answer
    I was searching the ado in qt
    I could not find how to use the Internet
    You can give a sample code


Log in to reply
 

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