OCILIB在VS2008中的使用
2014年4月29日17:21:46
1、下载ocilib-3.12.1-windows.zip。解压到D盘。
2、创建一个空的控制台程序。
3、选择“工具”--“选项”--“项目和解决方案”--“VC++目录”--“包含文件”,添加D:\ocilib-3.12.1-windows\ocilib\include。
4、选择“工具”--“选项”--“项目和解决方案”--“VC++目录”--“库文件”,添加D:\ocilib-3.12.1-windows\ocilib\lib32。
5、建立一个cpp文件。
6、源文件内容:
//test.cpp文件
#pragma comment(lib,"ociliba.lib")
#define OCI_CHARSET_ANSI
#include <ocilib.h>
int main(void)
{
OCI_Connection *cn;
if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT|OCI_ENV_CONTEXT))
return EXIT_FAILURE;
cn = OCI_ConnectionCreate("orcl", "etc_ctrlr", "itsmoe", OCI_SESSION_DEFAULT);
OCI_Statement * st;
st = OCI_StatementCreate(cn);
int nRet =0;
OCI_ExecuteStmt(st, "select SN, MVLICENSE,CARDID,ENTRYTIME from LWW_TXF_TEST");
OCI_Resultset *rs;
rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
{
printf("%i - %s - %s\n", OCI_GetInt(rs, 1), OCI_GetString(rs, 2),OCI_GetString2(rs,"CARDID"));
OCI_Date * d1 = NULL;
d1 = OCI_GetDate2(rs,"ENTRYTIME");
char str[20] = {0};
OCI_DateToText(d1, "YYYY/MM/DD HH24:MI:SS",sizeof(str), str);
printf("%s\n",str);
}
//事务
OCI_Prepare(st, "update LWW_TXF_TEST set MVLICENSE = 'abc123陆lww' where SN = 775 ");
nRet = OCI_Execute(st);
//日期类型要注意
nRet = OCI_ExecuteStmt(st, "update LWW_TXF_TEST set ENTRYTIME = to_date('1999-09-12', 'YYYY-MM-DD') where SN = 775 ");
OCI_Error * pError = OCI_GetLastError();
printf("%s\n", OCI_ErrorGetString(pError));
nRet = OCI_ExecuteStmt(st, "update LWW_TXF_TEST set CARDID = 'ABC456陆lww' where SN = 775 ");
OCI_Commit(cn);
/*if(OCI_IsConnected(cn))
{
OCI_Rollback(cn);
}*/
//OCI_Rollback(cn);
OCI_Break(cn);
OCI_Cleanup();
return 0;
}