int ConnectDB()
{
int ret;
EXEC SQL BEGIN DECLARE SECTION; //接受数据库数据的变量必须在这个段代码之间定义
char DBUserName[64];
char DBPasswd[64];
char DBname[64];
EXEC SQL END DECLARE SECTION;
memset(DBUserName, 0, sizeof(DBUserName));
memset(DBPasswd, 0, sizeof(DBPasswd));
memset(DBname, 0, sizeof(DBname));
GetDBUSER(DBUserName); //用getenv(), 获取配置文件里面的信息
GetDBPASSWD(DBPasswd);
GetDBNAME(DBname);
if(strlen(DBname) == 0) //本地数据库
{
EXEC SQL CONNECT :DBUserName IDENTIFIED BY :DBPasswd;
}
else //远程数据库
{
EXEC SQL CONNECT :DBUserName IDENTIFIED BY :DBPasswd USING :DBname;
}
if(sqlca.sqlcode) //sqlca.sqlcode == 0,成功 sqlca.sqlcode == -1 失败
{
if (sqlca.sqlcode == 28002) //the password will expire within %s days
{
printf("in ConnectDB:: Waring database sqlca.sqlcode = [%d] [the password will expire within %%s days]\n", sqlca.sqlcode);
return(0);
}
else
{
ret = GetDBErrCode();
printf("in ConnectDB:: DBname = [%s] DBUserName = [%s]\n",DBname,DBUserName);
printf("in ConnectDB:: database sqlca.sqlcode = [%d]\n",sqlca.sqlcode);
return(ret);
}
}
printf("in ConnectDB:: connect DB OK!\n");
return(0);
}
阅读(1560) | 评论(0) | 转发(0) |