⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cociexampledlg.cpp

📁 这个是用OCI接口在VC平台上对Oracle 数据库的操作.包括对数据库的连接,数据表的提取,查询,删除,修改等基本功能.这个代码已经编译实现.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// COCIExampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "COCIExample.h"
#include "COCIExampleDlg.h"
#include "ConnectDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCOCIExampleDlg dialog

CCOCIExampleDlg::CCOCIExampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCOCIExampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCOCIExampleDlg)
	m_strTablename = _T("");
	m_strFieldName = _T("");
	m_strCondition = _T("");
	m_strFieldVal = _T("");
	m_strConFName = _T("");
	m_strConFVal = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CCOCIExampleDlg::~CCOCIExampleDlg()
{
	OCISessionEnd(svchp,errhp,authp,(ub4)0);
	OCIServerDetach(srvhp,errhp,OCI_DEFAULT);

	OCIHandleFree((dvoid*)srvhp,OCI_HTYPE_SERVER);
	OCIHandleFree((dvoid*)svchp,OCI_HTYPE_SVCCTX);
	OCIHandleFree((dvoid*)errhp,OCI_HTYPE_ERROR);
	OCIHandleFree((dvoid*)authp,OCI_HTYPE_SESSION);
	OCIHandleFree((dvoid*)stmthp,OCI_HTYPE_STMT);
	OCIHandleFree((dvoid*)dschp,OCI_HTYPE_DESCRIBE);
}

void CCOCIExampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCOCIExampleDlg)
	DDX_Control(pDX, IDC_CO_CONFNAME, m_comConFNameCtrl);
	DDX_Control(pDX, IDC_COBFNAME, m_comboCtrl);
	DDX_Control(pDX, IDC_LIST_TABLEDATA, m_listCtrl);
	DDX_Control(pDX, IDC_LIST_TABLENAME, m_listTablename);
	DDX_LBString(pDX, IDC_LIST_TABLENAME, m_strTablename);
	DDX_CBString(pDX, IDC_COBFNAME, m_strFieldName);
	DDX_CBString(pDX, IDC_COMB_CONDITION, m_strCondition);
	DDX_Text(pDX, IDC_EFFIELDVAL, m_strFieldVal);
	DDX_CBString(pDX, IDC_CO_CONFNAME, m_strConFName);
	DDX_Text(pDX, IDC_EDIT1, m_strConFVal);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCOCIExampleDlg, CDialog)
	//{{AFX_MSG_MAP(CCOCIExampleDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUT_CONNECTDB, OnButConnectdb)
	ON_BN_CLICKED(IDC_B_TABLESELECTOK, OnBTableselectok)
	ON_BN_CLICKED(IDC_BUT_QUERY, OnButQuery)
	ON_BN_CLICKED(IDC_BUT_DELETE, OnButDelete)
	ON_BN_CLICKED(IDC_BUT_UPTATE, OnButUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCOCIExampleDlg message handlers

BOOL CCOCIExampleDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCOCIExampleDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCOCIExampleDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCOCIExampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCOCIExampleDlg::OnButConnectdb() 
{
	// TODO: Add your control notification handler code here	
	ErrorProc(errhp,OCIEnvInit(& envhp,OCI_DEFAULT,0,0));
	OCIHandleAlloc(envhp,(void**)& errhp,OCI_HTYPE_ERROR,0,0);
	OCIHandleAlloc(envhp,(void**)& srvhp,OCI_HTYPE_SERVER,0,0);
    OCIHandleAlloc(envhp,(void**)& svchp,OCI_HTYPE_SVCCTX,0,0);
	OCIHandleAlloc(envhp,(void**)& authp,OCI_HTYPE_SESSION,0,0);
    OCIHandleAlloc((dvoid*)envhp,(dvoid**)& dschp,OCI_HTYPE_DESCRIBE,0,0);
	OCIHandleAlloc((dvoid*)envhp,(dvoid**)& stmthp,OCI_HTYPE_STMT,0,0);

	CConnectDlg ConnDlg;
    ConnDlg.m_strDBName="vwserver";
	ConnDlg.m_strUserName="";
	ConnDlg.m_strUserID="";
	if(ConnDlg.DoModal()==IDCANCEL) 
	{
        MessageBox("DLG has been out!");
		return;
	}
	
	UpdateData(true);
	sword status;
	status=OCIServerAttach(srvhp,errhp,(unsigned char*)(LPCTSTR)ConnDlg.m_strDBName,
               (sb4)strlen(ConnDlg.m_strDBName),OCI_DEFAULT);
	if(status!=0)
	{
         MessageBox("Database has been found!");
	}
	
	ErrorProc(errhp,OCIAttrSet((dvoid*)svchp,(ub4) OCI_HTYPE_SVCCTX,
		  (dvoid*)srvhp,(ub4)0,OCI_ATTR_SERVER,errhp));

	LPCTSTR uid,pwd;
	uid=ConnDlg.m_strUserName;
	pwd=ConnDlg.m_strUserID;

	ErrorProc(errhp,OCIAttrSet(authp,OCI_HTYPE_SESSION,
		(dvoid*)uid,(ub4)strlen(uid),OCI_ATTR_USERNAME,errhp));
    ErrorProc(errhp,OCIAttrSet(authp,OCI_HTYPE_SESSION,(dvoid*)pwd,
		(ub4)strlen(pwd),OCI_ATTR_PASSWORD,errhp));
    
	//status=OCISessionBegin(svchp,errhp,authp,OCI_CRED_RDBMS,OCI_DEFAULT);//这句话连不上!
	ErrorProc(errhp,OCISessionBegin(svchp,errhp,authp,OCI_CRED_RDBMS,OCI_DEFAULT));
    

	if(status==OCI_ERROR) 
	{
		MessageBox("Database has not been connected!");
		return;
	}
	
	status=OCIAttrSet(svchp,OCI_HTYPE_SVCCTX,(dvoid*)authp,0,OCI_ATTR_SESSION,errhp);
	if(status!=0) return;

	CString strSQL;
	text    textSQL[1024];
	strSQL="SELECT TABLE_NAME FROM USER_TABLES";
	wsprintf((char*)textSQL,"%s",strSQL);
	status=OCIStmtPrepare(stmthp,errhp,textSQL,
		strlen((char*)textSQL),OCI_NTV_SYNTAX,OCI_DEFAULT);
	
	ErrorProc(errhp,OCIStmtExecute(svchp,stmthp,errhp,(ub4)0,0,
		NULL,NULL,OCI_DEFAULT));

//	ub4 col_num;//还没有使用.
	ErrorProc(errhp,OCIParamGet(stmthp,OCI_HTYPE_STMT,
		errhp,(void**)& colhp,1));
	ErrorProc(errhp,OCIAttrGet(colhp,OCI_DTYPE_PARAM,
		&collen[0],0,OCI_ATTR_DATA_SIZE,errhp));
	ErrorProc(errhp,OCIAttrGet(colhp,OCI_DTYPE_PARAM,
		&coltype[0],0,OCI_ATTR_DATA_TYPE,errhp));
	colbuf[0]=(text*)new text[(int)collen[0]+1];

	ErrorProc(errhp,OCIDefineByPos(stmthp,&defhp[0],
		errhp,1,(ub1*)colbuf[0],collen[0]+1,SQLT_STR,
		&ind[0],0,0,OCI_DEFAULT));                      //defhp中的参数修改过!

	while((OCIStmtFetch(stmthp,errhp,1,OCI_FETCH_NEXT,
		OCI_DEFAULT))!=OCI_NO_DATA)
	{
		m_listTablename.AddString((char*) colbuf[0]);
	}
	delete colbuf[0];
	
}

void CCOCIExampleDlg::ErrorProc(dvoid *err, sword status)
{
	CString str;
	sb4 errcode;
	text errbuf[512];
    if(status==OCI_ERROR)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -