Solved

ODBC Sample

Posted on 1998-11-12
5
581 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now