Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ODBC Sample

Posted on 1998-11-12
5
Medium Priority
?
655 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 150 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

916 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