2

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.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129

3 Answers3

4

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;
    }

    ....
}
vlukham
  • 409
  • 4
  • 11
  • aww snap, i forgot about QT. Let me take a look to see see if it is sufficient for what i am doing. – Fallenreaper Nov 05 '12 at 19:51
  • then you simply do your query `#include ` `{ QSqlQuery query; query.prepare("SELECT * FROM ... WHERE ..."); query.exec(); while(query.next()) { int test = query.value(0).toInt(); } query.finish(); }` – vlukham Nov 05 '12 at 19:53
  • Are there better ways then using QT. I know my one bud uses it mostly just for GUI's but i wasnt sure if it a good tool for what i am trying to do. – Fallenreaper Nov 05 '12 at 19:55
  • http://stackoverflow.com/questions/9336282/porting-win32-code-windows-h-to-linux probably you should't use windows.h functions or replace it with posix – vlukham Nov 05 '12 at 20:10
1

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.

TallTed
  • 9,069
  • 2
  • 22
  • 37
1

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;
}
vkalit
  • 647
  • 8
  • 19