roell
asked on
C Connection to MS Access
How to connect to ms access database.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
}
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_C HAR, 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);
}
DBclose();
}
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
{
/*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;
}
}
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,
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_C
retcode = SQLFetch(fstmt);
while(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
printf("%s\n",name);
retcode = SQLFetch(fstmt);
}
DBcloseCursor(fstmt);
}
DBclose();
}
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept DrMaltz' comment as answer
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Kent (Kdo)
EE Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
Accept DrMaltz' comment as answer
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Kent (Kdo)
EE Cleanup Volunteer
ASKER
the header files and the other files needed.