How can i connect Sql server 2000 through c++

Recommended Answers

All 5 Replies

There is a layer for accessing databases from any native program. This is called ODBC.
ODBC is a library that allows accessing to any database, using a specific "Driver" (probably shipped with SQL Server).
The ODBC API for C is a bit complicated. There is a few c++ bindings that are well.
libodbc++ (an opensource project) should fit .http://libodbcxx.sourceforge.net/

There is a layer for accessing databases from any native program. This is called ODBC.
ODBC is a library that allows accessing to any database, using a specific "Driver" (probably shipped with SQL Server).
The ODBC API for C is a bit complicated. There is a few c++ bindings that are well.
libodbc++ (an opensource project) should fit .http://libodbcxx.sourceforge.net/

Now plz told me what should I do to connect SQl server 2000 through C?

Now plz told me what should I do to connect SQl server 2000 through C?

1- Install or verify that an ODBC driver is installed for your database.

2 - In your C source code
a- connect to the data source (using the driver, or a DSN as the example above)
b- execute the SQL queries using the SQLExecDirect() function.
c- close the connection using SQLDisconnect(), and then SQLFreeHandle().

Anyway, I really think you should use a c++ library.

Sample code (taken from the site below)

#include <stdio.h>
#include <sql.h>
#include <sqlext.h>

/*
 * see Retrieving ODBC Diagnostics
 * for a definition of extract_error().
 */
static void extract_error(
    char *fn,
    SQLHANDLE handle,
    SQLSMALLINT type);

main() {
  SQLHENV env;
  SQLHDBC dbc;
  SQLHSTMT stmt;
  SQLRETURN ret; /* ODBC API return status */
  SQLCHAR outstr[1024];
  SQLSMALLINT outstrlen;

  /* Allocate an environment handle */
  SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  /* We want ODBC 3 support */
  SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
  /* Allocate a connection handle */
  SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
  /* Connect to the DSN mydsn */
  ret = SQLDriverConnect(dbc, (void *)1, "DSN=fred;", SQL_NTS,
			 outstr, sizeof(outstr), &outstrlen,
			 SQL_DRIVER_COMPLETE);
  if (SQL_SUCCEEDED(ret)) {
    printf("Connected\n");
    printf("Returned connection string was:\n\t%s\n", outstr);
    if (ret == SQL_SUCCESS_WITH_INFO) {
      printf("Driver reported the following diagnostics\n");
      extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
    }

    /* SQL query code go here */

    SQLDisconnect(dbc);		/* disconnect from driver */
  } else {
    fprintf(stderr, "Failed to connect\n");
    extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
  }
  /* free up allocated handles */
  SQLFreeHandle(SQL_HANDLE_DBC, dbc);
  SQLFreeHandle(SQL_HANDLE_ENV, env);
}

Now more infos about the ODBC Api can be found here :

http://www.easysoft.com/developer/languages/c/odbc_tutorial.html#connect_simple

i dont know how to connect c++ with Oracle,plz guide with the basics for this...pls help..
Priya

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.