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
Solved

Use ODBC connect to MS SQL in C ?

Posted on 2002-04-23
3
218 Views
Last Modified: 2010-04-15
How do I use ODBC connection to MS SQL for inserting some data in C ?

Any sample ?
0
Comment
Question by:tanc02
3 Comments
 
LVL 6

Expert Comment

by:Triskelion
ID: 6965628
My first question is
   Can you use C++ (or Visual C++)?
0
 
LVL 1

Author Comment

by:tanc02
ID: 6966552
ok, I can use C++.
0
 
LVL 10

Accepted Solution

by:
makerp earned 100 total points
ID: 6968143
this is a simple example ive knocked up for you, create a simple access or sql server database and create

a system dsn for it in control panel. the open function uses global varaibles etc so you will probably

want to chage this.

/* for db stuff */
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <stdio.h>

HENV henv;
HDBC hdbc;
int dbopen=0;

/* opens a connection using global variables , bad i know */
#define PM_DSN "your_system_dsn"
#define PM_DSN_USER "your_db_username_if_any"
#define PM_DSN_PW "your_password_if_any"

int DBopen(void)
{
   int res=0;
   RETCODE retcode;
   /*allocate the environment handle*/
   if(SQLAllocEnv(&henv)==SQL_SUCCESS)
   {
        /*allocate the connection handle*/
        if(SQLAllocConnect(henv, &hdbc)==SQL_SUCCESS)
        {
             /* Set login timeout to 5 seconds. */
             SQLSetConnectOption(hdbc, SQL_LOGIN_TIMEOUT, 5);
             SQLSetConnectOption(hdbc, SQL_CURSOR_TYPE, SQL_CURSOR_STATIC);
             /* Connect to data source */
             retcode = SQLConnect(hdbc, PM_DSN, SQL_NTS, PM_DSN_USER, SQL_NTS, PM_DSN_PW, SQL_NTS);


             if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
             {
                       res=1;
             }
        }
        else
        {
             SQLFreeConnect(hdbc);
        }
   }
   else
   {
        SQLFreeEnv(henv);
   }
   dbopen=res;
   return res;
}

void DBclose(void)
{
   if(dbopen)
   {
        SQLDisconnect(hdbc);
        SQLFreeConnect(hdbc);
        SQLFreeEnv(henv);
        if(DATABASE_NAME)
        {
             free(DATABASE_NAME);
        }
   }
   dbopen=0;
}
int DBexecute(char *sql,HSTMT *hstmt)
{
   int res=0;
   RETCODE retcode;

   if(SQLAllocStmt(hdbc, hstmt)== SQL_SUCCESS)
   {
        retcode=SQLPrepare(*hstmt,sql,strlen(sql));
        if(retcode==SQL_SUCCESS)
        {
             retcode=SQLExecute(*hstmt);
             if(retcode==SQL_SUCCESS)
             {
                  res=1;
             }
        }
   }
   return res;
}

void DBcloseCursor(HSTMT hstmt)
{
   SQLFreeStmt(hstmt, SQL_DROP);
}

void main()
{
   char sql[255];
   HSTMT fstmt;
   long lens;
   RETCODE retcode;
   char name[100];

   DBopen();

   sprintf(sql,"SELECT name FROM users");
   if(DBexecute(sql,&fstmt))
   {      
        SQLBindCol(fstmt,1,SQL_C_CHAR, name,sizeof(name),&lens);
       
        retcode = SQLFetch(fstmt);    
        while(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
        {
             printf("%s\n",name);
             retcode = SQLFetch(fstmt);
        }
        DBcloseCursor(fstmt);
   }
   DBclose();
}
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

856 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