IT Community - Software Programming, Web Development and Technical Support

How to connect C with DataBase?

This is a discussion on How to connect C with DataBase? within the C and C++ Programming forums, part of the Software Development category; Hi Frnds, Any possiblilities their in C to connect DataBase. If possibilities their Explain Any one to me? yuva...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 09-24-2007, 03:53 AM
yuva yuva is offline
D-Web Trainee
 
Join Date: Sep 2007
Posts: 3
yuva is on a distinguished road
Default How to connect C with DataBase?

Hi Frnds,

Any possiblilities their in C to connect DataBase. If possibilities their Explain Any one to me?


yuva
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-24-2007, 02:23 AM
seesamjagan seesamjagan is offline
D-Web Programmer
 
Join Date: Aug 2007
Location: Chennai
Posts: 66
seesamjagan is on a distinguished road
Send a message via AIM to seesamjagan Send a message via Yahoo to seesamjagan
Default Re: How to connect C with DataBase?

hi

i think there is no possibilities in c to connect the database..

one possible solution is ..... use Structures & Structure pointers to simulate the above requirements
__________________
SeeSamJagan
- Sky is not the "LIMIT", Death is not the END, There is still something beyond....
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-24-2007, 03:00 AM
Sundaram Sundaram is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Location: chennai
Posts: 117
Sundaram is on a distinguished road
Send a message via MSN to Sundaram Send a message via Yahoo to Sundaram
Smile Re: How to connect C with DataBase?

Hi ,
Oracle provides a tool as "pro C".that is used to coonect oracle to C program.
while in other databases there must be some native calls(API of database) provided by that database.using these calls you can connect that database.

regards,

M.Sundaram
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-30-2007, 10:34 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: How to connect C with DataBase?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlcli1.h>
#include "utilcli.h" /* Header file for CLI sample code */

int DbBasicConnect(SQLHANDLE, char *, char *, char *);
int DbDriverConnect(SQLHANDLE, char *, char *, char *);
int DbBrowseConnect(SQLHANDLE, char *, char *, char *);

int main(int argc, char *argv[])
{
SQLRETURN cliRC = SQL_SUCCESS;
int rc = 0;
SQLHANDLE henv; /* environment handle */
SQLHANDLE hdbc; /* connection handle */

char dbAlias[SQL_MAX_DSN_LENGTH + 1];
char user[MAX_UID_LENGTH + 1];
char pswd[MAX_PWD_LENGTH + 1];

/* check the command line arguments */
rc = CmdLineArgsCheck1(argc, argv, dbAlias, user, pswd);
if (rc != 0)
{
return 1;
}

printf("\nTHIS SAMPLE SHOWS ");
printf("HOW TO CONNECT TO AND DISCONNECT FROM A DATABASE.\n");

/* allocate an environment handle */
cliRC = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
if (cliRC != SQL_SUCCESS)
{
printf("\n--ERROR while allocating the environment handle.\n");
printf(" cliRC = %d\n", cliRC);
printf(" line = %d\n", __LINE__);
printf(" file = %s\n", __FILE__);
return 1;
}

/* set attribute to enable application to run as ODBC 3.0 application */
cliRC = SQLSetEnvAttr(henv,
SQL_ATTR_ODBC_VERSION,
(void *)SQL_OV_ODBC3,
0);
ENV_HANDLE_CHECK(henv, cliRC);

/* connect to a database with SQLConnect() */
/* this is the basic connection */
rc = DbBasicConnect(henv, dbAlias, user, pswd);
if (rc != 0)
{
return rc;
}

/* connect to a database with SQLDriverConnect() */
rc = DbDriverConnect(henv, dbAlias, user, pswd);
if (rc != 0)
{
return rc;
}

/* connect to a database with SQLBrowseConnect() */
rc = DbBrowseConnect(henv, dbAlias, user, pswd);
if (rc != 0)
{
return rc;
}

/* free the environment handle */
cliRC = SQLFreeHandle(SQL_HANDLE_ENV, henv);
ENV_HANDLE_CHECK(henv, cliRC);

return 0;
} /* main */

/* connect to a database with a basic connection using SQLConnect() */
int DbBasicConnect(SQLHANDLE henv,
char db1Alias[],
char user[],
char pswd[])
{
SQLRETURN cliRC = SQL_SUCCESS;
int rc = 0;
SQLHANDLE hdbc; /* connection handle */

printf("\n-----------------------------------------------------------");
printf("\nUSE THE CLI FUNCTIONS\n");
printf(" SQLAllocHandle\n");
printf(" SQLConnect\n");
printf(" SQLDisconnect\n");
printf(" SQLFreeHandle\n");
printf("TO CONNECT TO AND DISCONNECT FROM A DATABASE:\n");

/* allocate a database connection handle */
cliRC = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
ENV_HANDLE_CHECK(henv, cliRC);

printf("\n Connecting to the database %s ...\n", db1Alias);

/* connect to the database */
cliRC = SQLConnect(hdbc,
(SQLCHAR *)db1Alias,
SQL_NTS,
(SQLCHAR *)user,
SQL_NTS,
(SQLCHAR *)pswd,
SQL_NTS);
DBC_HANDLE_CHECK(hdbc, cliRC);
printf(" Connected to the database %s.\n", db1Alias);

/********* Start using the connection *************************/

/********* Stop using the connection **************************/

printf("\n Disconnecting from the database %s...\n", db1Alias);

/* disconnect from the database */
cliRC = SQLDisconnect(hdbc);
DBC_HANDLE_CHECK(hdbc, cliRC);

printf(" Disconnected from the database %s.\n", db1Alias);

/* free the connection handle */
cliRC = SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
DBC_HANDLE_CHECK(hdbc, cliRC);

return 0;
} /* DbBasicConnect */

/* connect to a database with additional connection parameters
using SQLDriverConnect() */
int DbDriverConnect(SQLHANDLE henv,
char db1Alias[],
char user[],
char pswd[])
{
SQLRETURN cliRC = SQL_SUCCESS;
int rc = 0;
SQLHANDLE hdbc; /* connection handle */
SQLCHAR connStr[255];

printf("\n-----------------------------------------------------------");
printf("\nUSE THE CLI FUNCTIONS\n");
printf(" SQLAllocHandle\n");
printf(" SQLDriverConnect\n");
printf(" SQLDisconnect\n");
printf(" SQLFreeHandle\n");
printf("TO CONNECT TO AND DISCONNECT FROM A DATABASE:\n");

/* allocate a database connection handle */
cliRC = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
ENV_HANDLE_CHECK(henv, cliRC);

printf("\n Connecting to the database %s ...\n", db1Alias);
sprintf((char *)connStr,
"DSN=%s; UID=%s; PWD=%s; AUTOCOMMIT=0; CONNECTTYPE=1;",
db1Alias, user, pswd);

/* connect to a data source */
cliRC = SQLDriverConnect(hdbc,
(SQLHWND)NULL,
connStr,
SQL_NTS,
NULL,
0,
NULL,
SQL_DRIVER_NOPROMPT);
DBC_HANDLE_CHECK(hdbc, cliRC);

printf(" Connected to the database %s.\n", db1Alias);

/********* Start using the connection *************************/

/********* Stop using the connection **************************/

printf("\n Disconnecting from the database %s...\n", db1Alias);

/* disconnect from the database */
cliRC = SQLDisconnect(hdbc);
DBC_HANDLE_CHECK(hdbc, cliRC);

printf(" Disconnected from the database %s.\n", db1Alias);

/* free the connection handle */
cliRC = SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
DBC_HANDLE_CHECK(hdbc, cliRC);

return 0;
} /* DbDriverConnect */

/* connect to a database iteratively using SQLBrowseConnect() */
int DbBrowseConnect(SQLHANDLE henv,
char db1Alias[],
char user[],
char pswd[])
{
SQLRETURN cliRC = SQL_SUCCESS;
int rc = 0;
SQLHANDLE hdbc; /* connection handle */
SQLCHAR connInStr[255]; /* browse request connection string */
SQLCHAR outStr[1025]; /* browse result connection string*/
SQLSMALLINT indicator; /* number of bytes to return */
int count = 1;

printf("\n-----------------------------------------------------------");
printf("\nUSE THE CLI FUNCTIONS:\n");
printf(" SQLAllocHandle\n");
printf(" SQLBrowseConnect\n");
printf(" SQLDisconnect\n");
printf(" SQLFreeHandle\n");
printf("TO CONNECT TO AND DISCONNECT FROM A DATABASE:\n");

/* allocate a database connection handle */
cliRC = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
ENV_HANDLE_CHECK(henv, cliRC);

/* connect to the database */
printf("\n Connecting to the database %s ...\n", db1Alias);
sprintf((char *)connInStr,
"DSN=%s; UID=%s; PWD=%s; AUTOCOMMIT=0;", db1Alias, user, pswd);

/********* Start using the connection *************************/

cliRC = SQL_NEED_DATA;
while (cliRC == SQL_NEED_DATA)
{
/* get required attributes to connect to data source */
cliRC = SQLBrowseConnect(hdbc,
connInStr,
SQL_NTS,
outStr,
sizeof(outStr),
&indicator);
DBC_HANDLE_CHECK(hdbc, cliRC);

printf(" So far, have connected %d times to database %s\n",
count++, db1Alias);
printf(" Resulting connection string: %s\n", outStr);

/* if inadequate connection information was provided, exit
the program */
if (cliRC == SQL_NEED_DATA)
{
printf(" You can provide other connection information "
"here by setting connInStr\n");
break;
}

/* if the connection was successful, output confirmation */
if (cliRC == SQL_SUCCESS)
{
printf(" Connected to the database %s.\n", db1Alias);
}
}

/********* Stop using the connection **************************/

printf("\n Disconnecting from the database %s...\n", db1Alias);

/* disconnect from the database */
cliRC = SQLDisconnect(hdbc);
DBC_HANDLE_CHECK(hdbc, cliRC);

printf(" Disconnected from the database %s.\n", db1Alias);

/* free the connection handle */
cliRC = SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
DBC_HANDLE_CHECK(hdbc, cliRC);

return 0;
} /* DbBrowseConnect */
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-21-2008, 08:35 PM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Smile Re: How to connect C with DataBase?

The easiest way to access an Access database from C++ is to use ODBC. An access database can easily be setup as an ODBC data source.

(OR)

Earlier the program is using MS access database and code is like below.

CDaoDatabase* pDB = NULL;
pDB = new CDaoDatabase;
char query[256], *name;
try
{
// Force usage of latest DAO engine
AfxGetModuleState()->m_dwVersion = 0x0601;
AfxDaoInit();

char *dbPath = new char[strlen(envVar) + 40];
sprintf(dbPath, "%s\\MilkDB.mdb",envVar );
pDB->Open(dbPath);
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/c-c-programming/3940-how-connect-c-database.html
Posted By For Type Date
c# programming: Blogs, Photos, Videos and more on Technorati This thread Refback 09-24-2007 08:58 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between Regular database connection and Persistent Database Connections? Falcon PHP Programming 1 11-03-2007 04:13 AM
Can I connect to a Microsoft Access database without a DSN itbarota PHP Programming 0 09-10-2007 11:30 PM
How to connect FTP Server using Perl? raj Perl 1 07-20-2007 02:45 AM
How to connect a Database while closing the browser window in asp .net ? oxygen ASP and ASP.NET Programming 5 07-20-2007 02:20 AM
problem when connect mysql for php Jeyaseelansarc PHP Programming 1 05-20-2007 09:39 PM


All times are GMT -7. The time now is 06:52 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0