trying to connect to MSSQL through a C++ ODBC connection but cant find the right demo

11,745

Solution 1

with c++ i reccomend use QT library http://qt-project.org/downloads

...
#include <QtSql>
...

int main(int argc, char *argv[])
{
    ....

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("DRIVER={SQL Server};Server=YOUR SERVER;Database=YOUR DB NAME;User ID=YOUR USER;Password=YOUR PASS;Trusted_Connection=yes;");

    if(!db.open())
    {
       qDebug() << db.lastError().text();

       return 0;
    }

    ....
}

Solution 2

In stdafx.h:

#pragma once

#include <windows.h> //!first include
#include <sqltypes.h> //!

#include "targetver.h"
#include <stdio.h>
#include <tchar.h>

In yourMainFile.cpp:

 #include "stdafx.h"
 #include <iostream>
 #include <sql.h>
 #include <sqlext.h>

 using namespace std;

 int _tmain(int argc, _TCHAR* argv[])
 {
    RETCODE rc;        // ODBC return code 
    HENV henv;         // Environment 
    HDBC hdbc;         // Connection handle 
    HSTMT hstmt;       // Statement handle 
    SDWORD cbData;     // Output length of data

    // attempt to connect to the ODBC database 
    char db[] = "MSSQLSERVER"; // Server name 
    cout << "Attempting to open database " << db << "..." << endl; 
    SQLAllocEnv(&henv); 
    SQLAllocConnect(henv, &hdbc); 
    rc = SQLConnect(hdbc, (unsigned char*)db, SQL_NTS, 0, 0, 0, 0);
    cout << rc << endl;

    // if not successful, quit 
    if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO)) 
    { 
        cout << "Cannot open database -- make sure ODBC is configured properly."      << endl; 
        SQLFreeConnect(hdbc); 
        SQLFreeEnv(henv); 
        cout << "Press ENTER to continue." << endl; 
        cin.get(); 
        return 1; 
    }
    cout << "Hello, SQL-Server!" << endl;
    return 0;
}

Solution 3

Microsoft's documentation is of limited use, once you move off Windows. Their discussion of the ODBC APIs is fine -- but anything about Visual Studio or other Windows-specific components should generally be ignored.

iODBC.org can provide some pointers -- especially if you look into the source for iODBC Demo.app and/or iODBC Test.command, which ship with the free and open source iODBC SDK.

You may also benefit from developing and testing with a commercially-supported ODBC driver, such as my employer's offering.

Share:
11,745
Fallenreaper
Author by

Fallenreaper

Hacker, Programmer, software Dev, Software Engineer.. see my Twitter and website for more details.

Updated on June 04, 2022

Comments

  • Fallenreaper
    Fallenreaper almost 2 years

    I am programming on my macbook pro and need to connect to my company's MSSQL server for programming a commercial product.

    How do you actually connect to it? I was looking on the MSDN website, and i didnt quite get it.

    for the purposes of my demo, i was just going to create a new project within XCODE and just create a console application which will output the data. Once that it set up, ill implement different things with the connection.

    Edit: Added some code:

    #include <iostream>
    //#include <windows.h>
    #include <sqlext.h>
    #include <sqltypes.h>
    #include <sql.h>
    
    using namespace std;
    
    int main(int argc, const char * argv[])
    {
    SQLHENV hEnv;
    SQLHDBC hDbc;
    string connection = "AAA";
    string db = "DB";
    string user = "user";
    string pass = "password";
    string data = "DRIVER={SQL Server Native Client 11.0};SERVER="+connection+";DATABASE="+db+";UID="+user+";PWD="+pass+";";
    //SQLCHAR* pwszConnStr = (SQLCHAR*)("Driver={SQL Server Native Client 11.0};Server="+connection+";Database="+db+";Uid="+user+";Pwd="+pass+";");
    SQLCHAR* pwszConnStr = (SQLCHAR*)data.c_str();
    //cout  << data << endl;
    cout  << pwszConnStr << endl;
    //error seems to occur in 1 of the 3 SQL statements below.
    SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
    SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
    SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc);
    
    RETCODE rc = SQLDriverConnect(hDbc, NULL, pwszConnStr, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_PROMPT);
    
    if(rc == SQL_SUCCESS){
        cout << "Connected to the Database" << endl;
    }else{
        cout << "No Connection Established" << endl;
    }
    
    
    return 0;
    }
    

    It fails to compile, and i am thinking it is related to me commenting out the windows.h file. The issue is that windows.h is not found on my macbook pro and figure it is when developing in VStudios.