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...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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.... |
| |||
| 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 |
| |||
| #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... |
| |||
| 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); } |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |