Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ODBC Sample

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 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