Connecting database using C++ Programming

Asked By 0 points N/A Posted on -
qa-featured

I have a problem about how will I connect my Program or Software to the Database using C++ Programming Language.

Do I have to make a code for connecting my database?
How can I also create a database and what database that could be compatible with C++ Programming?

SHARE
Best Answer by Franks good
Answered By 0 points N/A #126114

Connecting database using C++ Programming

qa-featured

C++ programming language is a high level language that you must remember it cannot run from first to an end. Example: #include<stdio.h>char*suits[4]=("Brown,"Yellow","Red","Black")char values.You can use code in C++ programming language.You should write your database into code language of C++ program.Thus install your C++ program software.Could you learn the basic code of C++ program.If you don't know the exact code then how you can be able to make your database into code.C++ programming language is a difficult programming language.Class colours int sidx.vidx.colours.

Best Answer
Best Answer
Answered By 15 points N/A #126115

Connecting database using C++ Programming

qa-featured

Just follow these steps.

  • Include the Header Files
  • Open a Connection to a Database
  • Choose Driver.
  • Query the Database
  • Extract the Data out of the Executed Query
  • Traverse Through the Results
  • Close the Statement and the Connection

 

//Header files:
 
#include <sql.h>
#include<sqltypes.h>
#include<sqlext.h>
...
....
 
//Declaration:
 
SQLHandle hdlEnv, hdlConn, hdlStmt, hdlDbc
char* stmt = "SELECT * from NutHead"; //SQL statementß NutHead is the Table name 
 
//for example
char *dsnName = “COLLECTOR” ß name of your program or what ever….. 
char* userID = "eXceed";
char* passwd = "hole";
char* retVal[256];
unsigned int cbData;
 
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hdlEnv); 
SQLSetEnvAttr(hdlEnv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC30);
SQLAllocHandle(SQL_HANDLE_DBC, hdlEnv, &hdlConn); 
SQLConnect(hdlConn, (SQLCHAR*)dsnName,SQL_NTS,(SQLCHAR*)userID,SQL_NTS, (SQLCHAR*)passwd, SQL_NTS);
SQLAllocHandle(SQL_HANDLE_STMT, hdlDbc, &hdlStmt);
SQLExecDirect(hdlStmt, (SQLCHAR*)stmt, SQL_NTS); 
 
 
//Initialize the database connection
 
while(SQLFetch(hdlStmt) == SQL_SUCCEEDED)
{
  SQLGetData(hdlStmt,0,DT_STRING,retVal,256,&cbData);
  std::cout << retVal << std::endl;
}
SQLFreeHandle(SQL_HANDLE_STMT, hdlStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hdlConn); 
SQLFreeHandle(SQL_HANDLE_ENV, hdlEnv);  //End the connection

 

Related Questions