Using C to get at data in Access database 
Author Message
 Using C to get at data in Access database

I'm fairly new to C and am not sure if it is possible to use C to
extract data from an MS Access database.

Is this possible and has anybody any suggestions about how it is
achieved.

Many Thanks



Thu, 05 Aug 1999 03:00:00 GMT  
 Using C to get at data in Access database

Quote:

>I'm fairly new to C and am not sure if it is possible to use C to
>extract data from an MS Access database.
>Is this possible and has anybody any suggestions about how it is
>achieved.
>Many Thanks

DONT FLAME ME I KNOW THIS IS A C GROUP AND NOT A MICRO$OFT GROUP...
Here is an ODBC example....

/**********************************************************************************
 odbcapi.c - ODBC Database functions
 Author: Karl Weller

 Sample ODBC Code. Selects NAME,ADDRESS,CITY from table ADDRESSTABLE
 from datasource ODBCSourceName.  The username and password are "user"
&
 "password".  This code is for reference only and has never been
included
 in a "live" program, i.e. use at your own risk!

***********************************************************************************/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"
#include "odbcinst.h"
#include "sql.h"
#include "sqlext.h"

char sqlselecterr[] = "SQL: SELECT FAILED";

int verbose = 0;
extern int showhl7,showsql;
int stopprocessing=0;
int sqlerrorcount = 0;

int get_odbcdata(void )
{
    int retcode,rtc = -1;
    char *sqlstring;
    char szName[260],szAddress[260],szCity[260];
    long cbID1,cbID2,cbID3;

    HENV    henv=NULL;
    HDBC    hdbc=NULL;
    HSTMT   hstmt=NULL;

    sqlstring = malloc(15000);
    if (sqlstring == NULL) return(-1);
    retcode = SQLAllocEnv(&henv);              /* Allocate environment
handle */
    if (retcode == SQL_SUCCESS) {
        retcode = SQLAllocConnect(henv, &hdbc);  /* Allocate
connection handle */
        if (retcode == SQL_SUCCESS) {
            SQLSetConnectOption(hdbc, SQL_LOGIN_TIMEOUT, 90);
            retcode = SQLConnect(hdbc, "ODBCSourceName",
SQL_NTS,"user", SQL_NTS,"password", SQL_NTS);
            if (retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WITH_INFO) {
                retcode = SQLAllocStmt(hdbc, &hstmt); /* Statement
handle */
                if (retcode == SQL_SUCCESS) {
                    disp_message("ODBC OPEN HL7LOG SUCCESS");
                    SendStatusMessage("ODBC OPEN HL7LOG SUCCESS");
                    rtc=0;
                }
            }
        }
    }
    if (rtc) {
        disp_message("ODBC OPEN HL7LOG FAILED");
    }
    else {
        strcpy(sqlstring,"SELECT NAME, ADDRESS, CITY FROM ADDRESSTABLE
ORDER BY NAME");
        if (verbose) {
            disp_message(sqlstring);
        }
        retcode = SQLExecDirect(hstmt,sqlstring,strlen(sqlstring));
        if (retcode == SQL_SUCCESS) {
            SQLBindCol(hstmt, 1, SQL_C_CHAR, szName, 256, &cbID1);
            SQLBindCol(hstmt, 2, SQL_C_CHAR, szAddress, 256, &cbID2);
            SQLBindCol(hstmt, 3, SQL_C_CHAR, szCity, 256, &cbID3);
            retcode = SQL_SUCCESS;
            while(1) {
                retcode = SQLFetch(hstmt);
                if (retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WITH_INFO) {
                    szName[255] = '\0';
                    szAddress[255] = '\0';
                    szCity[255] = '\0';
/*
 do something with these here !!!
*/
                }
                else break;
            }
            SQLFreeStmt(hstmt,SQL_UNBIND);
        }
        else {
            rtc = -1;
        }

        disp_message("Closing Files");
        if (hstmt != NULL) {
            SQLFreeStmt(hstmt, SQL_DROP);
            hstmt = NULL;
        }
        if (hdbc != NULL) {
            SQLDisconnect(hdbc);
            SQLFreeConnect(hdbc);
            hdbc = NULL;
        }
        if (henv != NULL) {
            SQLFreeEnv(henv);
            henv = NULL;
        }
    }
    free(sqlstring);
    return(rtc);

Quote:
}



Fri, 06 Aug 1999 03:00:00 GMT  
 Using C to get at data in Access database


Quote:

> I'm fairly new to C and am not sure if it is possible to use C to
> extract data from an MS Access database.

    It is possible. For the specifics, check out a Windows
    programming newsgroup, but it can be done with ODBC. It's not a
    task I'd wish on someone fairly new to C, however. Perhaps you
    could use C++ instead; some vendors include pre-packaged stuff
    to use ODBC from C++, which while slow and pathetic might still
    be easier than coding it up yourself.


Sat, 07 Aug 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Copy Excel data into Access database using ADO for Visual C++

2. Using sql to insert data into Access database

3. Insert data into Access database using sql

4. Accessing Data from an Access database from a Web Server

5. Accessing Data from an Access database from a Web Server

6. Problem accessing Access 2000 database using DAO. - URGENT

7. Accessing an MS-Access database file using Visual C++ 4.0

8. getting database data w/out datagrid

9. How to access Custom Database Properties of a Access 97 database programmatically

10. Accessing data in two DIFFERENT Oracle databases via ODBC

11. accessing data returned from a database

12. C sources for reading/writing binary data to an Access database

 

 
Powered by phpBB® Forum Software