Solved

ODBC Sample

Posted on 1998-11-12
5
605 Views
Last Modified: 2008-02-01
I've been using MS SQL native calls for my databse functionality. I'm finally being forced to use ODBC. Can someone provide me a basic sample that shows the equivalent to the following functions in straight C?

dbinit
dbopen
dbfcmd
dbsqlexec
dbnextrow
dbclose
dbexit
0
Comment
Question by:flfmdll
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:pagladasu
ID: 1025974
I can provide you one - just tell me what does dbfcmd does. Also does dbinit establishes the connection with the database.

0
 

Author Comment

by:flfmdll
ID: 1025975
The dbfcmd formats a SQL command into a particular DBPROCESS. I don't have it in front of me now. So my syntax may not be exact. But an example might be:

dbfcmd(dbproc1, "select * from table");
0
 
LVL 4

Accepted Solution

by:
pagladasu earned 50 total points
ID: 1025976
I am using VC++ compiler. Hope this helps. Change the connect string to suit a MSSQL database. These are code snippets - will help you to get an idea.

#include <sql.h>
#include <sqlext.h>

#define CONNECTSTR "DBQ=D:\\Espi\\MyDb.mdb;DRIVER={Microsoft Access Driver (*.mdb)}"
#define CONNECTLEN (sizeof(CONNECTSTR)-1)
#define SQLTRY(x,y) \
      {\
            returncode=y;\
            if(returncode != SQL_SUCCESS)\
                  AfxMessageBox(x);\
      }

      SQLHENV hEnv;
      SQLHDBC hDBC;
      SQLHSTMT hStmt;
      SQLCHAR szConnectStr[255];
      SQLCHAR szStmt[255];
      SQLCHAR szMemberName[255];
      SQLCHAR szDesignation[255];
      SWORD cbConnectStr;
      RETCODE returncode;
      SDWORD sdwMNLen;
      SDWORD sdwDGLen;

You can look into these functions
SQLTRY("1",::SQLAllocEnv(&hEnv))
SQLTRY("2",::SQLAllocConnect(hEnv,&hDBC))
SQLTRY("3",::SQLDriverConnect(hDBC,NULL,(unsigned char *)CONNECTSTR,CONNECTLEN,szConnectStr,sizeof(szConnectStr),&cbConnectStr,SQL_DRIVER_NOPROMPT))
      ::sprintf((char *)szStmt,"SELECT MemberName,Designation FROM Members");
      SQLTRY("A",::SQLAllocStmt(hDBC,&hStmt))
      SQLTRY("4",::SQLPrepare(hStmt,szStmt,strlen((char *)szStmt)))
      SQLTRY("5",::SQLBindCol(hStmt,1,SQL_C_CHAR,(PTR)szMemberName,sizeof(szMemberName),&sdwMNLen))
      SQLTRY("6",::SQLBindCol(hStmt,2,SQL_C_CHAR,(PTR)szDesignation,sizeof(szDesignation),&sdwDGLen))
      SQLTRY("7",::SQLExecute(hStmt))
      SQLTRY("8",::SQLFetch(hStmt))
      m_1=szMemberName;//m_1 and m_2 are CString variables
      m_2=szDesignation;
      UpdateData(FALSE);
      ::SQLFreeStmt(hStmt,SQL_CLOSE);
      ::SQLDisconnect(hDBC);
      ::SQLFreeConnect(hDBC);
      ::SQLFreeEnv(hEnv);


0
 

Author Comment

by:flfmdll
ID: 1025977
Some aspects of what you have listed are C++. Will this also work in straight C?
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1025978
ODBC API's are supposed to work in C also. It is not a class library.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question