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

📄 ociexampledlg.cpp

📁 这是书上的代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// OCIExampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "OCIExample.h"
#include "OCIExampleDlg.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()

/////////////////////////////////////////////////////////////////////////////
// COCIExampleDlg dialog

COCIExampleDlg::COCIExampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COCIExampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COCIExampleDlg)
	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_ICON1);
	//初始化服务器句柄为空
	srvhp=NULL;
}

void COCIExampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COCIExampleDlg)
	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_E_CONFVAL, m_strConFVal);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COCIExampleDlg, CDialog)
	//{{AFX_MSG_MAP(COCIExampleDlg)
	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_UPDATE, OnButUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COCIExampleDlg message handlers

BOOL COCIExampleDlg::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 COCIExampleDlg::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 COCIExampleDlg::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 COCIExampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void COCIExampleDlg::OnButConnectdb() 
{
	//代码4.1.2
	//初始化OCI环境
	// environment initialization
	OCIEnvInit((OCIEnv **) &envhp,(ub4) OCI_DEFAULT,(size_t) 0,(dvoid **) 0 );
    //代码4.2.1: //svchp
	//分配句柄与数据结构
	//分配错误句柄
	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);
	// allocate describe handle
    //分配描述句柄
    OCIHandleAlloc((dvoid *) envhp,(dvoid **) &dschp,
                        OCI_HTYPE_DESCRIBE, 
                        (size_t) 0,(dvoid **) 0);
	//分配语句句柄
    ErrorProc(errhp,OCIHandleAlloc((dvoid *) envhp,(dvoid **) &stmthp,
                        (ub4) OCI_HTYPE_STMT,  
                        (size_t) 0,(dvoid **) 0));
	//代码4.2.1
	//产生接收用户数据的对话框
	CConectDlg  ConnDlg;
    ConnDlg.m_strDBName="imagedb";
    ConnDlg.m_strUserName="scott";
	ConnDlg.m_strUserID="tiger";
	if(ConnDlg.DoModal()==IDCANCEL)
		return;

	//代码4.2.2
	// 连接数据库
	LPCTSTR Server;
    Server=ConnDlg.m_strDBName;
    ErrorProc(errhp,
	     OCIServerAttach(srvhp,errhp,(unsigned char *)Server, (sb4)strlen(Server),
		                (ub4)OCI_DEFAULT));
    // 设置数据库的属性:服务器环境
    ErrorProc(errhp,OCIAttrSet((dvoid *) svchp, 
                        (ub4) OCI_HTYPE_SVCCTX,
                               (dvoid *) srvhp, 
                                       (ub4) 0, 
                         (ub4) OCI_ATTR_SERVER, 
                                       errhp));
	//代码4.2.3
	//认证用户并且开始会话
	LPCTSTR uid, pwd;
    uid=ConnDlg.m_strUserName;
    pwd=ConnDlg.m_strUserID;
	sword status;
	//认证用户名称;
    ErrorProc(errhp,OCIAttrSet(authp, 
		      (ub4)OCI_HTYPE_SESSION, 
	   (dvoid *)uid, (ub4)strlen(uid),
			        OCI_ATTR_USERNAME,
			                 errhp));	

    //认证用户密码
   ErrorProc(errhp,OCIAttrSet(authp, 
	         (ub4)OCI_HTYPE_SESSION, 
	                   (dvoid *)pwd, 
			       (ub4)strlen(pwd),
			 OCI_ATTR_PASSWORD,errhp));	

    //以申明的用户名称与密码开始会话 
   status=OCISessionBegin(svchp, 
	                      errhp, 
	                      authp, 
	       (ub4) OCI_CRED_RDBMS,
	          (ub4)OCI_DEFAULT);
    if(status!=0)
		return ;
	// set the Service context
    status=OCIAttrSet(svchp, (ub4)OCI_HTYPE_SVCCTX, 
	         (dvoid *)authp, (ub4)0,
			 OCI_ATTR_SESSION,errhp);
     if(status!=0)
	
	//代码4.3.2
	//处理SQL的第一步,准备SQL
	CString aa="AA";
    CString strSQL;
	strSQL="SELECT TABLE_NAME FROM USER_TABLES";
	text textSQL[1024];
	wsprintf((char*)textSQL,"%s",strSQL);
    //
	if(status=OCIStmtPrepare(stmthp,errhp,textSQL,strlen((char *)textSQL),OCI_NTV_SYNTAX,OCI_DEFAULT ))
	{
		ErrorProc(errhp,status);
		return;
	}
	//代码4.3.3
	//处理SQL的第三步,执行
	if(status=OCIStmtExecute(svchp,stmthp,errhp,(ub4)0,0,NULL,NULL,OCI_DEFAULT))
	{
		ErrorProc(errhp,status);
		return;
	}
    //代码4.3.4
    //处理SQL的第四步,描述
    ub4 col_num;//存放SELECT语句选中的列数
	ub4 k=1;
	//读取选择列表中的项数
	ErrorProc(errhp,OCIAttrGet(stmthp,OCI_HTYPE_STMT,&col_num,0,
		OCI_ATTR_PARAM_COUNT,errhp));
    //为选择项分配参数描述符
    ErrorProc(errhp,OCIParamGet(stmthp,OCI_HTYPE_STMT,errhp,(void **)&colhp,
			k));
    //读取选择项的数据长度
	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];
    
	//代码4.3.5
	//处理SQL的第五步,定义变量
	if(status=OCIDefineByPos(stmthp,&defhp[k-1],errhp,1,(ub1*)colbuf[0],
		collen[0]+1,SQLT_STR,&ind,0,0,OCI_DEFAULT))
	{
		ErrorProc(errhp,status);
		return;
	}

	//代码4.3.6
    //处理SQL的第六步,取值
	while((OCIStmtFetch(stmthp,errhp,1,OCI_FETCH_NEXT,OCI_DEFAULT))!=OCI_NO_DATA)
	{
		CString pass=colbuf[0];
		//把获取的用户的基表的名称加到ListBox控件中
		m_listTablename.AddString(pass);
	}
	//删除所分配的缓冲区
    delete  colbuf[0];
	//使得选择表的按钮可用
	GetDlgItem(IDC_B_TABLESELECTOK )->EnableWindow(true);

}

BOOL COCIExampleDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
  if(srvhp!=NULL)
  {
  //代码4.4.1
  //结束会话
  OCISessionEnd(svchp, errhp, authp, (ub4) 0);
  //断开与数据库的连接
  OCIServerDetach(srvhp, errhp, (ub4) OCI_DEFAULT);
  //释放OCI句柄 
  OCIHandleFree((dvoid *) srvhp, (ub4) OCI_HTYPE_SERVER);
  OCIHandleFree((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX);
  OCIHandleFree((dvoid *) errhp, (ub4) OCI_HTYPE_ERROR);
  OCIHandleFree((dvoid *) authp, (ub4) OCI_HTYPE_SESSION);
  OCIHandleFree((dvoid *) stmthp, (ub4) OCI_HTYPE_STMT);
  OCIHandleFree((dvoid *) dschp, (ub4) OCI_HTYPE_DESCRIBE);
  }
	return CDialog::DestroyWindow();
}

void COCIExampleDlg::ErrorProc(dvoid *err, sword status)
{
	 text errbuf[512];
	sb4  errcode;
	CString str;
	switch(status)
	{
	case OCI_SUCCESS:
		break;
	case OCI_SUCCESS_WITH_INFO:
		(void) AfxMessageBox("OCI错误代码:OCI_SUCCESS_WITH_INFO\n");
		break;
	case OCI_NEED_DATA:
		(void) AfxMessageBox("OCI错误代码:OCI_NEED_DATA\n");
		break;
	case OCI_NO_DATA:
		(void) AfxMessageBox("OCI错误代码:OCI_NO_DATA\n");
		break;
	case OCI_ERROR:
		(void)OCIErrorGet((dvoid *)errhp,(ub4)1,NULL,&errcode,
			errbuf,(ub4)sizeof(errbuf),OCI_HTYPE_ERROR);
		//(void)AfxMessageBox("错误号:%d\n错误信息:%s\n",errcode,errbuf);
		str.Format("错误号:%d\n错误信息:%s\n",errcode,errbuf);
		AfxMessageBox(str);

⌨️ 快捷键说明

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