Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:19:28+00:00 2026-06-12T02:19:28+00:00

I have a server of windows server 2003 and its IP on my local

  • 0

I have a server of windows server 2003 and its IP on my local network is 192.168.1.220
This server has SOL server 2005 express edition installed. This SQL server has a database called amir.

I want to connect to it from a Linux client on the same network.
SQL server service using port 1617 on my server and I used this port to connect to the server using java.

Wow i want to use QT C++ but my code doesn’t work.

This is my code:

#include <QtCore/QCoreApplication>
#include <iostream>
#include <QSqldatabase>
#include <QSqldriver>
int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);
   QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
   bool test=db.isValid();//true
   test=db.isDriverAvailable("QODBC");//true
   db.setHostName("192.168.1.220\\SQLEXPRESS");
   db.setDatabaseName("DRIVER={SQL Server};SERVER=192.168.1.220\\SQLEXPRESS:1617;DATABASE=amir");
   db.setUserName("sa");
   db.setPassword("amir");
   db.setPort(1617);
   test=db.isValid();//true
   if(!db.open())
   {
      cout<<endl<<"not connected"<<endl;
      QString error=db.lastError().text();
      cout<<error.toLocal8Bit().data();
      return false;
   }
   else
      cout<<endl<<"connected"<<endl;
      return true;
 }

Every time i try this it out “not connected” and the error is

[unixODBC][Driver Manager]Data source name not found, and no default driver specified QODBC3: Unable to connect

Using these parameters I can connect using java

So what is the wrong here? and if there is another fasting way to connect to the SQL server using qt c++ than ODBC driver.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:19:29+00:00Added an answer on June 12, 2026 at 2:19 am

    You need to setup a data source name on your server to connect through ODBC. Here is some code that I use to setup a DSN:

    QString SQLServerProvider::buildDSN(QString server, QString database, QString username, QString password)
    {
    #ifdef Q_WS_MACX
        QString dsn = QString("DRIVER=/usr/local/lib/libtdsodbc.so;SERVER=%1;TDS_VERSION=8pClient;DATABASE=%2;PORT=1433;UID=%3;PWD=%4;").arg(server).arg(database).arg(username).arg(password);
    #endif
    
    #ifdef Q_WS_X11
        QString dsn = QString("DRIVER={FreeTDS};SERVER=%1;TDS_VERSION=8.0;PORT=1433;DATABASE=%2;UID=%3;PWD=%4;").arg(server).arg(database).arg(username).arg(password);
    #endif
    
    #ifdef Q_WS_WIN
        QString dsn = QString("DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;UID=%3;PWD=%4;").arg(server).arg(database).arg(username).arg(password);
    #endif
        return dsn;
    }
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", databaseName);
    db.setDatabaseName(buildDSN(server, database, username, password));
    

    Here is some code that I forgot to put in the initial post:

        #ifdef Q_WS_X11
        QString dir = QDir::homePath();
        QDir d;
        QString libdir = d.absolutePath();
    
        QFile odbcinst(dir + "/.odbcinst.ini");
        if(!odbcinst.exists())
        {
            odbcinst.open(QIODevice::WriteOnly | QIODevice::Text);
            QTextStream out(&odbcinst);
            out << "[FreeTDS]\n";
            out << "Description = v0.91 with protocol v8.0\n";
            out << "Driver = " + libdir + "/libtdsodbc.so\n";
            out << "Setup = " + libdir + "/libtdsodbc.so\n";
            out << "FileUsage = 1";
            odbcinst.close();
        }
        else
        {
            QList<QString> lines;
    
            odbcinst.open(QIODevice::ReadOnly | QIODevice::Text);
            QTextStream readfile(&odbcinst);
    
            int i = 0, lnbr = 0;
            bool found = false;
            while(!readfile.atEnd())
            {
                QString line = readfile.readLine();
                if(line.contains("[FreeTDS]"))
                {
                    lnbr = i;
                    found = true;
                }
                lines.append(line);
                i++;
            }
            odbcinst.close();
    
            // append to end
            if(!found)
            {
                // append to the end
                odbcinst.open(QIODevice::Append | QIODevice::Text);
                QTextStream file(&odbcinst);
    
                file << "\n[FreeTDS]\n";
                file << "Description = v0.91 with protocol v8.0\n";
                file << "Driver = " + libdir + "/libtdsodbc.so\n";
                file << "Setup = " + libdir + "/libtdsodbc.so\n";
                file << "FileUsage = 1";
                odbcinst.close();
            }
            else // update existing entry
            {
                qDebug() << "Found an entry for FreeTDS. Updating driver to " + libdir + "/libtdsodbc.so.";
                qDebug() << lines[lnbr+2];
                qDebug() << lines[lnbr+3];
    
                lines.replace(lnbr + 2, "Driver = " + libdir + "/libtdsodbc.so");
                lines.replace(lnbr + 3, "Setup = " + libdir + "/libtdsodbc.so");
    
                QString text;
                for(int j = 0; j < lines.count(); j++)
                {
                    text.append(lines[j] + "\n");
                }
    
                odbcinst.open(QIODevice::WriteOnly | QIODevice::Text);
                QTextStream updatefile(&odbcinst);
                updatefile << text;
                odbcinst.close();
            }
    
        }
    #endif
    

    This code creates the .odbcinst.ini file in your home directory if it doesn’t exist and adds an entry for FreeTDS. If it does exist, it will append to the end of the file. If an entry for FreeTDS exists in the file already, it will update the existing file. Here’s a guide for setting up FreeTDS if you haven’t already: http://pzuk.wordpress.com/2012/02/03/how-to-make-freetds-unixodbc-and-qt-working-together/

    Note, that the code for configuring FreeTDS that I posted is only required if you want to bundle FreeTDS with your application and have the libary path setup correctly from where you launch. It runs as a standard user and not as root so everything is done in the local user account.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a windows 2003 server (WEb edition) which has .net 3.5 sp1 installed
I have a VPN in my office. One machine has windows server 2003 installed.
I have a windows 2003 server running IIS 6. IIS has roughly 120 sites
I have a Windows Server 2003 64-Bit VPS, and I'm experiencing a problem with
I have a Windows Server 2003 system that is used for terminal services. We
I have three Windows Server 2003 with 2 GB RAM. Server1 tomcat 5.5.25 jvm
I have an iis server on a windows 2003 production machine that will not
I have a Windows 2003 Server with IIS, I installed VisualSVN Server on it.
I have 1 PC (Windows Server 2003 + SQL Server 2008) and 2 PCs
I have a virtual machine running windows 2003 server. It is on a separate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.