/******************************************************************************
* Pro* C/C++ SELECT INSERT 演示程序
*
* 向joe_t2表插入测试数据,表生成准备脚本见当前目录ctScript.sql
*
* 黑龙江省哈尔滨市平房区
* 作者:高宏伟(DukeJoe)
* 2005-3-21 16:10
******************************************************************************/
#include
#include
#include
#include
#include
#define UNAME_LEN20
#define PWD_LEN40
#defineBUFFER_LENGTH1024
void sql_error(char *msg) ;
void TRACE(char * pMsg) ;
int QueryCount() ;
int InsertData(int iMaxF1, int iMaxF2) ;
int main()
{
/* Declare variables.No declare section is needed if MODE=ORACLE.*/
VARCHAR username[UNAME_LEN];
varchar password[PWD_LEN];
time_t tmBegin, tmEnd ;
char * pc ;
char szBuffer[BUFFER_LENGTH] ;
const int iMaxF1 = 10 ;
const int iMaxF2 = 5 ;
time(&tmBegin) ;
/* 用户名口令 */
strncpy((char *) username.arr, "billing", UNAME_LEN);
username.len = strlen((char *) username.arr);
strncpy((char *) password.arr, "billing", PWD_LEN);
password.len = strlen((char *) password.arr);
/* 注册sql_error为错误处理函数 */
EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--");
/* 连接数据库 */
EXEC SQL CONNECT :username IDENTIFIED BY :password;
sprintf(szBuffer, "Connected to ORACLE as user: %s", username.arr);
/**************************************************************************
* 为保证程序的连贯性,不必在SQL Plus中经常truncate table,
* 在程序中加入查询和DELETE的功能
*************************************************************************/
/* 查询当前的记录数 */
QueryCount() ;
InsertData(iMaxF1, iMaxF2) ;
EXEC SQL COMMIT WORK RELEASE;
time(&tmEnd) ;
pc = ctime(&tmBegin) ;
printf("开始时间:%s", pc) ;
pc = ctime(&tmEnd) ;
printf("结束时间:%s", pc) ;
return 0 ;
}
/******************************************************************************
* 系统跟踪函数(仿Microsoft Visual C++ TRACE 宏)
* v1.0
*****************************************************************************/
void TRACE(char * pMsg)
{
time_t ttSimple ;
char szBuffer[64] ;
struct tm * ptmShow ;
time(&ttSimple) ;
ptmShow = localtime(&ttSimple) ;
strftime(szBuffer, 64, "%Y-%m-%d %H:%M:%S", ptmShow) ;
printf("Logging(%s)-->%s", szBuffer, pMsg) ;
}
int QueryCount()
{
int iCount = 0 ;
EXEC SQL SELECT count(*) INTO :iCount
FROM joe_t2 ;
if ( iCount > 0 )
{
char szBuffer[16] ;
printf("在表joe_t2中发现%d条记录,如果不删除,可能因为主键约束的原因无法插入新值"
"。是否删除(y/n)?[Y]\t", iCount) ;
gets(szBuffer) ;
switch( *szBuffer )
{
case 'n':
case 'N':
break ;
default :
EXEC SQL DELETE FROM joe_t2 ;
}
}
return 0 ;
}
int InsertData(int iMaxF1, int iMaxF2)
{
int iF1, iF2 ;
char szBuffer[BUFFER_LENGTH] ;
/* 插入数据 */
for ( iF1 = 0 ; iF1 < iMaxF1 ; iF1++ )
{
for ( iF2 = 0 ; iF2 < iMaxF2 ; iF2++ )
{
sprintf(szBuffer, "第 %3d 条记录", iF1*iMaxF2+iF2) ;
TRACE(szBuffer) ;
EXEC SQL INSERT INTO joe_t2(f1, f2, f3, f4)
VALUES ( :iF1, :iF2, :szBuffer, sysdate) ;
}
}
return 0 ;
}
/******************************************************************************
* 错误处理函数
*****************************************************************************/
void sql_error(char *msg)
{
char err_msg[128];
int buf_len, msg_len;
EXEC SQL WHENEVER SQLERROR CONTINUE;
printf("%s", msg);
buf_len = sizeof (err_msg);
sqlglm(err_msg, &buf_len, &msg_len);
printf("%.*s", msg_len, err_msg);
EXEC SQL ROLLBACK RELEASE;
exit(1);
}
/* 这是一个前几天写的Pro*C/C++程序,看懂它你就会用C++连接ORACLE了 */
--------------------------------------------------------------------------------
// BeingConnectionStringCpp
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
#include
#include
#include
// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void ConnectionStringX();
_bstr_t GetState(int intState);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);
///////////////////////////////////////////////////////////
// //
// Main Function //
// //
///////////////////////////////////////////////////////////
void main()
{
if(FAILED(::CoInitialize(NULL)))
return;
ConnectionStringX();
//Wait here for user to see the output..
printf("Press any key to continue...");
getch();
::CoUninitialize();
}
///////////////////////////////////////////////////////////
// //
// ConnectionStringX Function //
// //
///////////////////////////////////////////////////////////
void ConnectionStringX()
{
// Define Connection object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace
_ConnectionPtr pConnection1 = NULL;
_ConnectionPtr pConnection2 = NULL;
_ConnectionPtr pConnection3 = NULL;
_ConnectionPtr pConnection4 = NULL;
//Define Other Variables
HRESULT hr = S_OK;
try
{
// Open a connection using OLE DB syntax.
TESTHR(pConnection1.CreateInstance(__uuidof(Connection)));
pConnection1->ConnectionString =
"Provider='sqloledb';Data Source='MySqlServer';"
"Initial Catalog='Pubs';Integrated Security='SSPI';";
pConnection1->ConnectionTimeout = 30;
pConnection1->Open("","","",adConnectUnspecified);
printf("cnn1 state: %s",
(LPCTSTR)GetState(pConnection1->State));
// Open a connection using a DSN and ODBC tags.
// It is assumed that you have create DSN 'Pubs' with a user name as
// 'MyUserId' and password as 'MyPassword'.
TESTHR(pConnection2.CreateInstance(__uuidof(Connection)));
pConnection2->ConnectionString = "DSN=Pubs;UID=MyUserId;PWD=MyPassword;";
pConnection2->Open("","","",adConnectUnspecified);
printf("cnn2 state: %s",
(LPCTSTR)GetState(pConnection2->State));
// Open a connection using a DSN and OLE DB tags.
TESTHR(pConnection3.CreateInstance(__uuidof(Connection)));
pConnection3->ConnectionString = "Data Source=Pubs;";
pConnection3->Open("","","",adConnectUnspecified);
printf("cnn3 state: %s",
(LPCTSTR)GetState(pConnection3->State));
// Open a connection using a DSN and individual
// arguments instead of a connection string.
// It is assumed that you have create DSN 'Pubs' with a user name as
// 'MyUserId' and password as 'MyPassword'.
TESTHR(pConnection4.CreateInstance(__uuidof(Connection)));
pConnection4->Open("Pubs","MyUserId","MyPassword",adConnectUnspecified);
printf("cnn4 state: %s",
(LPCTSTR)GetState(pConnection4->State));
}
catch(_com_error &e)
{
// Notify user of any errors.
// Pass a connection pointer accessed from the Connection.
PrintProviderError(pConnection1);
if(pConnection2)
PrintProviderError(pConnection2);
if(pConnection3)
PrintProviderError(pConnection3);
if(pConnection4)
PrintProviderError(pConnection4);
PrintComError(e);
}
//Cleanup objects before exit.
if (pConnection1)
if (pConnection1->State == adStateOpen)
pConnection1->Close();
if (pConnection2)
if (pConnection2->State == adStateOpen)
pConnection2->Close();
if (pConnection3)
if (pConnection3->State == adStateOpen)
pConnection3->Close();
if (pConnection4)
if (pConnection4->State == adStateOpen)
pConnection4->Close();
}
///////////////////////////////////////////////////////////
// //
// GetState Function //
// //
///////////////////////////////////////////////////////////
_bstr_t GetState(int intState)
{
_bstr_t strState;
switch(intState)
{
case adStateClosed:
strState = "adStateClosed";
break;
case adStateOpen:
strState = "adStateOpen";
break;
default:
;
}
return strState;
}
///////////////////////////////////////////////////////////
// //
// PrintProviderError Function //
// //
///////////////////////////////////////////////////////////
void PrintProviderError(_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;
if( (pConnection->Errors->Count) > 0)
{
long nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(long i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("Error number: %x\t%s", pErr->Number,
(LPCSTR)pErr->Description);
}
}
}
///////////////////////////////////////////////////////////
// //
// PrintComError Function //
// //
///////////////////////////////////////////////////////////
void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
// Print Com errors.
printf("Error");
printf("\tCode = %08lx", e.Error());
printf("\tCode meaning = %s", e.ErrorMessage());
printf("\tSource = %s", (LPCSTR) bstrSource);
printf("\tDescription = %s", (LPCSTR) bstrDescription);
}
// EndConnectionStringCpp
/* 这是一个ADO的例子,看懂它,你一样可以连接ORACLE,不是能不能连,就看你想不想看懂这两个程序了,希望对你有用,祝你好运 */
阅读(1361) | 评论(0) | 转发(0) |