#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <Sqltypes.h>
#include<string.h>
#define SNO_LEN 30
#define NAME_LEN 50
#define DEPART_LEN 100
#define SSEX_LEN 5
int main()
{
/* Step 1 定义句柄和变量 */
//以king开头的表示的是连接KingbaseES的变量
//以server开头的表示的是连接SQLSERVER的变量
SQLHENV kinghenv,serverhenv; //环境句柄
SQLHDBC kinghdbc,serverhdbc; //连接句柄
SQLHSTMT kinghstmt,serverhstmt; //语句句柄
SQLRETURN ret;
SQLCHAR sName[NAME_LEN],sDepart[DEPART_LEN],sSex[SSEX_LEN],sSno[SNO_LEN];
SQLINTEGER sAge;
SQLINTEGER cbAge=0,cbSno=SQL_NTS,cbSex=SQL_NTS,cbName=SQL_NTS,cbDepart=SQL_NTS;
SQLCHAR A[9]=”oracledb”,B[6]=”scott”,C[6]=”tiger”;
ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&kinghenv);
ret=SQLSetEnvAttr(kinghenv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
ret=SQLAllocHandle(SQL_HANDLE_DBC,kinghenv,&kinghdbc);
ret=SQLConnect(kinghdbc,A,SQL_NTS,B,SQL_NTS,C,SQL_NTS);
if(!SQL_SUCCEEDED(ret))//连接失败时返回错误值
{printf(“连接失败!”);return -1;}
else printf(“连接成功!”);
ret=SQLAllocHandle(SQL_HANDLE_STMT,kinghdbc,&kinghstmt);
ret=SQLSetStmtAttr(kinghstmt,SQL_ATTR_ROW_BIND_TYPE,(SQLPOINTER)SQL_BIND_BY_COLUMN,SQL_IS_INTEGER );
//*执行SQL语句
SQLCHAR t[23]=”SELECT * FROM STUDENTs”;
ret=SQLExecDirect(kinghstmt,t,SQL_NTS);
if (ret==SQL_SUCCESS || ret==SQL_SUCCESS_WITH_INFO)
{
printf(“输送sql语句成功”);
ret=SQLBindCol(kinghstmt,1,SQL_C_CHAR,sSno,SNO_LEN,&cbSno);
ret=SQLBindCol(kinghstmt,2,SQL_C_CHAR,sName,NAME_LEN,&cbName);
//ret=SQLBindCol(kinghstmt, 3, SQL_C_CHAR, sSex, SSEX_LEN, &cbSex);
//ret=SQLBindCol(kinghstmt, 4, SQL_C_LONG, &sAge, 0, &cbAge);
//ret=SQLBindCol(kinghstmt, 5, SQL_C_CHAR, sDepart, DEPART_LEN, &cbDepart);
}
while ( (ret=SQLFetch(kinghstmt))!=SQL_NO_DATA_FOUND)
{ printf(“\n”);
if(ret==SQL_ERROR) printf(“Fetch error\n”);
printf(“%30s%30s\n”,sSno,sName);
}
// Step 7 中止处理
SQLFreeHandle(SQL_HANDLE_STMT,kinghstmt);
SQLDisconnect(kinghdbc);
SQLFreeHandle(SQL_HANDLE_DBC,kinghdbc);
SQLFreeHandle(SQL_HANDLE_ENV,kinghenv);
return 0;
}
>> 本文固定链接: http://www.vcgood.com/archives/2023
>> 转载请注明: jinfan1009 2007年12月16日 于 C语言帝国 发表