Solved

Database access through C

Posted on 2004-04-01
9
282 Views
Last Modified: 2010-04-15
How to access MS-Access database details througn "C"?
0
Comment
Question by:karthikeyan_ns2003
9 Comments
 
LVL 5

Expert Comment

by:g0rath
ID: 10731824
What OS/Compiler will you be using for this project?
0
 
LVL 24

Accepted Solution

by:
fridom earned 34 total points
ID: 10732072
I assume he wants to use windows, so he should check ODBC. E.g from here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_core_database_topics_.28.odbc.29.asp

Regards
Friedrich
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 33 total points
ID: 10732364
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:ankuratvb
ID: 10732408
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10732422
Of course,the solution u want would depend on ur platform and compiler.

The EE links provide help for various compilers.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10738734
ankuratvb, I can't help thinking that I see more oldlook EE links in the C TA than the C++ TA.... funy thing really ;-)
0
 

Assisted Solution

by:agri_amit
agri_amit earned 33 total points
ID: 10739487
Hi karthikeyan_ns2003,

U can use the following code .
This is desgined on the basis of ODBC interaction to Databse.Create a odbc for ur ms-access databse with name "conn" ,and now this code will work . Here the assumption is that u need usename 'sa' and password 'sa' to communicate to database.
This program will fetch the 1 column data from cq_employee table and will write to txt file.


#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <odbcss.h>

#define NAME_LEN 50

SQLCHAR      szName[NAME_LEN];
SQLINTEGER   cbName;
SQLRETURN    retcode;
SQLHENV      henv = SQL_NULL_HENV;
SQLHDBC      hdbc1 = SQL_NULL_HDBC;
SQLHSTMT      hstmt1 = SQL_NULL_HSTMT;
SQLSMALLINT  colnum=0;
SQLCHAR *    colname;
SQLSMALLINT   buflen;//=sizeof(colname);
SQLSMALLINT *    nlptr;  
SQLSMALLINT *    dtptr;
SQLUINTEGER *    colsptr;
SQLSMALLINT *    dec;
SQLSMALLINT *    nptr;


FILE *fp;

int main() {
   RETCODE retcode;
   UCHAR   szDSN[SQL_MAX_DSN_LENGTH+1] = "conn",
         szUID[MAXNAME] = "sa",
         szAuthStr[MAXNAME] = "sa";
   fp=fopen("sqlquery.txt","w+");

    // Allocate the ODBC Environment and save handle.
   
   retcode = SQLAllocEnv (&henv);
   // Notify ODBC that this is an ODBC 3.0 application.
   
    // Allocate an ODBC connection handle and connect.
   retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
   retcode = SQLConnect(hdbc1, szDSN, (SWORD)strlen(szDSN),szUID, (SWORD)strlen(szUID),szAuthStr, (SWORD)strlen(szAuthStr));
   if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) )
   {
         // Connect failed, call SQLGetDiagRec for errors.
   }
   else {
     
        retcode =  SQLAllocHandle(SQL_HANDLE_STMT,hdbc1, &hstmt1);

retcode = SQLExecDirect(hstmt1,"SELECT * FROM cq_employee",SQL_NTS);

retcode=SQLDescribeCol(hstmt1,1,colname,25,&nptr,&dtptr,colsptr,dec,nptr);

printf("%u \n",dtptr);

if (retcode == SQL_SUCCESS) {
   while (TRUE) {
      retcode = SQLFetch(hstmt1);
      if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
//        printf("Error!");
      }
      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){
     
         
         SQLGetData(hstmt1, 1, SQL_C_CHAR, szName, NAME_LEN, &cbName);
       

         /* Print the row of data */
           
           fprintf( fp,"%s \n",szName);
      } else {
         break;
      }
   }
}

   }

   // Allocate statement handles and do ODBC processing.
   /* Clean up. */
   SQLDisconnect(hdbc1);
   SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
   SQLFreeHandle(SQL_HANDLE_ENV, henv);
   fclose(fp);
   return(0);
}

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

708 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now