---
title: "ODBC访问Oracle实例（附代码）"
author: "alienchang"
date: "2011-12-01T16:23:56Z"
tags: []
categories: ["数据库"]
slug: "odbc-oracle"
draft: false
lastmod: "2011-12-01T16:23:56Z"
id: 1287966
url: "https://alienchang.iteye.com/blog/1287966"
---

ODBC访问Oracle基本流程和代码：

```c

#include &lt;Windows.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &quot;sql.h&quot;
#include &quot;sqlext.h&quot;

void check_return( RETCODE rc, HENV henv, HDBC hdbc, HSTMT hstmt )
{
	UCHAR state_str [sql_max_message_length];
	SDWORD native_error;
	UCHAR  error_msg [sql_max_message_length];
	SWORD  error_msg_avail = SQL_MAX_MESSAGE_LENGTH - 1;
	SWORD  error_msg_len;
	RETCODE local_rc;
	if (rc != SQL_ERROR &amp;&amp; rc != SQL_SUCCESS_WITH_INFO )
	{
		return;
	}
	local_rc = SQLError (henv,hdbc,hstmt,state_str,&amp;native_error,error_msg,error_msg_avail,&amp;error_msg_len);

	if (local_rc != SQL_SUCCESS &amp;&amp; rc !=SQL_SUCCESS_WITH_INFO)
	{
		fprintf(stderr,&quot;Uninterpretable error; exiting \n&quot;);
		exit (EXIT_FAILURE);
	}
	if (rc == SQL_SUCCESS_WITH_INFO)
	{
		fprintf(stderr,&quot;Fatal Error %s: %s\n&quot;, state_str,error_msg);
		return;
	}
	fprintf(stderr,&quot;Fatal Error %s: %s\n&quot;,state_str,error_msg);
	exit (EXIT_FAILURE);
}

/*----------------------------------------------------------------------*/

int main (void)
{
	HENV henv = SQL_NULL_HENV;
	HDBC hdbc = SQL_NULL_HDBC;
	HSTMT hstmt = SQL_NULL_HSTMT;
	RETCODE rc;
	char buf[257];
	short buflen;

	printf (&quot;Initialize the environment structure.\n&quot;);
	SQLAllocEnv (&amp;henv);

	printf (&quot;Initialize the connection structure.\n&quot;);
	SQLAllocConnect (henv,&amp;hdbc);
	printf(&quot;Load the ODBC driver.\n&quot;);
	rc = SQLConnect(hdbc,(SQLCHAR*)&quot;Oracle&quot;, SQL_NTS, //注明：DBDEMO是一个系统数据源，如果这样子写就不行L“DNS=DBDEMO”
		(SQLCHAR*)&quot;alien&quot;, SQL_NTS, (SQLCHAR*)&quot;alienchang&quot;, SQL_NTS);
	//rc = SQLDriverConnect (hdbc,0,(SQLCHAR*)&quot;DSN=Oracle;UID=alien;PASSWD=alienchang&quot;,SQL_NTS,(UCHAR*) buf,sizeof (buf),&amp;buflen,SQL_DRIVER_COMPLETE);
	check_return (rc,henv,hdbc,hstmt);

	printf (&quot;Initialize the statement structure.\n&quot;);
	SQLAllocStmt (hdbc,&amp;hstmt);

	SQLUINTEGER ID;
	SQLCHAR Name[20];
	SQLINTEGER szID = 0, szName=0;
	

	/************************************************************************/
	/* prepare, bind and execute                                            */
	/************************************************************************/
	SQLPrepare(hstmt, (SQLCHAR*)&quot;begin say( ?, ?); end;&quot;, SQL_NTS);
	SQLBindParameter(hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, 10, 0, 
		&amp;Name, 0, &amp;szName);
	SQLBindParameter(hstmt, 2, SQL_PARAM_OUTPUT, SQL_INTEGER, SQL_INTEGER, 64, 0,
		&amp;ID, 0, 0);
	rc= SQLExecute(hstmt);
	printf(&quot;%03d %d %s %d\n&quot;, ID, szID, Name, szName);


	/************************************************************************/
	/* SqlExecDirect Select                                                 */
	/************************************************************************/
	SQLSMALLINT cols;
	SQLINTEGER   sID;
	SQLCHAR   szAccount[50], szRegdate[50];
	SQLINTEGER cbID, cbAccount, cbRegdate; //save the length of parameter
	
	rc = SQLExecDirect (hstmt,(SQLCHAR*)&quot;Select id, account, to_char(regdate,'yyyy-mm-dd hh24:mi:ss') from hx_account&quot;, SQL_NTS);
	check_return (rc, henv, hdbc, hstmt);

	while(SQL_NO_DATA != SQLFetch(hstmt)){
		SQLGetData(hstmt, 1, SQL_C_ULONG, &amp;sID, 0, &amp;cbID);
		//此处并没有指明BufferLength参数的值，是因为数据类型是定长的LONG型
		SQLGetData(hstmt, 2, SQL_C_CHAR, szAccount, 50, &amp;cbAccount);
		SQLGetData(hstmt, 3, SQL_C_CHAR, szRegdate, 50,&amp;cbRegdate);
		printf(&quot;%8d %s %s\n&quot;, sID,  szAccount, szRegdate);
	}

	SQLCHAR colName[21];
	
	SQLUINTEGER szCol;
	SQLSMALLINT colNums, szCName, dType, Digits, isNULL;
	
	rc = SQLNumResultCols(hstmt, &amp;colNums);
	rc = SQLDescribeCol(hstmt, 1, colName, 20, &amp;szCName, &amp;dType, &amp;szCol, &amp;Digits, &amp;isNULL);
	
	printf(&quot;列数：%d \n第一列：name：%s %d\t type: %d\t size: %d\t isnull:%d\n&quot;, colNums, colName, szCName, dType, szCol, isNULL);

	/************************************************************************/
	/* Create Table and Insert                                              */
	/************************************************************************/
	printf (&quot;Creat table table \&quot;foo\&quot;.\n&quot;);
	rc = SQLExecDirect (hstmt,(SQLCHAR*)&quot;CREATE TABLE foo (bar INTEGER)&quot;, SQL_NTS);
	check_return (rc, henv, hdbc, hstmt);

	printf (&quot;Insert values into table \&quot;foo\&quot;.\n&quot;);
	rc = SQLExecDirect (hstmt,(SQLCHAR*)&quot;INSERT INTO foo(bar) VALUES (1)&quot;, SQL_NTS);
	check_return (rc, henv, hdbc, hstmt);
	rc = SQLExecDirect (hstmt,(SQLCHAR*)&quot;INSERT INTO foo(bar) VALUES (2)&quot;, SQL_NTS);
	check_return (rc, henv, hdbc, hstmt);
	rc = SQLExecDirect (hstmt,(SQLCHAR*)&quot;INSERT INTO foo(bar) VALUES (3)&quot;, SQL_NTS);
	check_return (rc, henv, hdbc, hstmt);

	SQLINTEGER rowNums;
	rc = SQLRowCount(hstmt, &amp;rowNums);
	printf(&quot;影响行数：%d\n&quot;, rowNums);

	printf (&quot;Drop table \&quot;foo\&quot;.\n&quot;);
	rc = SQLExecDirect (hstmt, (SQLCHAR*)&quot;DROP TABLE foo&quot;, SQL_NTS);
	check_return (rc,henv,hdbc,hstmt);

	/*  We're done:free resources and exit*/
	printf (&quot;Free the statement handle.\n&quot;);
	SQLFreeStmt (hstmt,SQL_DROP);
	printf (&quot;Disconnect from the data source.\n&quot;);
	SQLDisconnect (hdbc);
	printf (&quot;Free the connection structure.\n&quot;);
	SQLFreeConnect (hdbc);
	printf (&quot;Free the environment structure.\n&quot;);
	SQLFreeEnv (henv);
	printf (&quot;Goodbye!\n&quot;);
	exit (EXIT_SUCCESS);
}

```