Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic ODBC help!

Status
Not open for further replies.

mbames

Programmer
Jun 5, 2003
29
GB
I am new to C++ (and C in general). I need to write a small console app, that will eventually end up life as a service (but I am not worried about that at the moment).

I need to be able to connect to an Oracle database in order that I can:
(i) store a new record
(ii) retrieve a record

I have had a quick look through the help, and I seem to be getting more and more confused. Does anyone have any example code - at least showing how to open a connection to a DB, store some records....

Cheers,
Matt
 
Hi Matt, for examples and samples in a Windows environment you can try msdn.microsoft.com. Also google.com will find other resources.

I highly recommend using books targeted at beginners to get started. They are much more focused to beginners than the examples you will find online. Also the books tend to flow from start to finish. For example to do ODBC development to Oracle you will need an array of fundamental C/C++ programming knowledge and the books will take you through that first.


-pete
 
hi, i need tutorial about connecting visual c++ to SqlServer and how to query data from the database tables , please help me
 
this is a short sample of using ODBC
all rc = something means you must the next verify this rc. I did not put error verification code
Code:
#include<iostream>
#include<string>
#include<sql.h>
#include<sqlext.h>
using namespace std;
int main()
{
    HENV hEnv;
    HDBC hDbc;
    RETCODE rc;
    int iOut;
    char strOut[256];
    char szDSN[256] = &quot;driver={Microsoft Access Driver (*.mdb)};dbq=[c:\\db1.mdb];&quot;;
    //dsn samples:
    //&quot;driver={Microsoft Access Driver (*.mdb)};dbq=[f:\\db1.mdb];&quot;
    //&quot;driver={SQL Server};pwd={password there};Server={server name};Database={dbname there}&quot;
    //driver names for different databases:
    //{SQL Server}
    //{Microsoft ODBC for Oracle}
    //{Oracle in oracle9}
    //{Microsoft Access Driver (*.mdb)}
    //{MySQL ODBC 3.51 Driver}

    char* szSql = &quot;select * from table1&quot;;
    rc = SQLAllocEnv(&hEnv);
    rc = SQLAllocConnect(hEnv, &hDbc);

    rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN, 
        SQL_NTS, (unsigned char*)strOut, 
        255, (SQLSMALLINT*)&iOut, SQL_DRIVER_NOPROMPT);
    {
        int ival;
        char chval[128];
        int ret1;
        int ret2;
        HSTMT hStmt;
        rc = SQLAllocStmt(hDbc,&hStmt);
        rc = SQLPrepare(hStmt,(unsigned char*)szSql, SQL_NTS);//1
        //rc = SQLBindCol(hStmt, tab_column, tr_type, tr_value, tr_len, len_or_ind);
        rc = SQLBindCol(hStmt, 1, SQL_C_ULONG, &ival, 4, (SQLINTEGER*)& ret1);
		rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval, 128, (SQLINTEGER*)&ret2);
        rc = SQLExecute(hStmt); //2
        //if you have queries like drop/create/many update... 
        //instead of //1 //2 and //3 you could use
        //rc=SQLExecDirectA(hStmt,(unsigned char*)szSql,SQL_NTS);
        cout<< &quot;>table:&quot;<< endl;
        while(1) //3
        {
            rc = SQLFetch(hStmt);
            if(rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)break;
            cout<< &quot;{&quot;<< ival<<&quot;}{&quot;<< chval<< &quot;}&quot;<< endl;
        }
        rc=SQLFreeStmt(hStmt, SQL_DROP);
    }
    rc = SQLDisconnect(hDbc);
    rc = SQLFreeEnv(hEnv);
    return 0;
}

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top