NO

Author Topic: Connect to MS SQL database  (Read 5420 times)

Zodra1

  • Guest
Connect to MS SQL database
« on: July 14, 2008, 09:05:19 AM »
After three days of trying I gave up! Can not compile a very simple code:

#include <stdio.h>
#include <sql.h>
#include <sqlext.h>

main() {
#define WIN32_LEAN_AND_MEAN
  SQLHENV env;
  char driver[256];
  char attr[256];
  SQLSMALLINT driver_ret;
  SQLSMALLINT attr_ret;
  SQLUSMALLINT direction;
  SQLRETURN ret;

  SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);

  direction = SQL_FETCH_FIRST;
  while(SQL_SUCCEEDED(ret = SQLDrivers(env, direction,
                   driver, sizeof(driver), &driver_ret,
                   attr, sizeof(attr), &attr_ret))) {
    direction = SQL_FETCH_NEXT;
    printf("%s - %s\n", driver, attr);
    if (ret == SQL_SUCCESS_WITH_INFO) printf("\tdata truncation\n");
  }
}

I get about three or four errors per statement; most of them are from include files. Below is a paste of some 10 lines of errors:
Building ConnecttoSQL.obj.
C:\Program Files\PellesC\Include\Win\sqltypes.h(73): warning #2099: Missing type specifier.
C:\Program Files\PellesC\Include\Win\sqltypes.h(73): error #2001: Syntax error: expected ';' but found 'SQLHWND'.
C:\Program Files\PellesC\Include\Win\sqltypes.h(73): warning #2099: Missing type specifier.
C:\Program Files\PellesC\Include\Win\sqltypes.h(75): error #2121: Redeclaration of 'SQLSCHAR'.
C:\Program Files\PellesC\Include\Win\sqltypes.h(163): warning #2099: Missing type specifier.
C:\Program Files\PellesC\Include\Win\sqltypes.h(163): error #2001: Syntax error: expected ';' but found 'SQLBIGINT'.
C:\Program Files\PellesC\Include\Win\sqltypes.h(163): warning #2099: Missing type specifier.
C:\Program Files\PellesC\Include\Win\sqltypes.h(164): error #2121: Redeclaration of '__int64'.
C:\Program Files\PellesC\Include\Win\sqltypes.h(164): error #2001: Syntax error: expected ';' but found 'SQLUBIGINT'.
C:\Program Files\PellesC\Include\Win\sqltypes.h(164): warning #2099: Missing type specifier.
C:\Program Files\PellesC\Include\Win\sqltypes.h(180): error #2078: Invalid struct field declarations.
C:\Program Files\PellesC\Include\Win\sqltypes.h(180): error #2001: Syntax error: expected '}' but found 'DWORD'.
C:\Program Files\PellesC\Include\Win\sqltypes.h(181): warning #2099: Missing type specifier.
C:\Program Files\PellesC\Include\Win\sqltypes.h(181): error #2001: Syntax error: expected ';' but found 'Data2'.

Must be that I am doing something very very wrong. There is also one peculiar thing: when i type the program lines all letters are in uppercase. No, I don't have caps lock :) as everyone can see from this text. I suspect I have really beginners troubles, which have been already solved before, but i can't find solution in this forum.

Please heeeeeeelp!!!!

Zodra1

  • Guest
Re: Connect to MS SQL database
« Reply #1 on: July 14, 2008, 09:21:33 AM »
Ah! For uppercase question i found an add-in that converts everything in uppercase, and it was checked! So a minor problem solved.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Connect to MS SQL database
« Reply #2 on: July 14, 2008, 09:31:34 AM »
Use header files in correct order and compile with Microsoft compiler option.

Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <stdio.h>

May the source be with you

Zodra1

  • Guest
Re: Connect to MS SQL database
« Reply #3 on: July 14, 2008, 11:00:22 AM »
Many thanks again. I am now a little embarrassed as i ask so many questions. Compile went OK but now I am having trouble in build phase:

POLINK: error: Unresolved external symbol '_SQLAllocHandle'.
POLINK: error: Unresolved external symbol '_SQLSetEnvAttr'.
POLINK: error: Unresolved external symbol '_SQLDrivers'.
POLINK: fatal error: 3 unresolved external(s).

I must admit that this is practically my first real programming in windows environment. Linker obviously can't resolve three calls to some modules. I would gladly looked for those libs, but don't know where, how...

Zodra1

  • Guest
Re: Connect to MS SQL database
« Reply #4 on: July 14, 2008, 11:04:18 AM »
At last! I managed to find missing dll; it was in the sql.h include file; so many tanks to you again.