Solved

ODBC Sample

Posted on 1998-11-12
5
640 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
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…

636 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