Solved

C program linking Access database file

Posted on 2001-06-16
7
1,892 Views
Last Modified: 2008-03-17
How can I link my C program to a microsoft Access database file(search,change,and add data)?What header

files do I have to include?Do you have a sample program? (Using traditional C rather than visual C).Actually,i'm using visual C++ compiler but i don't want to use the capabilities of that compiler.I want the basics that can be handeled by any compiler.
0
Comment
Question by:istiso
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6197818
Using ODBC.

There used to be an ODBC sdk available from microsoft, but they change their site all the time, so my links are always failing :(
And very likely it is already included with VC.
0
 

Author Comment

by:istiso
ID: 6197843
it is important to know the exact header file.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6198400
Try <sql.h> which declares ODBC core functions
0
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 

Author Comment

by:istiso
ID: 6198478
error C2146: syntax error : missing ';' before identifier 'SQLHWND'
0
 
LVL 10

Expert Comment

by:makerp
ID: 6199908
i have gave this exmaple to a few people now, hope it helps

this is a simple example ive knocked up for you, create a simple access or sql server database and create
a system dsn for it in control panel. the open function uses global varaibles etc so you will probably
want to chage this.

/* for db stuff */
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <stdio.h>

HENV henv;
HDBC hdbc;
int dbopen=0;

/* opens a connection using global variables , bad i know */
#define PM_DSN "your_system_dsn"
#define PM_DSN_USER "your_db_username_if_any"
#define PM_DSN_PW "your_password_if_any"

int DBopen(void)
{
    int res=0;
    RETCODE retcode;
    /*allocate the environment handle*/
    if(SQLAllocEnv(&henv)==SQL_SUCCESS)
    {
         /*allocate the connection handle*/
         if(SQLAllocConnect(henv, &hdbc)==SQL_SUCCESS)
         {
              /* Set login timeout to 5 seconds. */
              SQLSetConnectOption(hdbc, SQL_LOGIN_TIMEOUT, 5);
              SQLSetConnectOption(hdbc, SQL_CURSOR_TYPE, SQL_CURSOR_STATIC);
              /* Connect to data source */
              retcode = SQLConnect(hdbc, PM_DSN, SQL_NTS, PM_DSN_USER, SQL_NTS, PM_DSN_PW, SQL_NTS);

              if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
              {
                        res=1;
                        DATABASE_NAME = getDBName();
              }
         }
         else
         {
              SQLFreeConnect(hdbc);
         }
    }
    else
    {
         SQLFreeEnv(henv);
    }
    dbopen=res;
    return res;
}

void DBclose(void)
{
    if(dbopen)
    {
         SQLDisconnect(hdbc);
         SQLFreeConnect(hdbc);
         SQLFreeEnv(henv);
         if(DATABASE_NAME)
         {
              free(DATABASE_NAME);
         }
    }
    dbopen=0;
}
int DBexecute(char *sql,HSTMT *hstmt)
{
    int res=0;
    RETCODE retcode;

    if(SQLAllocStmt(hdbc, hstmt)== SQL_SUCCESS)
    {
         retcode=SQLPrepare(*hstmt,sql,strlen(sql));
         if(retcode==SQL_SUCCESS)
         {
              retcode=SQLExecute(*hstmt);
              if(retcode==SQL_SUCCESS)
              {
                   res=1;
              }
         }
    }
    return res;
}

void DBcloseCursor(HSTMT hstmt)
{
    SQLFreeStmt(hstmt, SQL_DROP);
}

void main()
{
    char sql[255];
    HSTMT fstmt;
    long lens;
    RETCODE retcode;
    char name[100];

    DBopen();

    sprintf(sql,"SELECT name FROM users");
    if(DBexecute(sql,&fstmt))
    {      
         SQLBindCol(fstmt,1,SQL_C_CHAR, name,sizeof(name),&lens);
         
         retcode = SQLFetch(fstmt);    
         while(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
         {
              printf("%s\n",name);
              retcode = SQLFetch(fstmt);
         }
         DBcloseCursor(fstmt);
    }
    else
    {  
         printf("DBexecute failure : %s\n",getStmtError(fstmt));    
         exit(1);
    }
    DBclose();
}
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6247604
Other way see
http://www.1cplusplusstreet.com/xq/ASP/txtCodeId.1371/lngWId.3/qx/vb/scripts/ShowCode.htm

....
//**************************************
//    
// Name: Access MDB
// Description:To open MS Access or Ms S
//     QL server database using C/C++ or VC++
// By: Mokarrabin A Rahman
//
// Returns:Shows some recordsets
//
// Assumes:Change the name of the databa
//     se to any database and place it in the s
//     ame directory as the .exe. You shoul als
//     o change the SQL to your particular data
//     base. You may change the connect string
//     to connect to MS SQL Server.
//
// Side Effects:Great
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.1CPlusPlusStreet.com/xq/ASP/txtCodeI
//     d.1371/lngWId.3/qx/vb/scripts/ShowCode.h
//     tm//for details.//**************************************
//    

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
// This code comes from : www.geocities.
//     com/mokarrabin
#include <stdio.h>
#include <iostream.h>
void main(void)


    {
    CoInitialize(NULL);
    try


        {
        _RecordsetPtr pRst("ADODB.Recordset");
        // Connection String
        _bstr_t strCnn("DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;DBQ=GBOOK.mdb");
              // Open table
             pRst->Open("SELECT * FROM ProductService where ProductService like '%samir%';", strCnn, adOpenStatic, adLockReadOnly, adCmdText);
             
              pRst->MoveFirst();


                  while (!pRst->EndOfFile) {
                       cout<<(char*) ((_bstr_t) pRst->GetFields()->GetItem("ProductService")->GetValue())<<endl;
                       pRst->MoveNext();
                  }
                  pRst->Close();
                 
        }
        catch (_com_error &e)


            {
            cout<<(char*) e.Description();
        }
        ::CoUninitialize();
    }
....
alex

0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 50 total points
ID: 6351383
No reaction, I hope, you solved your problems.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question