Results 1 to 6 of 6

Thread: how to connect c++ with sql

  1. #1
    Join Date
    Dec 2010
    Posts
    68

    how to connect c++ with sql

    I’m making a project in my last year of bsc-it, and I’m using c++ and sql language. Is it possible to make connection between this two language, if yes then how. I don’t know much about this connection. Please give me the step by step procedure of connection, I’ve never done connection in any language. give me the easiest procedure for connection. I need your help.

  2. #2
    Join Date
    Nov 2009
    Posts
    687

    Re: how to connect c++ with sql

    Well, I’m not sure about the c++ connection with sql but try this code:
    CDatabase cdb;
    CString sDBName = _T("DBQ=your database name");
    CString sUserDSN = _T("ODBC;DSN=your sql DSN name found in ODBC Data Source- User DSN tab");
    CString sSQLStmt = _T("your sql statement");

    cdb.OpenEx( sUserDSN + ";" + sDBName, CDatabase::noOdbcDialog );

    CRecordset rsTemp(&cdb);
    rsTemp.Open(CRecordset::dynaset, sSQLStmt);

    This is the ODBC way. Hope it work for you.

  3. #3
    Join Date
    Nov 2009
    Posts
    758

    Re: how to connect c++ with sql

    You need to write following code in your program for connection:
    BOOL MyApp::ConnectToDatabase(CString szLogonUser,
    CString szUserPass, CString szServer,
    int nTrustedConnection)
    {
    m_pConnDdmData = NULL;
    CString szddmdata = "";
    int nVersion = 0, nTimeout = 30;

    //OLE DB Provider for SQL Server 2000
    if(DATABASEPROVIDER == SQL2000PROVIDER)
    {
    nTimeout = theApp.GetProfileInt(
    "SERVER CONNECTION SETTINGS","CONNECTION TIMEOUT",15);
    if((nTimeout < 15) || (nTimeout > 999)) nTimeout = 15;

    // Standard user that uses SQL Server Authentication
    if(nTrustedConnection == 1)
    {
    strCnn = "Provider=SQLOLEDB;Data Source=";
    strCnn += szServer;
    strCnn += ";Initial Catalog=ddmdata;Trusted_Connection=yes;";
    }
    else
    {
    strCnn = "Provider=SQLOLEDB;Data Source=";
    strCnn += szServer;
    strCnn += ";Initial Catalog=ddmdata;";
    strCnn += "User ID="; //jim2000;Password=password";
    strCnn += szLogonUser;
    strCnn += ";Password=";
    strCnn += szUserPass;
    }
    }

    try
    {
    theApp.TESTHR(m_pConnDdmData.CreateInstance(__uuidof(Connection)));
    m_pConnDdmData->ConnectionTimeout = nTimeout; //60;
    HRESULT hr = m_pConnDdmData->Open((_bstr_t)strCnn,"","",
    adConnectUnspecified);
    _bstr_t version = m_pConnDdmData->Version;

    szddmdata = (LPCSTR)version;
    szddmdata.Remove('.');
    nVersion = atoi(szddmdata);
    if(nVersion < 26)
    {
    CString sztext;
    sztext.Format("The Microsoft Data Access Components version "
    "must be at least version 2.6! You must update your "
    "MDAC Components from version %s.",(LPCSTR)version);

    MessageBox(NULL,sztext,"Terminating the DDM Program",
    MB_OK);
    return FALSE;
    }
    }
    catch(_com_error &e)
    {
    if(m_pConnDdmData != NULL)
    m_pConnDdmData.Release();
    m_pConnDdmData = NULL;
    AfxMessageBox("DDMDATA Database connection error!\nDescription: "
    + (_bstr_t)e.Description());
    return FALSE;
    }
    //_bstr_t szConnectstring = m_pConnDdmData->GetConnectionString();
    return TRUE;
    }
    The above code will allow your program to connect to a database.

  4. #4
    Join Date
    Apr 2009
    Posts
    970

    Re: how to connect c++ with sql

    If you want to connect your c++ program to a sql database, than you have to create a odbc connection for accessing database.
    Here are procedures or rather steps used in the C++ coding for connecting to a database:
    • Include Header Files
    • Open a Connection to a Database
    • Choose an ODBC Driver
    • Query the Database
    • Creating an ODBC Statement Object
    • Executing a Query and Returning an ODBCResultSet Object
    • Extracting Data from an ODBCResultSet

  5. #5
    Join Date
    May 2008
    Posts
    681

    Re: how to connect c++ with sql

    The connection of c++ program to database are explain step by step. To make aconnection follow the steps:
    1). Include the Header Files

    # include statements at the beginning of your programs:
    #include <sql.h>
    #include<sqltypes.h>
    #include<sqlext.h>

    2). Open a Connection to a Database

    Set the environment handle:
    SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hdlEnv);
    Set ODBC Driver version:
    SQLSetEnvAttr(hdlEnv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC30);
    Set the connection handle:
    SQLAllocHandle(SQL_HANDLE_DBC, hdlEnv, &hdlConn);
    Connect to the database:
    SQLConnect(hdlConn, (SQLCHAR*)dsnName,SQL_NTS,(SQLCHAR*)userID,SQL_NTS, (SQLCHAR*)passwd, SQL_NTS);

    3). Choose Driver

    •DSN – Data Source Name
    •Open the GUI ODBC Administrator (ODBCConfig)
    •Choose the appropriate ODBC driver
    •Provide a meaningful name to the DSN
    •Specify the server and the host string (host string is required if the server is running on a different machine)

    4). Query the Database

    Querying the database involves the following steps:
    –Creating a Statement
    SQLAllocHandle(SQL_HANDLE_STMT, hdlDbc, &hdlStmt);
    It allocates the memory for the statement handle. The database handle obtained during connection phase is passed as the second argument.
    – Executing a Query
    SQLExecDirect(hdlStmt, stmt, SQL_NTS);
    It executes the query, which is passed in as SQLCHAR* in the second argument.

    5). Extract the Data out of the Executed Query

    SQLGetData(hStmt,colNum,type,retVal,buffLength,&cbData);
    It extracts data from table as void* data and places it in retVal
    colNum refers to the column number provided in the SELECT statement in SQLExecDirect()
    Type is one of the standard ODBC data types
    example: DT_STRING * for a string data type
    DT_DOUBLE * for a double data type
    buffLength is the estimated size of the expected data
    cbData is the actual size of the data

    6). Traverse Through the Results

    SQLFetch(hStmt);
    Fetches the next record
    hStmt is the statement handle obtained using SQLAllocHandle
    If a record is available, It returns SQL_SUCCEEDED

    7). Close the Statement and the Connection

    SQLFreeHandle(SQL_HANDLE_STMT, hdlStmt);
    It closes and de-allocates the memory reserved for the statement handle
    SQLFreeHandle(SQL_HANDLE_DBC, hdlConn);
    It disconnects and de-allocates the memory reserved for the connection handle
    SQLFreeHandle(SQL_HANDLE_ENV, hdlEnv);
    It de-allocates the memory occupied by the environment handle


    the above code will connect your c++ program with a sql database.

  6. #6
    Join Date
    Nov 2009
    Posts
    824

    Re: how to connect c++ with sql

    For creating connection between your frontend and backend which is c++ and sql you need to write the following code:
    //Header files:
    02
    03 #include <sql.h>
    04 #include<sqltypes.h>
    05 #include<sqlext.h>
    06 ...
    07 ....
    08
    09 //Declaration:
    10
    11 SQLHandle hdlEnv, hdlConn, hdlStmt, hdlDbc
    12 char* stmt = "SELECT * from NutHead" NutHead is the Table name; //SQL statement
    13
    14 //for example
    15 name of your program or what ever…..char *dsnName = “COLLECTOR”
    16 char* userID = "eXceed";
    17 char* passwd = "hole";
    18 char* retVal[256];
    19 unsigned int cbData;
    20
    21 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hdlEnv);
    22 SQLSetEnvAttr(hdlEnv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC30);
    23 SQLAllocHandle(SQL_HANDLE_DBC, hdlEnv, &hdlConn);
    24 SQLConnect(hdlConn, (SQLCHAR*)dsnName,SQL_NTS,(SQLCHAR*)userID,SQL_NTS, (SQLCHAR*)passwd, SQL_NTS);
    25 SQLAllocHandle(SQL_HANDLE_STMT, hdlDbc, &hdlStmt);
    26 SQLExecDirect(hdlStmt, (SQLCHAR*)stmt, SQL_NTS);
    27
    28
    29 //Initialize the database connection
    30
    31 while(SQLFetch(hdlStmt) == SQL_SUCCEEDED)
    32 {
    33 SQLGetData(hdlStmt,0,DT_STRING,retVal,256,&cbData);
    34 std::cout << retVal << std::endl;
    35 }
    36 SQLFreeHandle(SQL_HANDLE_STMT, hdlStmt);
    37 SQLFreeHandle(SQL_HANDLE_DBC, hdlConn);
    38 SQLFreeHandle(SQL_HANDLE_ENV, hdlEnv); //End the connection

    The above code is a example of connecting c++ with database.

Similar Threads

  1. Replies: 9
    Last Post: 01-10-2011, 12:14 AM
  2. Replies: 4
    Last Post: 16-06-2010, 12:54 PM
  3. Replies: 2
    Last Post: 24-03-2009, 07:19 PM
  4. Replies: 1
    Last Post: 08-08-2008, 01:55 PM
  5. Can Connect to Router but unable to connect to internet
    By Q Jones in forum Windows Vista Network
    Replies: 1
    Last Post: 14-09-2007, 03:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,140,585.78586 seconds with 17 queries