Visual Studio Qt 5.11.2 SQL Server Express 2017 Driver



  • Hello!
    I am facing a problem with the ODBC connection. I am trying to connect to my SQL Server Express 2017 Instance, but it seems that i am not able to do it. (There are no errors/warnings).

    This is my code:

    #include "f1_system.h"
    #include <ActiveQt/qaxobject.h>
    #include <QtSql/qsqldatabase.h>
    
    F1_System::F1_System(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    
    	connect(ui.loginPushButton, SIGNAL(clicked()), this, SLOT(validateLogin()));
    }
    
    QString F1_System::connect2DB() {
    	QString connectionString = QStringLiteral("Driver={SQL Server Native Client 17.0 };Server=DESKTOP-4K9MAS2\\SQLEXPRESS01;Database=Formula1_BD;Trusted_Connection=yes;");
    	QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    	
    	db.setDatabaseName(connectionString);
    	if (db.open())
    		return QString("Connected");
    	else
    		return QString("Not Connected!");
    	
    }
    
    void F1_System::validateLogin() {
    	
    	ui.label->setText(connect2DB());
    }
    

    It keeps changing the text to "Not Connected!".
    Could someone let me know what is the problem?

    [Later edit]
    -- SOLVED --
    I solved it by replacing

    Driver = {SQL Server Native Client 17.0 }
    

    with

    Driver = {SQL Server}
    

    I don't really understand why it is working now, so now I could use some explanations on it. (or, at least, some hints)

    Thank you!



  • The Driver parameter in the connection string has to exactly match the driver name in ODBC admin drivers page.

    0_1542182004593_odbc.PNG

    Maybe you have some typo. And even this is windows, the name is case sensitive!



  • @sneubert Thank you! I undestand now what happened there.



  • @Ovidiu_GCO
    {SQL Server Native Client 17.0 };
    What is with the blank in front of "}"


Log in to reply
 

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